hdu1080 LCS变形
dp[i][j]表示配对的最大值。
dp[i-1][j]表示s1[i-1]与'-'配对.
dp[i][j-1]表示s2[j-1]与'-'配对.
dp[i-1][j-1]表示s1[i-1]与s2[j-1]配对。
dp[i][j]=max(dp[i-1][j]+mp[s1[i-1]]['-'],dp[i][j-1]+mp['-'][s2[j-1]],dp[i-1][j-1]+mp[s1[i-1]][s2[j-1]]);
#include<stdio.h> #include<string.h> #define maxn 110 int dp[maxn][maxn]; int mp[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,-99999999 }; char s1[maxn],s2[maxn],c1,c2; int cal(char c) { if(c=='A')return 0; if(c=='C')return 1; if(c=='G')return 2; if(c=='T')return 3; return 4; } int max(int x,int y) { return x>y?x:y; } int main() { int i,j,t,l1,l2; scanf("%d",&t); while(t--) { scanf("%d%s",&l1,s1); scanf("%d%s",&l2,s2); dp[0][0]=0; for(i=1;i<=l1;i++) dp[i][0]=dp[i-1][0]+mp[cal(s1[i-1])][4]; for(i=1;i<=l2;i++) dp[0][i]=dp[0][i-1]+mp[4][cal(s2[i-1])]; for(i=1;i<=l1;i++) for(j=1;j<=l2;j++) { dp[i][j]=dp[i-1][j-1]+mp[cal(s1[i-1])][cal(s2[j-1])]; dp[i][j]=max(dp[i][j],dp[i-1][j]+mp[cal(s1[i-1])][4]); dp[i][j]=max(dp[i][j],dp[i][j-1]+mp[4][cal(s2[j-1])]); } printf("%d\n",dp[l1][l2]); } }