-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCS2.cpp
More file actions
47 lines (34 loc) · 915 Bytes
/
Copy pathLCS2.cpp
File metadata and controls
47 lines (34 loc) · 915 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
40
41
42
43
44
45
46
47
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<string> vs;
typedef vector<vector<string>> vvs;
string lcs(const string& x, const string& y) {
if (x.length() == 0 || y.length() == 0) return "";
int n = x.length(), m = y.length();
auto dp = vvs(n + 1, vs(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] + string(1, x[i-1]);
else dp[i][j] = dp[i-1][j].size() > dp[i][j-1].size() ? dp[i-1][j] : dp[i][j-1];
}
}
return dp[n][m];
}
int main()
{
string a,b;
cin>>a>>b;
string k = lcs(a,b);
if(k.empty())
{
cout<<0<<endl;
return 0;
}
cout<<k.size()<<'\n'<<k<<endl;
}