DNA Consensus String UVA - 1368

来源:互联网 发布:单词背诵软件 编辑:程序博客网 时间:2024/05/16 03:33

开始的时候,我以为从给出的几个DNA序列之中找出距离最小的DNA序列。一直做也都不对,后来数了一下发现第一个样例的结果确实是10而不是7,那就排除了代码正确性的问题,之后又看了一遍样例,发现第一个样例结果的最短距离DNA序列并不在给出的几个DNA序列之中,恍然大悟。

#include <stdio.h>#include <string.h>#define maxn 1010char str[maxn][maxn];char res[maxn];int cof[maxn];int main(void){    int m;    scanf("%d", &m);    while(m--){        int r, len;        scanf("%d%d", &r, &len);        int i, j;        for(i = 0; i < r; i++)            scanf("%s", str[i]);        for(j = 0; j < len; j++){            memset(cof, 0, sizeof(cof));            for(i = 0; i < r; i++){                if(str[i][j] == 'A')                    cof[1]++;                else if(str[i][j] == 'C')                    cof[2]++;                else if(str[i][j] == 'G')                    cof[3]++;                else if(str[i][j] == 'T')                    cof[4]++;            }            int M = 1;            for(i = 2; i <= 4; i++){                if(cof[i] > cof[M])                    M = i;            }//ACGT在整数数组中的默认的排序解决字典序问题            if(M == 1)                res[j] = 'A';            else if(M == 2)                res[j] = 'C';            else if(M == 3)                res[j] = 'G';            else if(M == 4)                res[j] = 'T';        }        res[len] = '\0';        int num = 0;        for(i = 0; i < r; i++){            for(j = 0; j < len; j++){                if(str[i][j] != res[j])                    num ++;            }        }        printf("%s\n", res);        printf("%d\n", num);    }    return 0;}


0 0
原创粉丝点击