Subscribe Us

Responsive Advertisement

Advertisement

26. Longest Common Substring

 #include<bits/stdc++.h>

using namespace std;
int lcs(string &str1, string &str2)
{
    // Write your code here.
  int n,m,i,j;
    n=str1.size();
    m=str2.size();
    str1="#"+str1;
    str2="#"+str2;
    int dp[n+1][m+1];
    memset(dp,0,sizeof(dp));
    int ans=0;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=m;j++)
        {
            if(str1[i]==str2[j])
            {
                dp[i][j]=dp[i-1][j-1]+1;
            }
            ans=max(ans,dp[i][j]);
        }
        
    }
    return ans;

}

Post a Comment

0 Comments