LONGEST  COMMON SUBSEQUENCE




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

using namespace std;

int main()
{
char a[100],b[100],i,j,l1,l2;
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;

  else
  dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
 }
}

cout<<"LCS is "<<dp[l1][l2]<<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.