首页 > 代码库 > poj 1080 Human Gene Functions_简单dp

poj 1080 Human Gene Functions_简单dp

题目链接

题意:给你两个串,再给你一个表,按那个表求两串最大值

思路:

       dp[i][j]=max(dp[i-1][j-1]+map[a[i-1]][b[j-1]],dp[i-1][j]+map[a[i-1]][‘-‘],dp[i][j-1]+map[‘-‘][b[j-1]]);


#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define MAXN 110
char a[MAXN],b[MAXN];
int u[5][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,10}};
int dp[MAXN][MAXN];
int max(int a,int b,int c){
	a=a>b?a:b;
	return a>c?a:c;
}
int sv(char tmp){
	switch(tmp){
		case ‘A‘:return 0;break;
		case ‘C‘:return 1;break;
		case ‘G‘:return 2;break;
		case ‘T‘:return 3;break;
		case ‘-‘:return 4;break;
	}
	return 5;
}
int main(int argc, char** argv) {
	int t,i,j,lena,lenb;
	scanf("%d",&t);
	while(t--){
		scanf("%d %s",&lena,a);
		scanf("%d %s",&lenb,b);
		memset(dp,0,sizeof(dp));
		for(i=1;i<=lena;i++)
			dp[i][0]=dp[i-1][0]+u[sv(‘-‘)][sv(a[i-1])];
		for(i=1;i<=lenb;i++)
			dp[0][i]=dp[0][i-1]+u[sv(b[i-1])][sv(‘-‘)];
		for(i=1;i<=lena;i++)
			for(j=1;j<=lenb;j++)
				dp[i][j]=max(dp[i-1][j-1]+u[sv(b[j-1])][sv(a[i-1])],dp[i][j-1]+u[sv(b[j-1])][sv(‘-‘)],dp[i-1][j]+u[sv(‘-‘)][sv(a[i-1])]);
		printf("%d\n",dp[lena][lenb]);
	}
	return 0;
}