-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlongest_common_subsequence.cpp
More file actions
56 lines (46 loc) · 1.05 KB
/
longest_common_subsequence.cpp
File metadata and controls
56 lines (46 loc) · 1.05 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
// BOJ 9252 LCS 2
#include <bits/stdc++.h>
#define sz size()
#define bk back()
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
string lcs(string &x, string &y) {
int n = x.sz;
int m = y.sz;
vector<vector<int>> dp(n + 1, vector<int>(m + 1));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (x[i - 1] == y[j - 1])
dp[i][j] = dp[i - 1][j - 1] + 1;
else
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
string ret;
int i = n;
int j = m;
while (i > 0 && j > 0) {
if (x[i - 1] == y[j - 1]) {
ret += x[i - 1];
i--;
j--;
} else if (dp[i][j] == dp[i - 1][j])
i--;
else
j--;
}
reverse(ret.begin(), ret.end());
return ret;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string x, y;
cin >> x >> y;
string ans = lcs(x, y);
cout << ans.sz << '\n' << ans;
}