Poj 1080 Human Gene Functions

题目大意:已知ATCG四种不同的碱基之间的相似度。现在给出两个碱基序列,可以在两个串间插入‘-’即空白,求两串的最大相似度。

思路:有点类似于LCS,状态转移方程为dp[i][j]=getMax(dp[(i-1)][j-1]+table[str1[i]][str2[j]],dp[(i-1)][j]+table[str1[i]][4],dp[i][j-1]+table[4][str2[j]])。

#include <stdio.h>
#include <string.h>
#include <memory.h>
int table[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};
int dp[105][105];
int str1[105];
int str2[105];
int m,n;
int change(char c) {
	if (c=='A')
		return 0;
	else if (c=='C')
		return 1;
	else if (c=='G')
		return 2;
	else if (c=='T')
		return 3;
	else 
		return 4;
}
int getMax(int a,int b,int c) {
	int temp;
	if (a>b)
		temp=a;
	else 
		temp=b;
	if (c>temp)
		temp=c;
	return temp;
}
int solve_dp() {
	int i,j,ctr;

	dp[0][0]=0;
	for (i=1;i<=m;i++)
		dp[i][0]=dp[i-1][0]+table[str1[i]][4];
	for (i=1;i<=n;i++)
		dp[0][i]=dp[0][i-1]+table[4][str2[i]];
	for (i=1;i<=m;i++) {
		for (j=1;j<=n;j++) {
			dp[i][j]=getMax(dp[(i-1)][j-1]+table[str1[i]][str2[j]],dp[(i-1)][j]+table[str1[i]][4],dp[i][j-1]+table[4][str2[j]]);
		}
	}
	ctr=dp[m][n];
	return ctr;
}
int main()
{
	int t,i,j;
	int result;
	char temp;

	scanf("%d",&t);
	while (t--) {
		scanf("%d",&m);
		getchar();
		for (i=1;i<=m;i++) {
			scanf("%c",&temp);
			str1[i]=change(temp);
		}
		scanf("%d",&n);
		getchar();
		for (i=1;i<=n;i++) {
			scanf("%c",&temp);
			str2[i]=change(temp);
		}
		result=solve_dp();
		printf("%d\n",result);
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值