Uva1368.dna consensus string

Source: Internet
Author: User

Original question link: http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & page = show_problem & problem = 4114

 

13845357 1368 DNA consensus string Accepted C ++ 0.022 12:29:30

 

 DNA consensus string

 

Figure 1. DNA (deoxyribonucleic acid) is the molecule which contains the genetic instructions. it consists of four different nucleus otides, namely adenine, thymine, guanine, and cytosine as shown in Figure 1. if we represent a nucleus otide by its initial character, a DNA strand can be regarded as a long string (sequence of characters) consisting of the four characters A, T, G, and C. for example, assume we are given some part of a DNA strand which is composed of the following sequence of nucleus otides:


''Thymine-adenine-cytosine-thymine-Guanine-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

 


Figure 2. DNA sequences of gene X in five animals.

 


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 shocould 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 stringsS=S1 ,...,SM of LengthN, The Consensus error between a stringYOf LengthNAnd the setSIs the sum of the Hamming distancesYAnd eachSI inS. If the consensus errorYAndSIs the minimum among all possible stringsYOf LengthN,YIs called a consensus stringS. 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''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 consistsTTest cases. The number of test casesTIs given in the first line of the Input. Each test case starts with a line containing two integersMAndNWhich are separated by a single space. The integerM(4M50) represents the number of DNA sequences andN(4N1000) represents the length of the DNA sequences, respectively. In each of the nextMLines, each DNA sequence is given.

Output

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

Solution: the question must be a string with the smallest Hamming distance. the Hamming value in the question is the number of different letters from the original string. To minimize the overall Hamming value, we should isolate each location for separate consideration, and then compare the brute force.

 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <cstdlib> 5 #include <cctype> 6 #include <cmath> 7 #include <algorithm> 8 #include <numeric> 9 using namespace std;10 int main() {11     int n, len, l; cin >> l;12     string gene[55];13     char TGCA[4];14     while(l--) {15         cin >> n >> len;16         int cnt = 0;17         for(int i = 0; i < n; i++) {18             cin >> gene[i];19         }20 21         for(int i = 0; i < len; i++) {22             memset(TGCA, 0, sizeof(TGCA));23             for(int j = 0; j < n; j++) {24                 if(gene[j][i] == ‘T‘) {25                     TGCA[0] ++;26                 } else if (gene[j][i] == ‘G‘) {27                     TGCA[1] ++;28                 } else if (gene[j][i] == ‘C‘) {29                     TGCA[2] ++;30                 } else if (gene[j][i] == ‘A‘) {31                     TGCA[3] ++;32                 }33 34             }35 36             if(TGCA[0] > TGCA[1] && TGCA[0] > TGCA[2] && TGCA[0] > TGCA[3]) {37                 cout << ‘T‘;38                 cnt += TGCA[1] + TGCA[2] + TGCA[3];39             } else if(TGCA[1] >= TGCA[0] && TGCA[1] > TGCA[2] && TGCA[1] > TGCA[3] ) {40                 cout << ‘G‘;41                 cnt += TGCA[0] + TGCA[2] + TGCA[3];42             } else if(TGCA[2] >= TGCA[0] && TGCA[2] >= TGCA[1] && TGCA[2] > TGCA[3]) {43                 cout << ‘C‘;44                 cnt += TGCA[1] + TGCA[0] + TGCA[3];45             } else if(TGCA[3] >= TGCA[0] && TGCA[3] >= TGCA[1] && TGCA[3] >= TGCA[2]) {46                 cout << ‘A‘;47                 cnt += TGCA[1] + TGCA[2] + TGCA[0];48             }49 50         }51         cout << endl << cnt << endl;52     }53     return 0;54 }

 




 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.