POJ 1080--Human Gene Function

本题DP方案类似于LCS的DP设计。

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

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

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

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

  1. 若i = 0,dp[0][j] = sum{score[s2[k]][4],1 <= k <= j},其中score数组为相应字符得分。同理可求得dp[i][0]。
  2. 若s1[i] = s2[j],易知dp[i][j] = dp[i-1][j-1]+1。
  3. 若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;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值