-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11049.cpp
40 lines (40 loc) · 889 Bytes
/
11049.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
#include <bits/stdc++.h>
using namespace std;
int dp[1001][1001];
string a;
string b;
string ans;
int main() {
cin >> a >> b;
a = '0' + a;
b = '0' + b;
int alen = a.size();
int blen = b.size();
for(int i = 0; i < alen; i++) {
for(int j = 0; j < blen; j++) {
if(i == 0 || j == 0) {
dp[i][j] = 0;
continue;
}
if(a[i] == b[j]) {
dp[i][j] = dp[i-1][j-1] + 1;
}
else {
dp[i][j] = max(dp[i][j-1], dp[i-1][j]);
}
}
}
int i = alen - 1;
int j = blen - 1;
cout << dp[i][j] << '\n';
while(dp[i][j] != 0) {
if(dp[i][j] == dp[i][j-1]) j--;
else if(dp[i][j] == dp[i-1][j]) i--;
else {
ans = b[j] + ans;
i--;
j--;
}
}
cout << ans;
}