LONGEST COMMON SUBSTRING




#include<iostream>
#include<stdio.h>
#include<bits/stdc++.h>

using namespace std;

int main()
{
char a[100],b[100];
int i,j,l1,l2,result=0;
cout<<"enter first string "<<endl;
scanf("%s",a);

cout<<"enter second string "<<endl;
scanf("%s",b);

int dp[100][100];
l1=strlen(a);
l2=strlen(b);

for(i=0;i<=l1;i++)
{
 for(j=0;j<=l2;j++)
 {
  if(i==0 || j==0)
  dp[i][j]=0;

  else if(a[i-1]==b[j-1])              // same same mtch pe diagonal  se 1 jyada
  {
  dp[i][j]=dp[i-1][j-1]+1;
  result=max(result,dp[i][j]);
  }

  else
  dp[i][j]=0;
 }
}

cout<<"LC substring  is "<<result<<endl;

return 0;
}

Comments

Popular posts from this blog

Getting Started With MEAN App Development with AngularJs , ExpressJs , NodeJs and MongoDB.

B. Dreamoon and WiFi :calculate no. of ways : recursive solution (branch and bound )

A. Dreamoon and Stairs : minimum steps to reach : recursion solution.