uva1368 DNA Consensus String

Source: Internet
Author: User

<tex2html_verbatim_mark> Figure 1.

DNA (deoxyribonucleic Acid) is the molecule which contains the genetic instructions. It consists of four different nucleotides, namely adenine, thymine, guanine, and cytosine as shown in Figure 1. If we represent a nucleotide by it initial character, a DNA strand can be regarded as a long string (sequence of characte RS) consisting of the four characters A, T, G, and C. For example, assume we were given some part of a DNA strand which was composed of the following sequence of nucleotides:


' 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 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 has 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 is 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 both strings of equal length. For example, assume we is given the strings 'Agcat"and"Ggaat." The Hamming distance of these, strings is 2 because the 1st and the 3rd characters of the and the strings of the. 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<tex2html_verbatim_mark> of lengthN<tex2html_verbatim_mark> the consensus error between a stringy<tex2html_verbatim_mark> of lengthN<tex2html_verbatim_mark> and the SetS<tex2html_verbatim_mark> is the sum of the Hamming distances betweeny<tex2html_verbatim_mark> and eachsI<tex2html_verbatim_mark> inS<tex2html_verbatim_mark>. If the consensus error betweeny<tex2html_verbatim_mark> andS<tex2html_verbatim_mark> is the minimum among all possible strings  y <tex2html_verbatim_mark> of length  n <tex2html_verbatim_mark> ,  y <tex2html_verbatim_mark> is Called a consensus string Of S <tex2html_verbatim_mark> . For example, given the three strings '   agcat "'   agact " and "&NBSP; ggaat " the cons Ensus string of the given strings is '   agaat ' because the sum of the Hamming distances between '   Agaat and the three strings are 3 which is minimal. The consensus string is a 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 err Or is 7.

Input

Your program was to read from standard input. The input consists ofT<tex2html_verbatim_mark> test Cases. The number of test casesT <tex2html_verbatim_mark> is given in the first line of the input. Each test case starts with a line containing the integers m<tex2html_verbatim_mark> and n& Lt;tex2html_verbatim_mark> which is separated by a single space. The integer m<tex2html_verbatim_mark>(4m) <tex2html_verbatim_mark> Represents the number of DNA sequences and n<tex2html_verbatim_mark>(4n1000) <tex2html_verbatim_mark> represents the length of the DNA sequences, respectively. In each of the next m<tex2html_verbatim_mark> lines, each DNA sequence is given.

Output

Your program is-to-write to standard output. Print the consensus string in the first line of all case and the consensus error in the second line for 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 acgtacgtac ccgtacgtag gcgtacgtat tcgtacgtaa 6 ATGTTACCAT AAGTTA Cgat AACAAAGCAA AAGTTACCTT AAGTTACCAA TACTTACCAA
Sample Output
Taagatac 7 Acgtacgtaa 6 AAGTTACCAA 12
This problem is relatively simple, note the dictionary order is good.
Attach the AC code:
1#include <iostream>2#include <algorithm>3 4 using namespacestd;5 Charstr[ -][ -];6 Charch[ -];7 intt[4];8 intN;9 intb;Ten intSum; One intFlag; A intMain () - { -CIN >>N; the      while(n--) -     { -sum=0; -Cin >> a >>b; +          for(intI=0; i<a;i++) -CIN >>Str[i]; +          for(intI=0; i<b;i++) A         { atflag=0; -t[0]=t[1]=t[2]=t[3]=0; -              for(intj=0; j<a;j++) -             { -                 if(str[j][i]=='A') t[0]++; -                 if(str[j][i]=='C') t[1]++; in                 if(str[j][i]=='G') t[2]++; -                 if(str[j][i]=='T') t[3]++; to             } +              for(intI=0;i<4; i++) -                 if(T[i]>t[flag]) flag=i; the             if(flag==0) cout <<'A'; *             if(flag==1) cout <<'C'; $             if(flag==2) cout <<'G';Panax Notoginseng             if(flag==3) cout <<'T'; -sum+=T[flag]; the         } +cout <<Endl; Acout << a*b-sum <<Endl; the     } +     return 0; -}
View Code

uva1368 DNA Consensus String

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.