您的位置:首页 > 其它

Poj 1080 Human Gene Functions

2012-01-19 14:16 260 查看


题目大意:已知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]
;
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: