题目地址:http://poj.org/problem?id=1080
解析:dp[i][j]来由:
1.dp[i][j-1]转移,则‘-’和s2[j]匹配。
2.dp[i-1][j]转移,则s1[i]和‘-’匹配。
3.dp[i-1][j-1],则s1[i]和s2[j]匹配。三者取最大值即可。
#include <iostream>
#include <cmath>
#include <string>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <cstdio>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
using namespace std;
typedef long long ll;
#define INF 0x7fffffff
#define MAX(a,b) a>b?a:b
#define MIN(a,b) a>b?b:a
#define N 103
int len2,len1;
int dp[N][N];
char s1[N],s2[N];
int match[N][N];
void init(){
match['A']['T']=-1;match['T']['A']=-1;match['A']['A']=5;match['T']['T']=5;
match['A']['C']=-1;match['C']['A']=-1;match['A']['G']=-2;match['G']['A']=-2;
match['C']['G']=-3;match['G']['C']=-3;match['G']['G']=5;match['C']['C']=5;
match['C']['T']=-2;match['T']['C']=-2;match['T']['G']=-2;match['G']['T']=-2;
match['A']['-']=-3;match['-']['A']=-3;match['-']['C']=-4;match['C']['-']=-4;
match['G']['-']=-2;match['-']['G']=-2;match['-']['T']=-1;match['T']['-']=-1;
}
void solve(){
int i,j;
dp[0][0]=0;
for(i=1;i<=len1;i++)
dp[i][0]=dp[i-1][0]+match[s1[i]]['-'];
for(i=1;i<=len2;i++)
dp[0][i]=dp[0][i-1]+match['-'][s2[i]];
for(i=1;i<=len1;i++)
for(j=1;j<=len2;j++)
{
dp[i][j]=max(dp[i-1][j-1]+match[s1[i]][s2[j]],max(dp[i-1][j]+match[s1[i]]['-'],dp[i][j-1]+match['-'][s2[j]]));
}
cout<<dp[len1][len2]<<endl;
}
int main()
{
int t;
cin>>t;
init();
while(t--){
cin>>len1>>s1+1;
cin>>len2>>s2+1;
solve();
}
return 0;
}