1368:DNA Consensus String

问题简述:给定m个长度为n的DNA序列,求一个DNA序列,使其到所有这些序列的总hamming距离尽量小,如果有多个解,输出字典顺序的最小解。

贪心

#include
#include
using namespace std;
const int maxn = 1000 + 5,maxm = 50 + 5;
char table[maxn][maxn];
char A[] = {'A','C','G','T'};
int main(){
    int m,n,T;
    scanf("%d",&T);
    while(T--){
        memset(table,'\0',sizeof(table));
        scanf("%d %d",&m,&n);
        for(int i = 0;i < m;i++){
            scanf("%s",table[i]);
        }
        int dis = 0;
        char s[n];
        for(int i = 0;i < n;i++){
            int num[26] = {0};
            for(int j = 0;j < m;j++){
                num[table[j][i] - 'A']++;
            }
            int maxcnt = 0;
            char choose;
            for(int i = 0;i < 4;i++){
                if(num[A[i] - 'A'] > maxcnt){
                    maxcnt = num[A[i] - 'A'];
                    choose = A[i];
                }
            }
            dis += m - maxcnt;
            s[i] = choose;
        }
        for(int i = 0;i < n;i++) putchar(s[i]);
        printf("\n%d\n",dis);
    }

    return 0;
}

你可能感兴趣的:(UVa,ACM-ICPC,水题,字符串,贪心)