``Thymine-Adenine-Adenine-Cytosine-Thymine-Guanine-Cytosine-Cytosine-Guanine-Adenine-Thymine"
Then we can represent the above DNA strand with the string ``TAACTGCCGAT." The biologist Prof. Ahn found that a gene X commonly exists in the DNA strands of five different kinds of animals, namely dogs, cats, horses, cows, and monkeys. He also discovered that the DNA sequences of the gene X from each animal were very alike. See Figure 2.
DNA sequence of gene X | |
Cat: | GCATATGGCTGTGCA |
Dog: | GCAAATGGCTGTGCA |
Horse: | GCTAATGGGTGTCCA |
Cow: | GCAAATGGCTGTGCA |
Monkey: | GCAAATCGGTGAGCA |
Prof. Ahn thought that humans might also have the gene X and decided to search for the DNA sequence of X in human DNA. However, before searching, he should define a representative DNA sequence of gene X because its sequences are not exactly the same in the DNA of the five animals. He decided to use the Hamming distance to define the representative sequence. The Hamming distance is the number of different characters at each position from two strings of equal length. For example, assume we are given the two strings ``AGCAT" and ``GGAAT." The Hamming distance of these two strings is 2 because the 1st and the 3rd characters of the two strings are different. Using the Hamming distance, we can define a representative string for a set of multiple strings of equal length. Given a set of strings S = s1,..., sm of length n , the consensus error between a string y of length n and the set S is the sum of the Hamming distances between y and each si in S . If the consensus error between y and S is the minimum among all possible strings y of length n , y is called a consensus string of S . For example, given the three strings ``AGCAT" `` AGACT" and `` GGAAT" the consensus string of the given strings is `` AGAAT" because the sum of the Hamming distances between `` AGAAT" and the three strings is 3 which is minimal. (In this case, the consensus string is unique, but in general, there can be more than one consensus string.) We use the consensus string as a representative of the DNA sequence. For the example of Figure 2 above, a consensus string of gene X is `` GCAAATGGCTGTGCA" and the consensus error is 7.
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
心路历程:这个题目看到题以后我就想先把输入进来的多个字符串的每一个位置上出现的A,C,G,T进行统计然后存入相应的数组,进行完这一个步骤就要去求我们的目标字符串了,从第一个位置开始,是这个位置上出现次数最多的字母,并且记录不同的字母个数。这样就可以得出我们的目标函数和距离了。
PS:有一个很重要的是,那就是在求判断函数的时候一定要要按从小到大的顺序,这样可以保证这个字符串最小,要不然还要再去单独判断十分麻烦。
#include<stdio.h> #include<ctype.h> #include<string.h> int main() { int i, j, k, m, n, l, max, sum; scanf("%d", &k); for(i = 1; i <= k; i++) { char s1[1005], s2[1005], A[1005] = {0}, T[1005] = {0}, G[1005] = {0}, C[1005] = {0}; sum = 0; max = 0; scanf("%d%d", &m, &n); for(j = 0; j < m; j++) { scanf("%s", s1); for(l = 0; l < n; l++) { if(s1[l] == 'A') A[l] += 1; else if(s1[l] == 'C') C[l] += 1; else if(s1[l] == 'G') G[l] += 1; else if(s1[l] == 'T') T[l] += 1; } } for(j = 0; j < n; j++) { s2[j] = 'A'; max = A[j]; if(C[j] > max) { s2[j] = 'C'; max = C[j]; } if(G[j] > max) { s2[j] = 'G'; max = G[j]; } if(T[j] > max) { s2[j] = 'T'; max = T[j]; } sum += m - max; } s2[n] = '\0'; printf("%s\n%d\n", s2, sum); } return 0; }