本题收获:
1 利用字符ascii值来替代数组下标,使代码更清晰
2 注意dp数组初始化时不是统一初始化为0,是依次错开所有位置(与-对应的情况)而且对于这些情况需要累加
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
#define MAX_L 100
char ca[MAX_L+10], cb[MAX_L+10];
string a, b;
int dp[MAX_L+MAX_L+10][MAX_L+MAX_L+10], score[MAX_L+100][MAX_L+100];//ascii z = 90 enough
int T;
int main()
{
score['A']['A'] = 5;
score['A']['C'] = -1;
score['A']['G'] = -2;
score['A']['T'] = -1;
score['A']['-'] = -3; //注意这种技巧
score['C']['A'] = -1;
score['C']['C'] = 5;
score['C']['G'] = -3;
score['C']['T'] = -2;
score['C']['-'] = -4;
score['G']['A'] = -2;
score['G']['C'] = -3;
score['G']['G'] = 5;
score['G']['T'] = -2;
score['G']['-'] = -2;
score['T']['A'] = -1;
score['T']['C'] = -2;
score['T']['G'] = -2;
score['T']['T'] = 5;
score['T']['-'] = -1;
score['-']['A'] = -3;
score['-']['C'] = -4;
score['-']['T'] = -2;
score['-']['G'] = -1;
scanf("%d", &T);
while(T)
{
T--;
int lena, lenb;
cin >> lena >> a;
/*for(int i = 1;i <= lena;i++)
ca[i] = a[i-1];*/
cin >> lenb >> b;
/*for(int i = 1;i <= lenb;i++)
cb[i] = b[i-1];*/
memset(dp, 0, sizeof(dp));
//initialization
for(int i = 1;i <= lena;i++)
dp[i][0] = dp[i-1][0] + score[a[i-1]]['-'];
for(int j = 1;j <= lenb;j++)
dp[0][j] = dp[0][j-1] + score['-'][b[j-1]];
//极限情况就是完全错开
for(int i = 1;i <= lena;i++)
for(int j = 1;j <= lenb;j++)
{
if(dp[i-1][j-1]+score[a[i-1]][b[j-1]] > dp[i][j-1]+score[a[i-1]]['-'])
{
if(dp[i-1][j-1]+score[a[i-1]][b[j-1]] > dp[i-1][j]+score['-'][b[j-1]])
dp[i][j] = dp[i-1][j-1]+score[a[i-1]][b[j-1]];
else
dp[i][j] = dp[i-1][j]+score['-'][b[j-1]];
}
else
{
if(dp[i][j-1]+score[a[i-1]]['-'] < dp[i-1][j]+score['-'][b[j-1]])
dp[i][j] =dp[i-1][j]+score['-'][b[j-1]];
else
dp[i][j] = dp[i][j-1]+score[a[i-1]]['-'];
}
//dp[i][j] = max(dp[i-1][j-1]+score[a[i-1]][b[j-1]], max(dp[i-1][j]+score['-'][b[j-1]], dp[i][j-1]+score[a[i-1]]['-']));
//printf("%d ", dp[i][j]);
}
printf("\n");
printf("%d\n", dp[lena][lenb]);
}
return 0;
}