-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKMP.cpp
More file actions
42 lines (32 loc) · 847 Bytes
/
Copy pathKMP.cpp
File metadata and controls
42 lines (32 loc) · 847 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
/*************************************************************************
> File Name: KMP.cpp
> Author: zhangfb
> Mail: 1819067326
> Created Time: 一 4/19 12:01:43 2021
************************************************************************/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
using namespace std;
//s[]是长文本, p[]是模式串, n是s的长度, m是p的长度
//求模式串中的Next数组
for (int i = 2, j = 0; i <= m; i++) {
while (j && p[i]) != p[j + 1] j = ne[j];
if (p[i] == p[j + 1]) j++;
ne[i] = j;
}
//匹配
for (int i = 1, j = 0; i <= n; i++) {
while (j && s[i] != p[j + 1]) j = ne[j];
if (j == m) {
j = ne[j];
//匹配成功后的逻辑
}
}
int main() {
return 0;
}