-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
80 lines (70 loc) · 1.93 KB
/
main.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
void solve(string x, string y);
void getLCS(const string& x, string y, vector<int>& lastLine);
int main() {
string s1, s2;
cin >> s1 >> s2;
solve(s1, s2);
return 0;
}
void solve(string x, string y) {
if(y.empty()) {
return;
}
if (x.length() == 1) {
if (find(y.begin(), y.end(), x[0]) != y.end()) {
cout << x[0];
}
return;
}
int middle = x.length() / 2;
vector<int> first(y.length() + 1), second(y.length() + 1);
getLCS(x.substr(0, middle), y, first);
string xSecond = x, ySecond = y;
reverse(xSecond.begin(), xSecond.end());
reverse(ySecond.begin(), ySecond.end());
cout << xSecond << endl;
getLCS(xSecond.substr(0, x.length() - middle), ySecond, second);
int maxVal = second[0], maxValPos = 0;
for (int j = 0; j <= y.length(); j++) {
if (first[j] + second[y.length() - j] > maxVal) {
maxVal = first[j] + second[y.length() - j];
maxValPos = j;
}
}
if (first[y.length()] > maxVal) {
maxValPos = y.length();
}
if (middle == 0) {
middle++;
}
solve(x.substr(0, middle), y.substr(0, maxValPos));
solve(x.substr(middle, x.length()), y.substr(maxValPos, y.length()));
}
void getLCS(const string& x, string y, vector<int>& lastLine) {
cout << x << ' ' << y << endl;
vector<vector<int>> twoMatrixLines(2, vector<int>(y.length() + 1));
for (int j = 0; j <= y.length(); j++) {
twoMatrixLines[1][j] = 0;
}
for(char i : x) {
for (int j = 0; j <= y.length(); j++) {
twoMatrixLines[0][j] = twoMatrixLines[1][j];
}
for (int j = 1; j <= y.length(); j++) {
if (i == y[j - 1]) {
twoMatrixLines[1][j] = twoMatrixLines[0][j - 1] + 1;
}
else {
twoMatrixLines[1][j] = max(twoMatrixLines[1][j - 1], twoMatrixLines[0][j]);
}
}
}
for (int j = 0; j <= y.length(); j++) {
lastLine[j] = twoMatrixLines[1][j];
}
}