LRJ 3-7

#define _CRT_SECURE_NO_WARNINGS 

#include <cstdio>

int main()
{
    int T;
    int m, n;
    char dna[55][1005]; // 4 <= m <= 50, 4 <= n <= 1000


    scanf("%d", &T);
    while (T--) {
        scanf("%d%d", &m, &n);
        for (int i = 0; i < m; i++) {
            scanf("%s", dna[i]);
        }
        /*
        for (int i = 0; i < m; i++) {
            printf("%s\n", dna[i]);
        }
        */
        char ans[1005]; // 4 <= n <= 1000
        int cnt[4]; // count for 'A', 'C', 'G', 'T' in a column
        int max;
        char maxChar;

        // scan each column, select the character which occurs the most time, 
        // if there is a tie, select the lexicographically smaller one
        for (int j = 0; j < n; j++){ 

            cnt[0] = cnt[1] = cnt[2] = cnt[3] = max = 0;

            for (int i = 0; i < m; i++){
                switch (dna[i][j]){
                case 'A': {
                    cnt[0]++; 
                    if (cnt[0] > max || (cnt[0] == max && maxChar > 'A')){
                        max = cnt[0];
                        maxChar = 'A';
                    }
                    break;
                }
                case 'C': {
                    cnt[1]++;
                    if (cnt[1] > max || (cnt[1] == max && maxChar > 'C')){
                        max = cnt[1];
                        maxChar = 'C';
                    }
                    break;
                }
                case 'G': {
                    cnt[2]++;
                    if (cnt[2] > max || (cnt[2] == max && maxChar > 'G')){
                        max = cnt[2];
                        maxChar = 'G';
                    }
                    break;
                }
                case 'T': {
                    cnt[3]++;
                    if (cnt[3] > max){
                        max = cnt[3];
                        maxChar = 'T';
                    }
                    break;
                }
                }

            }

            ans[j] = maxChar;
        }
        ans[n] = '\0';
        printf("%s\n", ans);

        // count the total Hamming distance
        int h_dis = 0;
        for (int i = 0; i < m; i++){
            for (int j = 0; j < n; j++){
                if (dna[i][j] != ans[j])
                    h_dis++;
            }
        }
        printf("%d\n", h_dis);
    }

    return 0;
}

 

posted @ 2016-07-20 14:21  PatrickZhou  阅读(120)  评论(0编辑  收藏  举报