LA 3602 DNA Consensus String
题意:给m个长度为n的字符串,求一个长度为n的字符串,使得它和该m个不同字符之和最小,只有A、T、C、G四种字符
题解:直接暴力贪心,对于每一列,取最多的那个字符即可
1 #include<bits/stdc++.h> 2 using namespace std; 3 char s[52][1005]; 4 int main() 5 { 6 ios::sync_with_stdio(false); 7 int test; 8 cin >> test; 9 while( test -- ) 10 { 11 char t[1000+5]; 12 int n,m; 13 cin >> n >> m; 14 for(int i = 1;i <= n; i++ ) cin >> s[i]+1; 15 int ans = 0; 16 int pos[27]; 17 for(int j = 1;j <= m; j++) 18 { 19 memset(pos,0,sizeof(pos)); 20 for(int i = 1;i <= n;i++) pos[s[i][j]-'A']++; 21 int sum = max(pos[0],max(pos[2], max( pos[6], pos[('T'-'A')] ) )); 22 ans += (n-sum); 23 if(pos[0] >= pos[2] && pos[0] >= pos[6] && pos[0] >= pos['T'-'A']) t[j]='A'; 24 else if( pos[0] < pos[2] && pos[2] >= pos[6] && pos[2] >= pos['T'-'A'] ) t[j] = 'C'; 25 else if(pos[6] >= pos['T'-'A'] && pos[6] > pos[0] && pos[6] > pos[2]) t[j] = 'G'; 26 else t[j] = 'T'; 27 } 28 t[m+1] = '\0'; 29 cout << t+1 << endl; 30 cout << ans << endl; 31 } 32 }
浙公网安备 33010602011771号