Input
Your program is to read from standard input. The input consists of T test cases. The number of test cases T is given in the first line of the input. Each test case starts with a line containing two integers m and n which are separated by a single space. The integer m (4Output
Your program is to write to standard output. Print the consensus string in the first line of each case and the consensus error in the second line of each case. If there exists more than one consensus string, print the lexicographically smallest consensus string. The following shows sample input and output for three test cases.Sample Input
3 5 8 TATGATAC TAAGCTAC AAAGATCC TGAGATAC TAAGATGT 4 10 ACGTACGTAC CCGTACGTAG GCGTACGTAT TCGTACGTAA 6 10 ATGTTACCAT AAGTTACGAT AACAAAGCAA AAGTTACCTT AAGTTACCAA TACTTACCAA
Sample Output
TAAGATAC 7 ACGTACGTAA 6 AAGTTACCAA 12
分析:读取字符串,找到每列出现次数最多的字符。
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;
const int maxn = 1000 + 10;
int cnt[maxn][26];
char ans[maxn];
int main()
{
int T;
int m, n;
string s;
cin >> T;
while(T--) {
cin >> m >> n;
memset(cnt, 0, sizeof(cnt));
for(int i=0; i<m; i++) {
cin >> s;
for(int j=0; j<n; j++) {
cnt[j][s[j]-'A']++;
}
}
int diff = 0;
for(int i=0; i<n; i++) {
int total = 0, ch = 'A';
for(int j=0; j<26; j++) {
if(cnt[i][j]>total) {
total = cnt[i][j];
ch = 'A' + j;
}
}
diff += m - total;
ans[i] = ch;
}
for(int i=0; i<n; i++) putchar(ans[i]);
printf("\n%d\n", diff);
}
return 0;
}