-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlcs.cpp
More file actions
72 lines (63 loc) · 1.66 KB
/
Copy pathlcs.cpp
File metadata and controls
72 lines (63 loc) · 1.66 KB
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
#include <vector>
#include <algorithm>
#include <string>
#include <iostream>
#include <cstring>
using namespace std;
int dp[1000][1000];
int record[1000];
// 求解最长公共子序列,使用dp啦
string lcs(string a, string b) {
string res;
memset(dp, 0, sizeof(dp));
int a_len = a.length(), b_len = b.length();
// 记录a中字符串的位置
for (int i = 0; i < a_len; ++i) {
dp[0][i] = a[i] == b[0] ? 1 : 0;
}
for (int i = 0; i < b_len; ++i) {
dp[i][0] = a[0] == b[i] ? 1 : 0;
}
int max_len = 0;
for (int i = 1; i < a_len; ++i) {
for (int j = 1; j < b_len; ++j) {
if (a[i] == b[j]) {
dp[i][j] = dp[i - 1][j - 1] + 1;
// record[i] = i - 1;
} else {
// record[i] = dp[i - 1][j] > dp[i][j - 1] ? i - 1 : i;
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
max_len = max(max_len, dp[i][j]);
printf("%d ", dp[i][j]);
}
printf("\n");
}
// 开始构建结果字符串
cout << max_len << "\n";
int idx = 0;
res.resize(max_len, ' ');
// for (int i = 0; i < a_len; ++i) {
// if (dp[0][i] == 1) {
// res[idx++] = a[i];
// break;
// }
// }
int row = 1, col = 1;
while (row < b_len && col < a_len) {
if (dp[row][col] == dp[row - 1][col - 1]) {
res[idx++] = a[col];
row++;
col++;
} else {
col++;
}
}
return res;
}
int main() {
string a = "1ewq234567891234";
string b = "01234321234";
cout << lcs(a, b) << "\n";
return 0;
}