LCS的变形;
这一次才充分理解LCS;
dp[i+1][j+1]
dp[i][j+1]代表i被使用
同理代表j被使用
代码如下
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int value[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 getans(char a,char b)
{
int num1,num2;
if(a=='A') num1=0;
if(a=='C') num1=1;
if(a=='G') num1=2;
if(a=='T') num1=3;
if(a=='-') num1=4;
if(b=='A') num2=0;
if(b=='C') num2=1;
if(b=='G') num2=2;
if(b=='T') num2=3;
if(b=='-') num2=4;
return value[num1][num2];
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
int len1,len2;
char a[105],b[105];
scanf("%d%s",&len1,a);
scanf("%d%s",&len2,b);
int dp[105][105];
memset(dp,0,sizeof(dp));
for(int i=1;i<=len1;i++)
dp[i][0]=dp[i-1][0]+getans(a[i-1],'-');
for(int i=1;i<=len2;i++)
dp[0][i]=dp[0][i-1]+getans('-',b[i-1]);
for(int i=0;i<len1;i++)
for(int j=0;j<len2;j++){
dp[i+1][j+1]=dp[i][j]+getans(a[i],b[j]);
dp[i+1][j+1]=max(dp[i+1][j+1],dp[i][j+1]+getans(a[i],'-'));
dp[i+1][j+1]=max(dp[i+1][j+1],dp[i+1][j]+getans('-',b[j]));
}
printf("%d\n",dp[len1][len2]);
}
}