This repository was archived by the owner on Sep 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab05.cpp
More file actions
62 lines (50 loc) · 1.61 KB
/
lab05.cpp
File metadata and controls
62 lines (50 loc) · 1.61 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
#include "lab.h"
std::string LCS(std::string& str1, std::string& str2, std::vector<std::vector<int>>& C) {
std::string res;
int i = str1.length(), j = str2.length();
while (i > 0 && j > 0) {
if (str1[i - 1] == str2[j - 1]) {
res = str1[i - 1] + res;
i--;
j--;
}
else if (C[i - 1][j] >= C[i][j - 1]) {
i--;
}
else {
j--;
}
}
return res;
}
int calcLCS(std::string& str1, std::string& str2) {
int m = str1.length(), n = str2.length();
// 保存子问题解的数组
// C(i, j) 代表 x(1, ..., i) 和 y(1, ..., j) 的最长子序列
std::vector<std::vector<int>> C(m + 1, std::vector<int>(n + 1, 0));
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
if (str1[i - 1] == str2[j - 1]) {
// 如果 xi 和 yj 相等, 则可以是 x(1, ..., i)
// 和 y(1, ..., j) 的最长子序列最后一个元素
C[i][j] = C[i - 1][j - 1] + 1;
}
// 如果不相等, 则最长子序列是 x(1, ..., i - 1) 与 y(1, ..., j) 或
// x(1, ..., i) 与 y(1, ..., j - 1) 中最长的那个
else if (C[i - 1][j] >= C[i][j - 1]) {
C[i][j] = C[i - 1][j];
}
else {
C[i][j] = C[i][j - 1];
}
}
}
// std::cout << LCS(str1, str2, C) << std::endl;
return C[m][n];
}
void lab05() {
std::string str1;
std::string str2;
std::cin >> str1 >> str2;
std::cout << calcLCS(str1, str2);
}