-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongest_common_subsequence.cpp
More file actions
68 lines (46 loc) · 1.18 KB
/
longest_common_subsequence.cpp
File metadata and controls
68 lines (46 loc) · 1.18 KB
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
#include<bits/stdc++.h>
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
int max(int a, int b) {
return a > b ? a : b;
}
int LCS[100][100];
string LCSPrint(string S, string T, int m, int n)
{
if (m == 0 || n == 0) {
return string("");
}
if (S[m - 1] == T[n - 1])
{
return LCSPrint(S, T, m - 1, n - 1) + S[m - 1];
}
if (LCS[m - 1][n] > LCS[m][n - 1]) {
return LCSPrint(S, T, m - 1, n);
}
else {
return LCSPrint(S, T, m, n - 1);
}
}
string LCSAction(string S, string T) {
int m = S.length();
int n = T.length();
// from S 0 -> m
for(int i = 1; i <= m; ++i) {
// T 0 -> n
for(int j = 1 ; j <= n; ++j) {
// if chars are equal
if(S[i-1] == T[j-1]) {
// 1 + prev diagonal element
LCS[i][j] = 1 + LCS[i-1][j-1];
}
else {
// else find the max between up and left
LCS[i][j] = max(LCS[i-1][j], LCS[i][j-1]);
}
}
}
return LCSPrint(S,T,m,n);
}
int main() {
cout << LCSAction("XMJYAUZ", "MZJAWXU");
}