-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkmp.cpp
More file actions
69 lines (67 loc) · 1.56 KB
/
kmp.cpp
File metadata and controls
69 lines (67 loc) · 1.56 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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int SIZE = 1e6+6;
/*
* NEXT[]的含义:x[i-NEXT[i]...i-1] = x[0...NEXT[i]-1]
* NEXT[i]为满足 x[i-z...i-1] = x[0...z-1] 的最大z值(就是x的自身匹配)
*/
void kmp_pre(char x[], int m, int NEXT[]) {
int i, j;
j = NEXT[0] = -1;
i = 0;
while(i < m) {
while(j != -1 && x[i] != x[j]) j = NEXT[j];
NEXT[++i] = ++j;
}
}
/*
* kmpNEXT[i]的意思:NEXT[i] = NEXT[NEXT[...[NEXTp[i]]]](直到NEXT[i] < 0 或者 x[NEXT[i]] != x[i])
* 这样的预处理可以快一些
*/
void preKMP(char x[], int m, int kmpNEXT[]) {
int i, j;
j = kmpNEXT[0] = -1;
i = 0;
while(i < m) {
while(j != -1 && x[i] != x[j]) j = kmpNEXT[j];
if(x[++i] == x[++j]) kmpNEXT[i] = kmpNEXT[j];
else kmpNEXT[i] = j;
}
}
/*
* 返回 x 在 y 中出现的次数,可以重叠
*/
int NEXT[SIZE];
int KMP_Count(char x[], int m, char y[], int n) {
int i, j;
int ans = 0;
// preKMP(x, m, next);
kmp_pre(x, m, NEXT);
i = j = 0;
while(i < n) {
while(j != -1 && y[i] != x[j]) j = NEXT[j];
i++;j++;
if(j >= m) {
ans++;
j = NEXT[j];
}
}
return ans;
}
int main()
{
int T;
scanf("%d", &T);
while(T--) {
char str1[SIZE], str2[SIZE];
scanf("%s", str1);
scanf("%s", str2);
int m = strlen(str1), n = strlen(str2);
int ans = KMP_Count(str1, m, str2, n);
printf("%d\n", ans);
}
return 0;
}