Description
Input
Output
Sample Input
2 7 AGTGATG 5 GTTAG 7 AGCTATT 9 AGCTTTAAA
Sample Output
14 21
Source
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int MAX = 1000; int ans[MAX][MAX],f[MAX]; char dp[MAX][MAX],s1[MAX],s2[MAX]; int t1[MAX],t2[MAX]; bool vis[MAX][MAX]; int map[][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,0}}; int dfs(int x,int y) { int xy = 0; if(x == -1){ for(int i = 0 ; i <= y;i++) xy += map[t2[i]][4]; return xy; } if(y == -1){ for(int i = 0 ; i <= x; i++) xy += map[4][t1[i]]; return xy; } if(vis[x][y]) return ans[x][y]; vis[x][y] = true; ans[x][y] = max(dfs(x-1,y-1)+map[t1[x]][t2[y]],max(dfs(x,y-1)+map[4][t2[y]],dfs(x-1,y)+map[t1[x]][4])); return ans[x][y]; } int main() { f[‘A‘] = 0; f[‘C‘] = 1; f[‘G‘] = 2; f[‘T‘] = 3; f[‘-‘] = 4; int T; int n1,n2; scanf("%d",&T); while(T--){ scanf("%d%s%d%s",&n1,s1,&n2,s2); for(int i = 0; i < n1;i++) t1[i] = f[s1[i]]; for(int i = 0 ; i < n2;i++) t2[i] = f[s2[i]]; memset(vis,0,sizeof(vis)); printf("%d\n",dfs(n1-1,n2-1)); } return 0; }
POJ1080——DP——Human Gene Functions
原文:http://www.cnblogs.com/zero-begin/p/4368547.html