DNA Consensus String
Time limit: 3.000 seconds
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 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 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 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 (4![$ \le$](https://icpcarchive.ecs.baylor.edu/external/36/3602img2.png)
![$ \le$](https://icpcarchive.ecs.baylor.edu/external/36/3602img2.png)
![$ \le$](https://icpcarchive.ecs.baylor.edu/external/36/3602img2.png)
![$ \le$](https://icpcarchive.ecs.baylor.edu/external/36/3602img2.png)
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
这里要说明一下:
以下分析(包括C++代码)来源于:吊炸天的柯小帅
http://www.2cto.com/kf/201312/263670.html
题目大意:给定m个长度为n的DNA序列,求一个DNA序列,使其到所有序列的总hamming距离尽量小,如有多解,输出最小解。
解题思路:因为每个DNA序列都是相同长度的,所以可以枚举每一位DNA码,然后用贪心的思想选取出现次数最多的,如果有两个最多的,按要求用字典序小的。
以下是 吊炸天的柯小帅 的C++代码:
#include <stdio.h>
#include <string.h>
const int N = 1005;
const int M = 105;
int n, m, cnt[M];
char dna[M][N];
void init() {
scanf(%d%d, &m, &n);
for (int i = 0; i < m; i++) scanf(%s, dna[i]);
}
void solve() {
int ans = 0;
for (int i = 0; i < n; i++) {
int Max = 0, id;
memset(cnt, 0, sizeof(cnt));
for (int j = 0; j < m; j++) {
int tmp = dna[j][i] - 'A';
cnt[tmp]++;
if (cnt[tmp] > Max) {
Max = cnt[tmp];
id = tmp;
} else if (cnt[tmp] == Max && tmp < id)
id = tmp;
}
ans += m - Max;
printf(%c, 'A' + id);
}
printf(
%d
, ans);
}
int main() {
int cas;
scanf(%d, &cas);
while (cas--) {
init();
solve();
}
return 0;
}
而下面是本人原创的java代码:
import java.util.Scanner;
public class DNAConsensusString {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int T = input.nextInt();
for(int i = 0; i < T; i++) {
int m = input.nextInt();
int n = input.nextInt();
int[][] countArr = new int[4][n];//统计输入的DNA序列各列中 A、C、G、T的个数
for(int j = 0; j < 4; j++)
for(int k = 0; k < n; k++)
countArr[j][k] = 0;
for(int j = 0; j < m; j++) {
String str = input.next();
for(int k = 0; k < n; k++) {
char c = str.charAt(k);
if(c == 'A')
countArr[0][k]++;
else if(c == 'C')
countArr[1][k]++;
else if(c == 'G')
countArr[2][k]++;
else
countArr[3][k]++;
}
}
int hanmmingSum = 0;
int max;
char id = 'A';
char[] temp = {'A', 'C', 'G', 'T'};
for(int j = 0; j < n; j++) {
max = -1;
//找到列中最大值,以及对应的字母(当最大值相同时,找出最小字符序的字符)
for(int k = 0; k < 4; k++) {
if(max < countArr[k][j]) {
max = countArr[k][j];
id = temp[k];
}
else if(max == countArr[k][j] && temp[k] < id)
id = temp[k];
}
hanmmingSum += (m - max);
System.out.print(id);
}
System.out.println();
System.out.println(hanmmingSum);
}
}
}