-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlcs.cpp
More file actions
55 lines (51 loc) · 933 Bytes
/
lcs.cpp
File metadata and controls
55 lines (51 loc) · 933 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
46
47
48
49
50
51
52
53
54
55
/**
* Solution to longest common subsequence problem using memoization
*/
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int memo[100][100];
string A,B,C;
int LCS(int a,int b)
{
if(a==A.size() || b==B.size()) return 0;
int& res=memo[a][b];
if(res!=-1) return res;
res=0;
if(A[a]==B[b])
res=max(res,1+LCS(a+1,b+1));
else
res=max(LCS(a+1,b),LCS(a,b+1));
return res;
}
void construct(int a,int b)
{
if(a==A.size() || b==B.size()) return;
if(A[a]==B[b]){
C+=A[a];
construct(a+1,b+1);
return;
}
if(LCS(a,b+1)>LCS(a+1,b))
construct(a,b+1);
else
construct(a+1,b);
}
int main()
{
while(cin>>A>>B)
{
C.clear();
memset(memo,-1,sizeof(memo));
int LEN=LCS(0,0);
cout<<"Length: "<<LEN<<endl;
if(LEN>0){
cout<<"LCS Length: "<<LEN<<endl;
construct(0,0);
cout<<C<<endl;
}
}
return 0;
}