这题我用记忆化搜索做的。。。。
直接递推上去的:点击打开链接
这题的方法就是
dp[i][j]代表a的前i个字符与b的前j个字符所能获得的最大的匹配值
然后转移就是
ans = max( ans, score[num1[pos1]][num2[pos2]] + DFS( pos1 - 1, pos2 - 1 ) );//a[i]与b[j]匹配
ans = max( ans, score[4][num2[pos2]] + DFS( pos1, pos2 - 1 ) );//a[i]与‘-’匹配
ans = max( ans, score[num1[pos1]][4] + DFS( pos1 - 1, pos2 ) );//b[i]与‘-’匹配
AC代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAX 0x3f3f3f3f
int score[][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, -MAX },
};//A C G T -
int dp[110][110];
int num1[110], num2[110], length1, length2;
int in_data(){
char temp[110];
scanf( "%d", &length1 );
scanf( "%s", temp );
for( int i = 0; i < length1; i++ ){
if( temp[i] == 'A' ){
num1[i] = 0;
}else if( temp[i] == 'C' ){
num1[i] = 1;
}else if( temp[i] == 'G' ){
num1[i] = 2;
}else if( temp[i] == 'T' ){
num1[i] = 3;
}
}
scanf( "%d", &length2 );
scanf( "%s", temp );
for( int i = 0; i < length2; i++ ){
if( temp[i] == 'A' ){
num2[i] = 0;
}else if( temp[i] == 'C' ){
num2[i] = 1;
}else if( temp[i] == 'G' ){
num2[i] = 2;
}else if( temp[i] == 'T' ){
num2[i] = 3;
}
}
return 0;
}
int initial(){
for( int i = 0; i < length1; i++ ){
for( int j = 0; j < length2; j++ ){
dp[i][j] = -MAX;
}
}
return 0;
}
int DFS( int pos1, int pos2 ){
if( pos1 == -1 ){
if( pos2 == -1 ){
return 0;
}else if( pos2 == 0 ){
return score[4][num2[0]];
}else{
return score[4][num2[pos2]] + DFS( pos1, pos2 - 1 );
}
}
if( pos2 == -1 ){
if( pos1 == -1 ){
return 0;
}else if( pos1 == 0 ){
return score[num1[0]][4];
}else{
return score[num1[pos1]][4] + DFS( pos1 - 1, pos2 );
}
}
if( dp[pos1][pos2] != -MAX ){
return dp[pos1][pos2];
}
int ans = -MAX;
ans = max( ans, score[num1[pos1]][num2[pos2]] + DFS( pos1 - 1, pos2 - 1 ) );
ans = max( ans, score[4][num2[pos2]] + DFS( pos1, pos2 - 1 ) );
ans = max( ans, score[num1[pos1]][4] + DFS( pos1 - 1, pos2 ) );
return dp[pos1][pos2] = ans;
}
int main(){
int T;
scanf( "%d", &T );
while( T-- ){
in_data();
initial();
cout << DFS( length1 - 1, length2 - 1 ) << endl;
}
return 0;
}