-
Notifications
You must be signed in to change notification settings - Fork 1
/
LCS_.cpp
81 lines (70 loc) · 1.67 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
/* _
* _|_ o._ o._ __|_| _. _|_
* _>| ||| ||| |(_|| |(_|_>| |
* _|
* @author Amirul islam Al Mamun
* An Easy LCS LightOJ - 1110 >> DP
*/
#include <bits/stdc++.h>
using namespace std;
const int mx = 1e3;
int path[mx][mx], cost[mx][mx];
int lcs(string s1, string s2) {
for (int i = 1; i <= s1.length(); i++) {
for (int j = 1; j <= s2.length(); j++) {
// common word
if (s1[i-1] == s2[j-1]) {
cost[i][j] = cost[i-1][j-1] + 1;
path[i][j] = 1;
}
// UP greater
else if (cost[i-1][j] >= cost[i][j-1]) {
cost[i][j] = cost[i-1][j];
path[i][j] = 2;
}
// LEFT greater
else {
cost[i][j] = cost[i][j-1];
path[i][j] = 3;
}
}
}
return cost[s1.length()][s2.length()];
}
string get_lcs_string(string s1, string s2, int lcs_len) {
// no common substring
if (lcs_len == 0) {
return ":(";
}
int i = s1.length();
int j = s2.length();
int k = lcs_len;
stack <char> st;
while (k > 0) {
if (path[i][j] == 1) {
st.push(s1[i-1]);
i--;
j--;
k--;
}
else if (path[i][j] == 2) {
i--;
}
else {
j--;
}
}
string lcs_str = "";
while (!st.empty()) {
lcs_str.push_back((char) st.top());
st.pop();
}
return lcs_str;
}
int main() {
string s1, s2;
cin >> s1 >> s2;
int lcs_len = lcs(s1, s2);
cout << get_lcs_string(s1, s2, lcs_len) << endl;
return 0;
}