您的位置:首页 > 其它

POJ 1080--Human Gene Function

2014-08-05 19:45 260 查看
本题DP方案类似于LCS的DP设计。

考虑到两个长度分别为L1和L2的串s1,s2,匹配后的两串末尾字符只有三种可能:

(s1[L1],—),(s1[L1],s2[L2]),(—,s2[L2])。

设dp[i][j]为s1[i]之前的字符串和s2[j]之前的字符串所能匹配到的最大得分。

由定义及上面的结论很容易得出:

若i = 0,dp[0][j] = sum{score[s2[k]][4],1 <= k <= j},其中score数组为相应字符得分。同理可求得dp[i][0]。
若s1[i] = s2[j],易知dp[i][j] = dp[i-1][j-1]+1。
若s1[i] != s2[j],易知dp[i][j] = max{dp[i-1][j]+score[s1[i]][4],dp[i][j-1]+score[s2[j]][4],dp[i-1][j-1]+score[s1[i]][s2[j]]}。

#include<cstdio>
#include<cstring>
#define maxL 102

short _max(short x,short y,short z)
{
short maxVal = x;
if(maxVal < y)
maxVal = y;
if(maxVal < z)
maxVal = z;
return maxVal;
}

int encode(char c)
{
switch(c)
{
case 'A': return 0;
case 'C': return 1;
case 'G': return 2;
case 'T': return 3;
case '\n': return '\n';
default: return 0;
}
}

int strIn(int* L,char* s)
{
scanf("%d",L);
getchar();
for(int i = 0;i < *L;i++)
*(s++) = encode(getchar());
return 0;
}

int main()
{
int i,j;
int T;
int L1,L2;
char s1[maxL],s2[maxL];
short maxScore[maxL][maxL];
short score[4][5] = {{5,-1,-2,-1,-3},
{-1,5,-3,-2,-4},
{-2,-3,5,-2,-2},
{-1,-2,-2,5,-1}};
while(~scanf("%d",&T))
{
while(T--)
{
memset(maxScore,0,sizeof(maxScore));
strIn(&L1,s1+1);
strIn(&L2,s2+1);
for(i = 1;i <= L1;i++)
maxScore[i][0] = maxScore[i-1][0]+score[s1[i]][4];
for(i = 1;i <= L2;i++)
maxScore[0][i] = maxScore[0][i-1]+score[s2[i]][4];
for(i = 1;i <= L1;i++)
{
for(j = 1;j <= L2;j++)
{
if(s1[i] == s2[j])
maxScore[i][j] = maxScore[i-1][j-1]+5;
else
maxScore[i][j] = _max(maxScore[i-1][j]+score[s1[i]][4],maxScore[i][j-1]+score[s2[j]][4],maxScore[i-1][j-1]+score[s1[i]][s2[j]]);
}
}
printf("%d\n",maxScore[L1][L2]);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: