码迷,mamicode.com
首页 > 其他好文 > 详细

LRJ 3-7

时间:2016-07-20 15:03:37      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

#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;
}

 

LRJ 3-7

标签:

原文地址:http://www.cnblogs.com/patrickzhou/p/5688210.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!