hdu 1243 反恐训练营 最长公共字序列

此题的题意很明确,就是求最长公共子序列;

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int dp[2010][2010];
int hash[255];
int main()
{
    char str[2010],ter[2010],fir[2010];
    int n,i,j;
    while(scanf("%d",&n)!=EOF)
    {
        memset(hash,0,sizeof(hash));
        scanf("%s",&str);
        int x;
        for(i=0;i<n;i++)
        {
            scanf("%d",&x);
            hash[str[i]]=x;
        }
        scanf("%s%s",&ter,&fir);
        int l1,l2;
        l1=strlen(ter);
        l2=strlen(fir);
        int cir=max(l1,l2);
        for(i=0;i<=cir;i++)
        {
            dp[0][i]=0;
            dp[i][0]=0;
        }
        for(i=1;i<=l1;i++)
        {
            for(j=1;j<=l2;j++)
            {
                if(ter[i-1]==fir[j-1])
                {
                    dp[i][j]=dp[i-1][j-1]+hash[ter[i-1]];
                }
                else
                    dp[i][j]=max(dp[i][j-1],dp[i-1][j]);
            }
        }
        int Max=0;
        Max=dp[l1][l2];
        printf("%d\n",Max);
    }
    return 0;
}

 

posted @ 2013-06-17 18:22  fangguo  阅读(217)  评论(0编辑  收藏  举报