-
Notifications
You must be signed in to change notification settings - Fork 1
/
09252.cpp
43 lines (37 loc) · 893 Bytes
/
09252.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
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
void solve(void) {
string a, b, ans;
cin >> a;
cin >> b;
vector<vector<int> > dp(a.size()+1, vector<int>(b.size()+1, 0));
for (int i=0; i<a.size(); i++) {
for (int j=0; j<b.size(); j++) {
if (a[i] == b[j]) dp[i+1][j+1] = dp[i][j] + 1;
else dp[i+1][j+1] = max(dp[i+1][j], dp[i][j+1]);
}
}
if (dp[a.size()][b.size()] == 0) {
cout << "0\n";
return;
}
for (int i=a.size(), j=b.size(); i>0 && j>0;) {
if (dp[i][j] == dp[i-1][j]) i--;
else if (dp[i][j] == dp[i][j-1]) j--;
else {
ans.push_back(a[i-1]);
i--; j--;
}
}
reverse(ans.begin(), ans.end());
cout << dp[a.size()][b.size()] << "\n" << ans;
}
int main(void) {
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
solve();
return 0;
}