ArvinShaffer

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

这道题挺简单的,刚开始理解错误,以为是从已有的字符串里面求最短的距离,后面才发现是求一个到所有字符串最小距离的字符串,因为这样的字符串可能有多个,所以最后取最小字典序的字符串。

我的思路就是求每一列每个基因A、C、G、T的数量,找到最大值,最大值可能不止一个,但是锁定字典序最小的那个为所求字符串当前列的值,求解实例如下所示

TATGATAC
TAAGCTAC
AAAGATCC
TGAGATAC
TAAGATGT

对于上述字符串,第一列A,C,G,T的值分别为1,0,0,4,可见最大值为4,说明当前列到每个字符串之和最短的是T字母,所以得到所求字符串第一个字母为T,同样的方法求得剩下所有字符,最后得到所求字符串为TAAGATAC,而 consensus error的值等于每一列的值相加,第一列,最大值为4,那么consensus error在第一列的值为m-4=5-4=1;所以consensus error的值为1+1+1+0+1+0+2+1=7,具体实现代码如下:

#include<algorithm>  
#include<cstring>  
#include<cstdio> 
#include<iostream>
#define len  1010  
#define M 55  
using namespace std;

char dna[M][len];
int gene[5];
int m, n;


int main()
{
    int T;
    int minSum;
    char res[1010];
    cin >> T;
    while (T--) {
        cin >> m >> n;
        minSum = 0;
        for (int i = 0;i < m;i++)
            cin >> dna[i];
        int j = 0;
        for (j = 0;j < n;j++) {
            memset(gene, 0, sizeof(gene));
            for (int i = 0;i < m;i++) {
                switch (dna[i][j]) {
                case 'A':gene[0]++;break;
                case 'C':gene[1]++;break;
                case 'G':gene[2]++;break;
                case 'T':gene[3]++;break;
                default:break;
                }
            }
            int max = 0;
            for (int i = 0;i < 4;i++) {
                if (gene[i] > max) {
                    max = gene[i];
                    switch (i) {
                    case 0:res[j] = 'A';break;
                    case 1:res[j] = 'C';break;
                    case 2:res[j] = 'G';break;
                    case 3:res[j] = 'T';break;
                    default:break;
                    }
                }
            }
            minSum += m-max;
        }
        res[j] = '\0';
        cout << res << endl;
        cout << minSum << endl;

    }
    return 0;
}

 

posted on 2016-12-11 15:19  ArvinShaffer  阅读(122)  评论(0编辑  收藏  举报