首页 > 代码库 > LCS最长公共子串

LCS最长公共子串

http://cogs.pro/cogs/problem/problem.php?pid=476

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int dp[5050][5050]; char a[5050],b[5050];int main(){    freopen("lcslength.in","r",stdin);    freopen("lcslength.out","w",stdout);    int l1,l2,i,j;    scanf("%s%s",a+1,b+1);    l1=strlen(a+1);l2=strlen(b+1);    for(i=1;i<l1;i++){        for(j=1;j<l2;j++){            if(a[i]==b[j])dp[i][j]=dp[i-1][j-1]+1;            else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);        }    }    printf("%d",dp[l1-1][l2-1]);    return 0;}

技术分享

 

LCS最长公共子串