第一行输入两个正整数
代表矩阵的行数和列数。
此后
行,每行输入一个长度为
、仅由
和
构成的字符串,代表矩阵每一行中的元素。
输出一个整数,表示至多能使得多少列的元素均为
。
3 4 1111 1111 1111
4
在这个样例中,不需要进行操作,所有列的元素均为
。
3 2 01 10 11
1
在这个样例中,我们可以选择对第一行进行操作,使得第一行变为
,此时,第一列的元素均为
。
n,m = list(map(int,input().split()))
s = ''
dic ={}
for i in range(n):
s = s+input()
for i in range(m):
x = s[i::m]
if x in dic:
dic[x] += 1
else:
dic[x] = 1
print(max(dic.values())) package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
reader := bufio.NewReader(os.Stdin)
// 读取n和m
var n, m int
fmt.Fscan(reader, &n, &m)
// 读取矩阵
matrix := make([]string, n)
for i := 0; i < n; i++ {
fmt.Fscan(reader, &matrix[i])
}
// 统计每列字符串的出现次数
count := make(map[string]int)
// 使用strings.Builder来构建列字符串
var sb strings.Builder
sb.Grow(n) // 预分配空间
for j := 0; j < m; j++ {
sb.Reset()
for i := 0; i < n; i++ {
sb.WriteByte(matrix[i][j])
}
count[sb.String()]++
}
// 找出最大值
max := 1
for _, v := range count {
if v > max {
max = v
}
}
fmt.Println(max)
}
package main
import (
"fmt"
)
func main() {
var n, m int
fmt.Scanf("%d %d", &n, &m)
grid := make([]string, n)
for i := 0; i < n; i++ {
fmt.Scanf("%s", &grid[i])
}
colBytes := make([]byte, n) // 复用
count := make(map[string]int)
for j := 0; j < m; j++ {
for i := 0; i < n; i++ {
colBytes[i] = grid[i][j]
}
count[string(colBytes)]++
}
maxCount := 0
for _, c := range count {
if c > maxCount {
maxCount = c
}
}
fmt.Println(maxCount)
} // 遍历每一列,每一列的字符组成一个字符串,相同的字符串可以在变换的情况下是的全为 1
// 不同的字符串不可能通过变换使得全为 1 ( 能发现这一点就好了 ) 引用一个c++大佬写的注释
// 只需要统计每列字符串相同的数量就行
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
// 读取输入
let dp = []
let condition = undefined;
rl.on("line", function (line) {
if(!condition){
condition = line.split(" ").map(Number)
// [1,1,1,1]
dp = new Array(condition[1]).fill(1)
}else{
let arr = line.split('');
for(let i=0; i<arr.length; i++){
// 不能用字符串拼接,不然运算会超时
// 把dp中所有的掩码往左移一位这时候每一列都一样
// 然后再和输入的这列中的数 0 或 1进行 或运算后就得到一个不一样的唯一数
dp[i] = (dp[i]<<1) | Number(arr[i])
}
}
}).on("close", function() {
// 把相同列用位运算做统计
let countMap = {}
for(let i=0; i<dp.length; i++){
if(countMap[dp[i]]){
countMap[dp[i]]++
}else{
countMap[dp[i]] = 1
}
}
// 找出对象中最大的值
let sum = 0
Object.keys(countMap).forEach(key=>{
if(countMap[key]>sum){
sum = countMap[key]
}
})
console.log(sum)
})
把矩阵保存到数组里,遍历数组的每一列,若列字符串相等,说明无论翻转哪几行,这两个字符串始终都是相同的(能把它翻转为全为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);
}
} #include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<string> vec;
for(int i=0; i<n; i++) {
string str;
cin >> str;
vec.push_back( str );
}
unordered_map<string, int> ord_map;
for ( int i = 0; i < m; i++ ) {
string s;
for ( int j = 0; j < n; j++ ) {
s += vec[j][i];
}
ord_map[s]++;
}
int res = 0;
for (auto map : ord_map) {
res = max( res, map.second );
}
cout << res << endl;
return 0;
}
#include <algorithm>
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<string> array(n);
for (int i = 0; i < n; ++i)
cin >> array[i];
unordered_map<string, int> map;
for (int i = 0; i < m; ++i) {
string s;
for (int j = 0; j < n; ++j)
s += array[j][i];
++map[s];
}
int max = max_element(map.begin(), map.end(), [](const auto & a,
const auto & b) {
return a.second < b.second;
})->second;
cout << max;
}