UVa-1368

来源:互联网 发布:买家如何避免淘宝客 编辑:程序博客网 时间:2024/05/17 05:01

  DNA (Deoxyribonucleic Acid) is the molecule which containsthe genetic instructions. It consists of four differentnucleotides, namely Adenine, Thymine, Guanine, and Cytosineas shown in Figure 1. If we represent a nucleotide byits initial character, a DNA strand can be regarded as a longstring (sequence of characters) consisting of the four charactersA, T, G, and C. For example, assume we are given somepart of a DNA strand which is composed of the following sequenceof nucleotides:

  “Thymine-Adenine-Adenine-Cytosine-Thymine-Guanine-CytosineCytosine-Guanine-Adenine-Thymine”

Then we can represent the above DNA strand with thestring “TAACTGCCGAT.”

  The biologist Prof. Ahn found that a gene X commonlyexists in the DNA strands of five different kinds of animals,namely dogs, cats, horses, cows, and monkeys. He also discoveredthat the DNA sequences of the gene X from each animalwere very alike. See Figure 2.


  Prof. Ahn thought that humans might also have the gene X and decided to search for the DNAsequence of X in human DNA. However, before searching, he should define a representative DNAsequence of gene X because its sequences are not exactly the same in the DNA of the five animals. Hedecided 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 ofequal length. For example, assume we are given the two strings “AGCAT” and “GGAAT.” The Hammingdistance 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 equallength. Given a set of strings S = {s1, . . . , sm} of length n, the consensus error between a string y oflength n and the set S is the sum of the Hamming distances between y and each siin S. If the consensuserror between y and S is the minimum among all possible strings y of length n, y is called a consensusstring of S. For example, given the three strings “AGCAT” “AGACT” and “GGAAT” the consensus stringof the given strings is “AGAAT” because the sum of the Hamming distances between “AGAAT” and thethree strings is 3 which is minimal. (In this case, the consensus string is unique, but in general, therecan be more than one consensus string.) We use the consensus string as a representative of the DNAsequence. For the example of Figure 2 above, a consensus string of gene X is “GCAAATGGCTGTGCA” andthe consensus error is 7.


Input

Your program is to read from standard input. The input consists of T test cases. The number of testcases T is given in the first line of the input. Each test case starts with a line containing two integersm and n which are separated by a single space. The integer m (4 ≤ m ≤ 50) represents the numberof DNA sequences and n (4 ≤ n ≤ 1000) represents the length of the DNA sequences, respectively. Ineach of the next m lines, each DNA sequence is given.


Output

Your program is to write to standard output. Print the consensus string in the first line of each caseand the consensus error in the second line of each case. If there exists more than one consensus string,print the lexicographically smallest consensus string.


Sample Input

3

5 8

TATGATACT

AAGCTACA

AAGATCCT

GAGATACT

AAGATGT

4 10

ACGTACGTAC

CCGTACGTAG

GCGTACGTAT

TCGTACGTAA

6 10

ATGTTACCAT

AAGTTACGAT

AACAAAGCAA

AAGTTACCTT

AAGTTACCAA

TACTTACCAA


Sample Output

TAAGATAC

7

ACGTACGTAA

6

AAGTTACCAA

12


思路:本来以为在给出的序列中找最优解,但实际不是。分别找出每一列中最多的字母,求其个数。

欢迎交流:

#include<stdio.h>#include<string.h>char s[55][1005];int maxnum[1005];char maxchar[1005];int main(){int t;scanf("%d", &t);while (t--){memset(maxnum, 0, sizeof(maxnum));memset(maxchar, '\0', sizeof(maxchar));memset(s, 0, sizeof(s));int m, n;scanf("%d%d", &m, &n);for (int i = 0; i < m; i++){scanf("%s", s[i]);}for (int j = 0; j <n;j++){int numa=0, numc=0, numg=0, numt=0; for (int i = 0; i < m; i++){if (s[i][j] == 'A')numa++;else if (s[i][j] == 'C')numc++;else if (s[i][j] == 'G')numg++;else if (s[i][j] == 'T')numt++;}maxnum[j] = numa;maxchar[j] = 'A';if (maxnum[j] < numc){ maxnum[j] = numc; maxchar[j] = 'C'; }if (maxnum[j] < numg){ maxnum[j] = numg; maxchar[j] = 'G'; }if (maxnum[j] < numt){ maxnum[j] = numt; maxchar[j] = 'T'; }}int num = 0;for (int i = 0; i < n; i++)num += maxnum[i];printf("%s\n%d\n", maxchar, m*n-num);}return 0;}




0 0