原题地址
解题思路
直接对每一列在线处理,更新当前出现次数最多的字符,最后将每列的个数减去该字符的数量,就得到与之不同的字符个数。累加得到题目要求的Hamming distances
。
参考代码
#include<bits/stdc++.h>
using namespace std;
#define LOCAL //提交的时候一定注释
#define _for(i, a, b) for(int i = (a); i < (b); ++i)
#define _rep(i, a, b) for(int i = (a); i <= (b); ++i)
#define pb push_back
#define VI vector<int>
#define maxn 30
#define INF 0x3f3f3f3f
typedef long long LL;
vector<string> str;
int num[maxn];
int readint() {
int x; scanf("%d", &x); return x;
}
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int t, n, m;
scanf("%d", &t);
string s;
while (t--) {
str.clear();
scanf("%d%d\n", &n, &m);
_for(i, 0, n) {
getline(cin, s);
str.pb(s);
}
int sum = 0, less = 0;
string ans = "";
_for(j, 0, m) {
memset(num, 0, sizeof(num));
int cnt = 0;
char now = 'Z', tmp;
_for(i, 0, n) {
tmp = str[i][j];
num[tmp - 'A']++;
if (num[tmp - 'A'] > cnt || (num[tmp - 'A'] == cnt && tmp < now)) {
now = tmp;
cnt = num[tmp - 'A'];
}
}
ans = ans + now;
less += (n - num[now - 'A']);
}
printf("%s\n%d\n", ans.c_str(), less);
}
return 0;
}