poj 1080 Human Gene Functions
#include <iostream> //DP
using namespace std;
int max(int a,int b,int c)
{
int aa=a,bb=b,cc=c;
if(aa<bb)swap(aa,bb);
return aa>cc?aa:cc;
}
int matrix(char a,char b)
{
if(a=='A')
{
switch (b)
{
case 'A':return 5;break;
case 'C':return -1;break;
case 'G':return -2;break;
case 'T':return -1;break;
default:return -3;break;
}
}
else if(a=='C')
{
switch (b)
{
case 'A':return -1;break;
case 'C':return 5;break;
case 'G':return -3;break;
case 'T':return -2;break;
default:return -4;break;
}
}
else if(a=='G')
{
switch (b)
{
case 'A':return -2;break;
case 'C':return -3;break;
case 'G':return 5;break;
case 'T':return -2;break;
default:return -2;break;
}
}
else if(a=='T')
{
switch (b)
{
case 'A':return -1;break;
case 'C':return -2;break;
case 'G':return -2;break;
case 'T':return 5;break;
default:return -1;break;
}
}
else
{
switch (b)
{
case 'A':return -3;break;
case 'C':return -4;break;
case 'G':return -2;break;
default:return -1;break;
}
}
}
char str1[110],str2[110];
int ans[110][110];
int dp(int i,int j) //求字符串1,str1[0]-str1[i-1]与字符串2,str2[0]-str2[j-1] 所能匹配的最大分数
{
if(ans[i][j]!=0)
return ans[i][j];
else if(i==0&&j==0)
ans[i][j]=0;
else if(i==0)
ans[i][j]=dp(i,j-1)+matrix('-',str2[j-1]);
else if(j==0)
ans[i][j]=dp(i-1,j)+matrix(str1[i-1],'-');
else
{
ans[i][j]=max(dp(i-1,j-1)+matrix(str1[i-1],str2[j-1]);
dp(i-1,j)+matrix(str1[i-1],'-'),dp(i,j-1)+matrix('-',str2[j-1]));
}
return ans[i][j];
}
int main()
{
int t,i,len1,len2;
scanf("%d",&t);
while(t--)
{
scanf("%d%s",&len1,str1);
scanf("%d%s",&len2,str2);
memset(ans,0,sizeof(ans));
printf("%d\n",dp(len1,len2));
}
return 0;
}