(LCS)poj 1080 Human Gene Functions

题目
poj1080

题意:
给出两个字符串,求它们匹配成功后(两个字符串长度要相等,不相等的话可以在较短的字符串加字符 ’-‘ 增长)的最大得分。
得 分 = ∑ i = 0 s t r 1. l e n g h t ( ) − 1 ∑ j = 0 s t r 2. l e n g h t ( ) − 1 c n t ( i , j ) 得分 = \sum_{i=0}^{str1.lenght()-1} \sum_{j=0}^{str2.lenght()-1} cnt(i, j) =i=0str1.lenght()1j=0str2.lenght()1cnt(i,j)
cnt[ ][ ]:
在这里插入图片描述
思路:
Needleman - Wunsch算法
dp过程:
在这里插入图片描述
代码

#include <iostream>
#include <algorithm>
#include <map>
#define DEBUG freopen("_in.txt", "r", stdin); freopen("_out1.txt", "w", stdout);
#define CASE int t; cin >> t; while (t--)
using namespace std;
const int MAXN = 1e2 + 10;
char a[MAXN], b[MAXN];
int dp[MAXN][MAXN];
map<char,int> m;
int cnt[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, 0}};
void init(){
	m['A'] = 0;
	m['C'] = 1;
	m['G'] = 2;
	m['T'] = 3;
	m['-'] = 4;
}
void solve(){
	int lena, lenb;
	scanf("%d %s", &lena, a);
	scanf("%d %s", &lenb, b);
	for (int i = 0; i <= lena; i++)
		for (int j = 0; j <= lenb; j++)
			if (i == 0 && j == 0)
				dp[i][j] = 0;
			else if (i == 0)
				dp[i][j] = dp[i][j-1] + cnt[m['-']][m[b[j-1]]];
			else if (j == 0)
				dp[i][j] = dp[i-1][j] + cnt[m[a[i-1]]][m['-']];
			else
				dp[i][j] = max(dp[i-1][j-1]+cnt[m[a[i-1]]][m[b[j-1]]], max(dp[i][j-1]+cnt[m['-']][m[b[j-1]]], dp[i-1][j]+cnt[m[a[i-1]]][m['-']]));
	printf("%d\n", dp[lena][lenb]);
} 
int main(){
	init();
	CASE solve();
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值