#include<iostream>
#include<string>
using namespace std;
int a[105];
int b[105];
int dp[105][105];
int ko[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 maxi(int a,int b)
{
if(a>b)
return a;
else return b;
}
int main()
{
char s1[105],s2[105];
int T,n1,n2,i,j;
cin>>T;
while(T!=0)
{
cin>>n1;
for(i=1;i<=n1;i++)
{
cin>>s1[i];
if(s1[i]=='A')
a[i]=0;
if(s1[i]=='C')
a[i]=1;
if(s1[i]=='G')
a[i]=2;
if(s1[i]=='T')
a[i]=3;
}
cin>>n2;
for(i=1;i<=n2;i++)
{
cin>>s2[i];
if(s2[i]=='A')
b[i]=0;
if(s2[i]=='C')
b[i]=1;
if(s2[i]=='G')
b[i]=2;
if(s2[i]=='T')
b[i]=3;
}
for(i=1;i<=n1;i++)
dp[i][0]=dp[i-1][0]+ko[a[i]][4];
for(i=1;i<=n2;i++)
dp[0][i]=dp[0][i-1]+ko[4][b[i]];
for(i=1;i<=n1;i++)
for(j=1;j<=n2;j++)
{
dp[i][j]=dp[i-1][j-1]+ko[a[i]][b[j]];//注意这里和LCS有不同.
dp[i][j]=maxi(maxi(dp[i][j],dp[i-1][j]+ko[a[i]][4]),dp[i][j-1]+ko[4][b[j]]);
}
cout<<dp[n1][n2]<<endl;
T=T-1;
}
return 0;
}
最长公共子序列的变形,这次是将长度改为了分数,方法还是LCS.特别注意初始化,以及判断字符该于哪个字符对应的问题.
看代码.