NYOJ_246_Human Gene Functions

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<queue>
using namespace std;
int mark[120][120],dp[120][120];
int max(int a,int b,int c)
{
    if(a<b)
        a=b;
    if(a<c)
        a=c;
    return a;
}
void init() //初始化
{
     mark['A']['A']=mark['C']['C']=mark['G']['G']=mark['T']['T']=5;
    mark['A']['C']=mark['C']['A']=mark['A']['T']=mark['T']['A']=-1;
    mark[' ']['T']=mark['T'][' ']=-1;
    mark['A']['G']=mark['G']['A']=mark['C']['T']=mark['T']['C']=-2;
    mark['G']['T']=mark['T']['G']=mark['G'][' ']=mark[' ']['G']=-2;
    mark['A'][' ']=mark[' ']['A']=mark['C']['G']=mark['G']['C']=-3;
    mark['C'][' ']=mark[' ']['C']=-4;
}
int main()
{
   int t,l1,l2,i,j,k;
   char a[105],b[105];
   init();
   scanf("%d",&t);
   while(t--)
   {
       scanf("%d %s",&l1,a+1); //让字符串从下标1开始有利于计算
       scanf("%d %s",&l2,b+1);
       dp[0][0]=0;
       for(i=1;i<=l1;++i)
           dp[i][0]=dp[i-1][0]+mark[a[i]][' ']; //为何dp[i][0]要累加前面的值?
       for(i=1;i<=l2;++i)
           dp[0][i]=dp[0][i-1]+mark[' '][b[i]];
       for(i=1;i<=l1;++i)
           for(j=1;j<=l2;++j) //对于i位置和j位置,dp[i][j]有三种选择
           {
               dp[i][j]=max(dp[i][j-1]+mark[b[j]][' '],dp[i-1][j]+mark[a[i]][' '],dp[i-1][j-1]+mark[a[i]][b[j]]);
           }
           printf("%d\n",dp[l1][l2]);
   }
  return 0;
}

 

posted @ 2013-05-14 09:21  小仪在努力~  阅读(142)  评论(0编辑  收藏  举报