关闭

基因匹配问题

342人阅读 评论(0) 收藏 举报

就是dp, 数据处理的时候要注意。而且状态转换用了只用了一个sim[2][m],节省了空间。再者就是转换的时候有权值,加上权值比较即可。

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
char weight[5][5] = 
{
  {5,-1,-2,-1,-3},
  {-1,5,-3,-2,-4},
  {-2,-3,5,-2,-2},
  {-1,-2,-2,5,-1},
  {-3,-4,-2,-1,0},
};
int LenA, LenB, i, j;
int sum;
bool Cur;
char F[256];
char Str[101];
char GeneA[101], GeneB[101];
short sim[2][101];
inline short max(short A, short B)
{
  return A > B ? A:B;
} 
int main()
{
    F['A'] = 0; F['C'] = 1; F['G'] = 2; F['T'] = 3;
    scanf("%d",&sum);
    while(sum--)
    {
       scanf("%d %s",&LenA, &Str); 
       for(i=0; i<LenA; i++)
       GeneA[i+1] = F[Str[i]];
       scanf("%d %s", &LenB,&Str);
       for(i=0; i<LenB; i++)
       GeneB[i+1] = F[Str[i]];
       sim[0][0] = 0;
       for(j=1; j<=LenB; j++)
       sim[0][j] = sim[0][j-1] + weight[4][GeneB[j]];
       for(i=1, Cur=1; i<=LenA; i++,Cur =!Cur)
       {
         sim[Cur][0] = sim[!Cur][0] + weight[GeneA[i]][4];
         for(j=1; j<=LenB; j++)
         {
           sim[Cur][j] = sim[!Cur][j-1] + weight[GeneA[i]][GeneB[j]];
           sim[Cur][j] = max(sim[Cur][j], sim[Cur][j-1] + weight[4][GeneB[j]]);
           sim[Cur][j] = max(sim[Cur][j], sim[!Cur][j] + weight[GeneA[i]][4]);
         }
       }
       printf("%d\n", sim[!Cur][LenB]);
    } 
   system("pause");
   return 0;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:124808次
    • 积分:2362
    • 等级:
    • 排名:第16307名
    • 原创:115篇
    • 转载:11篇
    • 译文:3篇
    • 评论:13条
    常用链接
    最新评论