I 've been wrong about my questions recently, orz
The question is to find a string with the smallest sum of hamming values, rather than finding the smallest one from the given character.
In this case, we will perform column-by-column processing. The character at the current position of the requested string should be the highest number of occurrences in the column followed by the smallest ASCII Value
The Code is a little frustrated, and there are too many if statements
1 //#define LOCAL 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 using namespace std; 6 7 char gene[52][1010], ans[1010]; 8 int num[4]; 9 10 int main(void)11 {12 #ifdef LOCAL13 freopen("3602in.txt", "r", stdin);14 #endif15 16 int T;17 scanf("%d", &T);18 while(T--)19 {20 int n, m, hamming = 0;21 scanf("%d%d", &n ,&m);22 for(int i = 0; i < n; ++i)23 scanf("%s", gene[i]);24 for(int i = 0; i < m; ++i)25 {26 int Max = 0;27 for(int j = 0; j < 4; ++j) num[j] = 0;28 for(int j = 0; j < n; ++j)29 {30 if(gene[j][i] == ‘A‘) ++num[0];31 if(gene[j][i] == ‘C‘) ++num[1];32 if(gene[j][i] == ‘G‘) ++num[2];33 if(gene[j][i] == ‘T‘) ++num[3];34 }35 for(int j = 1; j < 4; ++j)36 if(num[j] > num[Max])37 Max = j;38 if(Max == 0) ans[i] = ‘A‘;39 if(Max == 1) ans[i] = ‘C‘;40 if(Max == 2) ans[i] = ‘G‘;41 if(Max == 3) ans[i] = ‘T‘;42 hamming += num[0] + num[1] + num[2] + num[3] - num[Max];43 }44 ans[m] = ‘\0‘;45 printf("%s\n%d\n", ans, hamming);46 }47 return 0;48 }
Code Jun
La 3602 DNA consensus string