1 /*
2 * Human Gene Functions.cpp
3 *
4 * Created on: 2011-12-9
5 *
6 * 类似 最长公共子串
7 */
8
9 #include <cstdio>
10 using namespace std;
11
12 const int maxn = 100 + 5;
13
14 const int s[5][5] = {{5, -1, -2, -1, -3}, //分数矩阵
15 {-1, 5, -3, -2, -4},
16 {-2, -3, 5, -2, -2},
17 {-1, -2, -2, 5, -1},
18 {-3, -4, -2, -1, -100000000}};
19 int t[128]; //转换“函数”
20 int aLen, bLen;
21 char a[maxn], b[maxn];
22 int dp[maxn][maxn];
23
24 int inline max(const int a, const int b, const int c){
25 int tmp = (a > b ? a : b);
26 return (c > tmp ? c : tmp);
27 }
28
29 int main(){
30 t['A'] = 0;
31 t['C'] = 1;
32 t['G'] = 2;
33 t['T'] = 3;
34 t[' '] = 4;
35
36 int caseNum;
37 scanf("%d", &caseNum);
38 while(caseNum--){
39 scanf("%d%s", &aLen, a+1);
40 scanf("%d%s", &bLen, b+1);
41
42 dp[0][0] = 0;
43 for(int i=1; i<=aLen; i++) //注意初始化
44 dp[i][0] = dp[i-1][0] + s[t[a[i]]][t[' ']];
45 for(int i=1; i<=bLen; i++)
46 dp[0][i] = dp[0][i-1] + s[t[' ']][t[b[i]]];
47
48 //dp
49 for(int i=1; i<=aLen; i++){
50 for(int j=1; j<=bLen; j++){
51 dp[i][j] = max(dp[i-1][j-1]+s[t[a[i]]][t[b[j]]], dp[i-1][j]+s[t[a[i]]][t[' ']],
52 dp[i][j-1]+s[t[' ']][t[b[j]]]);
53 }
54 }
55
56 printf("%d\n", dp[aLen][bLen]);
57 }
58
59
60 return 0;
61 }