forked from somiljain7/data-structure
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargestCommonSubsequence.cpp
More file actions
Latest commit
45 lines (41 loc) · 833 Bytes
/
Copy pathLargestCommonSubsequence.cpp
File metadata and controls
45 lines (41 loc) · 833 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<iostream>
#include<string.h>
usingnamespacestd;
intlcs(int m, int n, char a, char b){
int C[m+1][n+1];
int i,j;
for(i=0;i<=m;i++){
for(j=0;j<=n;j++){
if(i==0 || j==0){
C[i][j]=0;
}
elseif(a[i-1]==b[j-1]){
C[i][j]=C[i-1][j-1]+1;
}
else
C[i][j]=max(C[i-1][j],C[i][j-1]);
}
}
return C[m][n];
}
intmax(int m1, int m2){
int largest=0;
if(m1>m2)
largest=m1;
else
largest=m2;
return largest;
}
intmain()
{
char a[20];
char b[20];
cout<<"\n enter first string : ";
cin>>a;
cout<<"\n enter second string: ";
cin>>b;
int m=strlen(a);
int n=strlen(b);
cout<<"\n Length of LCS = "<<lcs(m,n,a,b);
return0;
}