题目链接:http://poj.org/problem?id=1080
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> using namespace std; const int maxn = 105; const int INF = 0x3f3f3f; int dp[maxn][maxn]; int A[maxn],B[maxn]; int mymap[6][6] = { {0, 0, 0, 0, 0, 0}, {0,5,-1,-2,-1,-3 }, {0,-1,5,-3,-2,-4 }, {0,-2,-3,5,-2,-2 }, {0,-1,-2,-2,5,-1 }, {0,-3,-4,-2,-1,0 } }; int tran(char ch){ int ret; switch(ch){ case 'A' : ret = 1; break; case 'C' : ret = 2; break; case 'G' : ret = 3; break; case 'T' : ret = 4; break; } return ret; } int main() { // freopen("E:\acm\input.txt","r",stdin); int T; cin>>T; while(T--){ int lenA,lenB; char a[maxn]; scanf("%d %s",&lenA,a+1); for(int i=1;i<=lenA;i++) A[i] = tran(a[i]); scanf("%d %s",&lenB,a+1); for(int i=1;i<=lenB;i++) B[i] = tran(a[i]); dp[0][0] = 0; for(int i=1;i<=lenB;i++) dp[0][i] = dp[0][i-1] + mymap[5][B[i]]; for(int i=1;i<=lenA;i++) dp[i][0] = dp[i-1][0] + mymap[A[i]][5]; // 初始化出现了问题,WA了一次。 for(int i=1;i<=lenA;i++) for(int j=1;j<=lenB;j++){ if(A[i] == B[j]) dp[i][j] = dp[i-1][j-1] + mymap[A[i]][B[j]]; else{ int Max = max(dp[i-1][j]+mymap[A[i]][5],dp[i][j-1]+mymap[5][B[j]]); dp[i][j] = max(dp[i-1][j-1] + mymap[A[i]][B[j]],Max); } } printf("%d ",dp[lenA][lenB]); } }