UVA - 1368 - DNA Consensus String (字符串处理)

题意:给n串m长的DNA序列,要求从这n串序列中得出一个最相似的序列,若出现次数相同,按字典序,并输出与所给序列不同的总数

#include
#include
char dna[55][1005], res[1005];


int main()
{
    int T;
    scanf("%d", &T);
    while(T--){
        int n, m;
        int sum = 0, i, j;
        int maxn = 0, imax;

        scanf("%d%d",&n,&m);
        for(int i=0;iscanf("%s",dna[i]);

        for(i = 0; i < m; i++){
            int a[4] = {0};
            for(j = 0; j < n; j++){
                switch(dna[j][i]){
                        case 'A':   a[0]++; break;
                        case 'C':   a[1]++; break;
                        case 'G':   a[2]++; break;
                        case 'T':   a[3]++; break;
                    }
            }
            for(j = 0; j < 4; j++){
                if(a[j] > maxn){
                    maxn = a[j];
                    imax = j;
                }
            }
            sum = sum+n-maxn;
            maxn = 0;
            switch(imax){
                    case 0: res[i] = 'A';   break;
                    case 1: res[i] = 'C';   break;
                    case 2: res[i] = 'G';   break;
                    case 3: res[i] = 'T';   break;
            }

        }   
        res[i] = '\0';
        printf("%s\n%d\n", res, sum);


    }
    return 0;
}

你可能感兴趣的:(uva)