首页 > 代码库 > COGS 445. [HAOI2010]最长公共子序列

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

#include<iostream>#include<cstdio>#include<cstring>#define mod 100000000#define maxn 5010using 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\n%d\n",f[l1][l2],s[l1][l2]);    return 0;}

 

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