题目链接:http://acm.hit.edu.cn/hoj/problem/view?id=1316
注意此DP方程:
设两基因串为an,bm
a[i],b[j]分别表示a串的第i个核苷
b串的第j个核苷
ax,by为an,bm的子串;
c[x][y]表示子串ax,by间最大相似程度值
则c[n][m]表示串an,bm间最大相似程度值
value(x,y)表示核苷x核苷y的相似程度值
'-'表示核苷为空
则有如下的状态转移方程:
1.c[i][j]=max(c[i-1][j-1]+value(a[i],b[j]),c[i][j-1]+value('-',b[j]),c[i-1][j]+value(a[i],'-'))
if i,j>0
2.c[i][0] = c[i-1][0] + value[i]['-']
3.c[0][i] = c[0][i-1] + value['-'][i]
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std;
char temp[1000];
char a[1000];
char b[1000];
int c[1000][1000];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int t;
int la,lb;
scanf("%d",&t);
int value[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
};
while(t--)
{
scanf(" %d %s",&la,temp);
for(int i=0;i<la;i++)
{
if(temp[i] == 'A')
{
a[i] = 1;
}
else if(temp[i] == 'C')
{
a[i] = 2;
}
else if(temp[i] == 'G')
{
a[i] = 3;
}
else if(temp[i] == 'T')
{
a[i] = 4;
}
}
scanf(" %d %s",&lb,temp);
for(int i=0;i<lb;i++)
{
if(temp[i] == 'A')
{
b[i] = 1;
}
else if(temp[i] == 'C')
{
b[i] = 2;
}
else if(temp[i] == 'G')
{
b[i] = 3;
}
else if(temp[i] == 'T')
{
b[i] = 4;
}
}
memset(c,0,sizeof(c));
for(int i=1;i<=lb;i++)
{
c[0][i] = c[0][i-1] + value[5][b[i-1]];
}
for(int i=1;i<=la;i++)
{
c[i][0] = c[i-1][0] + value[a[i-1]][5];
}
for(int i=1;i<=la;i++)
{
for(int j=1;j<=lb;j++)
{
c[i][j] = max(c[i-1][j-1] + value[a[i-1]][b[j-1]],max(c[i][j-1] + value[5][b[j-1]],c[i-1][j] + value[a[i-1]][5]));
}
}
printf("%d\n",c[la][lb]);
}
return 0;
}