Finding Longest Subsequence
"Nice One Program"
In this program we will have 2 Strings S1,S2 and Will Find The largest subsequence In both of These
#include<iostream>
#include<string.h>
using namespace std;
int lcs(char*,char*,int,int);
int max(int a,int b)
{
return((a>b)?a:b);
}
int main()
{ char s1[50],s2[50];
int m,n;
cout<<"Enter String 1:";
cin>>s1;
cout<<"Enter String 2:";
cin>>s2;
m=strlen(s1);
n=strlen(s2);
int x=lcs(s1,s2,m,n);
cout<<"Lngest SUBSEQ: "<<x;
return 0;
}
lcs(char* s1,char* s2,int m,int n)
{
if(m==0||n==0)
return 0;
else if(s1[m-1]==s2[n-1])
return 1+lcs(s1,s2,m-1,n-1);
else
return(max(lcs(s1,s2,m,n-1),lcs(s1,s2,m-1,n)));
}
No comments:
Post a Comment