POJ 1080.Human Gene Functions

题目:http://poj.org/problem?id=1080

AC代码(C++):

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <vector>
#include <queue>
#include <math.h>
#include <string>
#include <string.h>
#include <bitset>

#define INF 0xfffffff
#define MAXN 100105

using namespace std;

int n,m;
char str1[105];
char str2[105];
int dp[105][105];
int score[100][100];

int max3(int a, int b, int c){
	int tmp = a>b?a:b;
	return c>tmp?c:tmp;
}

int main(){
	int t;
	cin>>t;
	
	score['A']['A'] = 5;
	score['C']['C'] = 5;
	score['G']['G'] = 5;
	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);
	
	while(t--){
		cin>>n>>str1>>m>>str2;
		dp[0][0] = 0;
		for(int i = 1; i <= n; i++)dp[i][0] = dp[i-1][0] + score[str1[i-1]]['-'];
		for(int i = 1; i <= m; i++)dp[0][i] = dp[0][i-1] + score['-'][str2[i-1]];
		for(int i = 1; i <= n; i++){
			for(int j = 1; j <= m; j++){
				dp[i][j] = max3(dp[i-1][j-1]+score[str1[i-1]][str2[j-1]],dp[i-1][j] + score[str1[i-1]]['-'],dp[i][j-1] + score['-'][str2[j-1]]);
			}
		}
		cout<<dp[n][m]<<endl;
	}
}
总结: 动态规划, 最长公共子序列的变形. 这题的关键是在dp数组下移和右移的时候应该怎么加分数. 首先把行看做str1, 把列看做str2, 则下移代表在str2中插入-号来与str1匹配. 原因是如果下移的话, 则说明str1前进一步, 并且前进后与str2不匹配, 则需在str2中插入-号来与str1匹配. 右移同理.

如上图所示, 当dp从[4,5]下移到[5,5]时, 说明第5行的D与str2不匹配, 则往str2的BA(5列和6列)间插入-号来与之匹配. 右移也是一样的. 想通了这点之后这题就很简单了.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值