解题思路是,统计所有字符串对应位置上出现次数最多的字母,并将其拼接起来作为Hamming距离和最小的DNA序列,也可以算作贪心。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #include<iostream> #include<string> #include<cmath> #include<cstring> #include<typeinfo> using namespace std; int t, m, n; int seq[5]; string strArr[1005]; inline void seqSet(char c) { switch (c) { case 'A':seq[1]++; break; case 'C':seq[2]++; break; case 'G':seq[3]++; break; case 'T':seq[4]++; break; } } char seqGet[6] = " ACGT"; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> t; while (t--) { cin >> m >> n; for (int i = 1; i <= m; i++)cin >> strArr[i]; int cnt = 0; for (int i = 0; i < n; i++) { memset(seq, 0, sizeof(seq)); for (int k = 1; k <= m; k++)seqSet(strArr[k][i]); int Pmax = 1; for (int p = 2; p <= 4; p++) if (seq[p] > seq[Pmax]) Pmax = p; cnt += m - seq[Pmax]; cout << seqGet[Pmax]; } cout << '\n'; cout << cnt << '\n'; } } |