-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
39 lines (37 loc) · 988 Bytes
/
Copy pathmain.cpp
File metadata and controls
39 lines (37 loc) · 988 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
#include <bits/stdc++.h>
using namespace std;
auto sol = [](string a, string b) {
vector dp(a.size(), vector(b.size(), -1));
auto rec = [&](const auto& self, int i, int j) -> int {
if (i < 0 || j < 0) return 0;
int& ret = dp[i][j];
if (ret != -1) return ret;
ret = -(1 << 30);
ret = max(ret, self(self, i - 1, j));
ret = max(ret, self(self, i, j - 1));
if (a[i] == b[j]) ret = max(ret, self(self, i - 1, j - 1) + 1);
return ret;
};
string ret;
auto trace = [&](const auto& self, int i, int j) -> void {
if (i < 0 || j < 0) return;
int val = rec(rec, i, j);
if (rec(rec, i - 1, j) == val) {
self(self, i - 1, j);
}
else if (rec(rec, i, j - 1) == val) {
self(self, i, j - 1);
}
else {
self(self, i - 1, j - 1);
ret.push_back(a[i]);
}
};
trace(trace, a.size() - 1, b.size() - 1);
return ret;
};
int main() {
cin.tie(0)->sync_with_stdio(0);
string a, b; cin >> a >> b;
cout << sol(a, b) << '\n';
}