第一行输入两个正整数
代表矩阵的行数和列数。
此后
行,每行输入一个长度为
、仅由
和
构成的字符串,代表矩阵每一行中的元素。
输出一个整数,表示至多能使得多少列的元素均为
。
3 4 1111 1111 1111
4
在这个样例中,不需要进行操作,所有列的元素均为
。
3 2 01 10 11
1
在这个样例中,我们可以选择对第一行进行操作,使得第一行变为
,此时,第一列的元素均为
。
把矩阵保存到数组里,遍历数组的每一列,若列字符串相等,说明无论翻转哪几行,这两个字符串始终都是相同的(能把它翻转为全为1),记录相等列字符串的最大值即为所求。
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int rows = in.nextInt();
int cols = in.nextInt();
in.nextLine();
int res = 0;
String[] row = new String[rows];
for (int i = 0; i < rows; i++){
row[i] = in.nextLine();
}
HashMap<String, Integer> map = new HashMap<>();
for (int j = 0; j < cols; j++){
StringBuilder sb = new StringBuilder();
for (int i = 0; i < rows; i++){
sb.append(row[i].charAt(j));
}
String s = sb.toString();
map.merge(s, 1, Integer :: sum);
res = Math.max(res, map.get(s));
}
System.out.println(res);
}
}