-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAcWing831.KMP字符串.cpp
More file actions
46 lines (39 loc) · 1.1 KB
/
AcWing831.KMP字符串.cpp
File metadata and controls
46 lines (39 loc) · 1.1 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
#include <iostream>
#include <vector>
#include <string>
// 计算部分匹配表(前缀函数)
static std::vector<int> computePrefixFunction(const std::string& pattern) {
int m = pattern.length();
std::vector<int> lps(m, 0); // 最长相等前缀后缀数组
int j = 0;
for (int i = 1; i < m; ++i) {
while (j > 0 && pattern[i] != pattern[j]) {
j = lps[j - 1]; // 回退
}
if (pattern[i] == pattern[j]) {
++j;
}
lps[i] = j;
}
return lps;
}
// KMP匹配函数
static void KMP(const std::string& text, const std::string& pattern) {
int n = text.length();
int m = pattern.length();
// 计算部分匹配表
std::vector<int> lps = computePrefixFunction(pattern);
int j = 0; // pattern的指针
for (int i = 0; i < n; ++i) {
while (j > 0 && text[i] != pattern[j]) {
j = lps[j - 1]; // 回退
}
if (text[i] == pattern[j]) {
++j;
}
if (j == m) {
std::cout << "Pattern found at index " << i - m + 1 << std::endl;
j = lps[j - 1]; // 寻找下一个匹配
}
}
}