-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlCS.cpp
63 lines (63 loc) · 1.17 KB
/
lCS.cpp
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include<bits/stdc++.h>
using namespace std;
int main()
{
vector <char> v;
string a,b;
cout<<"Enter 1st String :"<<endl;
cin>>a;
cout<<"Enter 2nd string:"<<endl;
cin>>b;
int la = a.length() +1;
int lb = b.length() +1;
int s[la][lb];
for(int i=0;i<la;i++)
{
s[i][0]=0;
}
for(int i=0;i<lb;i++)
{
s[0][i]=0;
}
for(int i=1;i<la;i++)
{
for(int j=1;j<lb;j++)
{
if(a[i-1]!=b[j-1])
{
s[i][j]=max(s[i-1][j],s[i][j-1]);
}
else
{
s[i][j]=s[i-1][j-1]+1;
}
}
}
cout<<"minimum length of lcs ="<<s[la-1][lb-1]<<endl;
int x,y;
x=la-1;
y=lb-1;
while(s[x][y]>0)
{
if(s[x][y]==s[x][y-1])
{
y--;
}
else if(s[x][y]==s[x-1][y])
{
x--;
}
else
{
x--;
y--;
v.push_back(a[x]);
}
}
reverse(v.begin(),v.end());
for(int i=0;i<v.size();i++)
{
cout<<v[i]<<" ";
}
return 0;
}