-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCSstring_1recursive.cpp
More file actions
42 lines (37 loc) · 964 Bytes
/
LCSstring_1recursive.cpp
File metadata and controls
42 lines (37 loc) · 964 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
#include<iostream>
using namespace std;
int LCS(string x, string y, int m, int n, int count)
{
cout<<"X length "<<m<<endl;
cout<<"y length "<<n<<endl;
cout<<"Count of same letters in continuity "<<count<<endl;
if(n==0 || m==0)
{
cout<<"string empty "<<endl;
return count;
}
if(x[m-1]==y[n-1])
{
count++;
cout<<"Count after the letter matched "<<count<<endl;
return LCS(x,y, m-1, n-1,count);
}
else
{
int l1 = LCS(x,y,m-1,n,0);
int l2 = LCS(x,y,m,n-1,0);
cout<<"length left side "<<l1<<" length right side "<<l2<<endl;
cout<<"count previous "<<count<<endl;
cout<<"max length "<<max(count, max(l1,l2))<<endl;
return max(count, max(l1,l2));
}
}
int main()
{
string x = "abdfl";
string y = "abedfl";
int m = x.length();
int n = y.length();
int length = LCS(x,y,m,n,0);
cout<<"Length "<<length<<endl;
}