-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLCS.cpp
83 lines (71 loc) · 1.49 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s1,s2;
cin >> s1 >> s2;
int n = s1.size();
int m = s2.size();
int LCS_table[n+1][m+1];
char tracker[n+1][m+1];
for(int i =0; i<=n; i++)
{
LCS_table[i][0]=0;
}
for(int i =0; i<=m; i++)
{
LCS_table[0][i]=0;
}
for(int i =1; i<=n; i++)
{
for(int j = 1; j<=m; j++)
{
if(s1[i-1]==s2[j-1])
{
LCS_table[i][j] = LCS_table[i-1][j-1]+1;
tracker[i][j] = 'D';
}
else if( LCS_table[i-1][j] >= LCS_table[i][j-1])
{
LCS_table[i][j] = LCS_table[i-1][j] ;
tracker[i][j] = 'U';
}
else
{
LCS_table[i][j] = LCS_table[i][j-1];
tracker[i][j] = 'L';
}
}
}
cout << "Longest Common Subsequence: " << LCS_table[n][m] << endl;
string ans;
int i = n;
int j = m;
while(i>0 && j>0)
{
if(tracker[i][j]=='U')
{
i--;
}
else if(tracker[i][j]=='L')
{
j--;
}
else
{
ans+=s1[i-1];
i--;
j--;
}
}
reverse(ans.begin(), ans.end());
cout << ans << endl;
return 0;
}
/*
Input Value:
TCAGGTAT
ACTAGCTA
LCS = 5;
Subsequence: TAGTA
*/