• COGS 445. [HAOI2010]最长公共子序列


    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #define mod 100000000
    #define maxn 5010
    using namespace std;
    int l1,l2,f[maxn][maxn],s[maxn][maxn];
    char s1[maxn],s2[maxn];
    int main()
    {
        freopen("lcs.in","r",stdin);
        freopen("lcs.out","w",stdout);
        scanf("%s%s",s1+1,s2+1);
        l1=strlen(s1+1);l1--;
        l2=strlen(s2+1);l2--;
        for(int i=0;i<=l1;i++)s[i][0]=1;
        for(int i=1;i<=l2;i++)s[0][i]=1;
        for(int i=1;i<=l1;i++)
            for(int j=1;j<=l2;j++){
                if(s1[i]==s2[j]){
                    f[i][j]=f[i-1][j-1]+1;
                    s[i][j]=s[i-1][j-1];
                }
                else f[i][j]=max(f[i][j-1],f[i-1][j]);
                if(f[i][j]==f[i-1][j])s[i][j]+=s[i-1][j];
                if(f[i][j]==f[i][j-1])s[i][j]+=s[i][j-1];
                if(f[i][j]==f[i-1][j-1]&&s1[i]!=s2[j])
                    s[i][j]-=s[i-1][j-1];
                s[i][j]%=mod;
            }
        printf("%d
    %d
    ",f[l1][l2],s[l1][l2]);
        return 0;
    }
  • 相关阅读:
    tuple-1
    禅语-1
    综述的写作技巧-1
    皆大欢喜组合
    类和对象-3
    双棍练习
    CodeBlocks开发环境使用-1
    类和对象-2
    类和对象-1
    13-归并排序-分治策略应用于排序
  • 原文地址:https://www.cnblogs.com/yanlifneg/p/5854749.html
Copyright © 2020-2023  润新知