【算法竞赛入门经典】习题3-7 DNA序列(DNA Consensus String,ACM/ICPC Seoul 2006,UVa 1368)

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 ≤ m ≤ 50) represents the number of DNA sequences and n (4 ≤ n ≤ 1000) represents the length of the DNA sequences, respectively. In each 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 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.

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

#include<stdio.h>
#include<string.h>

int m,n,sum=0;
char a[55][1002];
char b[1002];
int c[1002][5];
const char d[]="ACGT";

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        sum=0;
        memset(c,0,sizeof(c));
        scanf("%d %d\n",&m,&n);
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                scanf("%c",&a[i][j]);
                if(a[i][j]=='A') c[j][0]++;
                else if(a[i][j]=='C')  c[j][1]++;
                else if(a[i][j]=='G')  c[j][2]++;
                else c[j][3]++;
            }
            getchar();
        }
        for(int i=0;i<n;i++){
            int k,maxn=-1;
            for(int j=0;j<4;j++){
                if(c[i][j]>maxn){
                    maxn=c[i][j];
                    k=j;
                }
            }
            sum+=(m-maxn);
            b[i]=d[k];
        }

        for(int i=0;i<n;i++)
            printf("%c",b[i]);
        printf("\n%d\n",sum);
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值