POJ1080 Human Gene Functions 动态规划 LCS的变形

题意读了半年,唉,给你两串字符,然后长度不同,你可以用‘-’把它们补成相同长度,补在哪里取决于得分,它会给你一个得分表,问你最大得分

跟LCS很像的DP数组 dp[i][j]表示第一个字符串取第i个元素第二个字符串取第三个元素,然后再预处理一个得分表加上即可

得分表:

score['A']['A'] = score['C']['C'] = score['G']['G'] = score['T']['T'] = 5;

	score['A']['C'] = score['C']['A'] = -1;
	score['A']['G'] = score['G']['A'] = -2;
	score['A']['T'] = score['T']['A'] = -1;
	score['A']['-'] = score['-']['A'] = -3;
	score['C']['G'] = score['G']['C'] = -3;
	score['C']['T'] = score['T']['C'] = -2;
	score['C']['-'] = score['-']['C'] = -4;
	score['G']['T'] = score['T']['G'] = -2;
	score['G']['-'] = score['-']['G'] = -2;
	score['T']['-'] = score['-']['T'] = -1;

	score['-']['-'] = -inf;

那么DP方程就好推了:

dp[i][j] = :

dp[i-1][j] + score[s1[i-1]][‘-‘]或者

dp[i][j-1] + score[‘-‘][s2[j-1]]或者

dp[i-1][j-1] + score[s1[i-1]][s2[j-1]]或者

这三者之中取最大的

然后就是边界问题我给忘记了

不够细心,若单单是i==0或者j==0,边界问题就出现了,边界不可能为0的,所以还得处理一下边界

#include<iostream>
#include<cstdio>
#include<list>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<cmath>
#include<memory.h>
#include<set>
#include<cctype>

#define ll long long

#define LL __int64

#define eps 1e-8

#define inf 0xfffffff

//const LL INF = 1LL<<61;

using namespace std;

//vector<pair<int,int> > G;
//typedef pair<int,int > P;
//vector<pair<int,int> > ::iterator iter;
//
//map<ll,int >mp;
//map<ll,int >::iterator p;

const int N = 1000 + 5;

int dp[N][N];
int score[200][200];

void init() {
	score['A']['A'] = score['C']['C'] = score['G']['G'] = score['T']['T'] = 5;

	score['A']['C'] = score['C']['A'] = -1;
	score['A']['G'] = score['G']['A'] = -2;
	score['A']['T'] = score['T']['A'] = -1;
	score['A']['-'] = score['-']['A'] = -3;
	score['C']['G'] = score['G']['C'] = -3;
	score['C']['T'] = score['T']['C'] = -2;
	score['C']['-'] = score['-']['C'] = -4;
	score['G']['T'] = score['T']['G'] = -2;
	score['G']['-'] = score['-']['G'] = -2;
	score['T']['-'] = score['-']['T'] = -1;

	score['-']['-'] = -inf;
}

int main () {
	init();
	int t;
	char s1[N];
	char s2[N];
	scanf("%d",&t);
	while(t--) {
		int n,m;
		memset(dp,0,sizeof(dp));
		scanf("%d %s",&n,s1);
		scanf("%d %s",&m,s2);
		for(int i=1;i<=n;i++)
			dp[i][0] = dp[i-1][0] + score[s1[i-1]]['-'];//边界处理
		for(int j=1;j<=m;j++)
			dp[0][j] = dp[0][j-1] + score['-'][s2[j-1]];//边界处理
		for(int i=1;i<=n;i++) {
			for(int j=1;j<=m;j++) {
				int t1 = dp[i-1][j] + score[s1[i-1]]['-'];
				int t2 = dp[i][j-1] + score['-'][s2[j-1]];
				int t3 = dp[i-1][j-1] + score[s1[i-1]][s2[j-1]];
				int maxn = max(t1,t2);
				dp[i][j] = max(maxn,t3);
			}
		}
		printf("%d\n",dp[n][m]);
	}
	return 0;
}

POJ1080 Human Gene Functions 动态规划 LCS的变形,布布扣,bubuko.com

时间: 2024-10-10 00:53:38

POJ1080 Human Gene Functions 动态规划 LCS的变形的相关文章

POJ 1080 Human Gene Functions(动态规划)

一开始用的DFS,无限TLE,贴丑代码 //version 1 TLE #include<cstdio> #include<cstring> #include<iostream> #define MAX_INT 2147483647 #define MAXN 105 using namespace std; int Map[5][5] = { {0,-3,-4,-2,-1}, {-3,5,-1,-2,-1}, {-4,-1,5,-3,-2}, {-2,-2,-3,5,-

POJ 1080 Human Gene Functions(LCS)

Description It is well known that a human gene can be considered as a sequence, consisting of four nucleotides, which are simply denoted by four letters, A, C, G, and T. Biologists have been interested in identifying human genes and determining their

poj 1080 Human Gene Functions(lcs,较难)

Human Gene Functions Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19573   Accepted: 10919 Description It is well known that a human gene can be considered as a sequence, consisting of four nucleotides, which are simply denoted by four

hdoj-1503-Human Gene Functions【LCS的变形】

Human Gene Functions Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2723 Accepted Submission(s): 1538 Problem Description It is well known that a human gene can be considered as a sequence, consi

poj1080 - Human Gene Functions (dp)

题面 It is well known that a human gene can be considered as a sequence, consisting of four nucleotides, which are simply denoted by four letters, A, C, G, and T. Biologists have been interested in identifying human genes and determining their function

POJ - 1080 - Human Gene Functions (LCS的变形)

题目传送:Human Gene Functions 思路:LCS的变形,定义状态dp[ i ][ j ]为取字符串s前i个字符字符串t前j个字符所获得的最大值,则可以得到状态转移方程为: dp[ i ][ j ] = max(dp[ i ][ j - 1] + f[ ' - ' ][ t[ j ] ], dp[ i - 1 ][ j ] + f[ s [ i ] ][ ' - ' ], dp[i - 1][ j - 1] + f[ s [ i ] ][ t [ j ] ]); AC代码: #in

POJ 1080 Human Gene Functions(求两字符串相似度:LCS变形)

POJ 1080 Human Gene Functions(求两字符串相似度:LCS变形) http://poj.org/problem?id=1080 题意: HDU1080 给你两个由字符A,C,G,T构造的字符串s1和s2, 现在你可以在这两个字符串中插入空格, 使得两串长相等(但是不能使得s1的空格对应s2的空格位置). 然后给你s1的特定字符对应s2中特定字符所能获得的分数矩阵: 问你最后两个字符串所能获得的最大分数是多少? 分析: 本题很类似于求字符串最短编辑距离或者求字符串LCS的

poj1080--Human Gene Functions(dp:LCS变形)

Human Gene Functions Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17206   Accepted: 9568 Description It is well known that a human gene can be considered as a sequence, consisting of four nucleotides, which are simply denoted by four

hdu 1080 Human Gene Functions (动态规划)

Human Gene Functions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2402    Accepted Submission(s): 1360 Problem Description It is well known that a human gene can be considered as a sequence,