-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy path4.7.cpp
66 lines (59 loc) · 1.5 KB
/
4.7.cpp
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
/*
* 题目名称:Oulipo
* 题目来源:POJ 3461
* 题目链接:http://poj.org/problem?id=3461
* 代码作者:杨泽邦(炉灰)
*/
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
const int MAXM = 10000 + 10;
int nextTable[MAXM];
void GetNextTable(string pattern) { //创建next表
int m = pattern.size();
int j = 0;
nextTable[j] = -1;
int t = nextTable[j];
while (j < m) {
if (t == -1 || pattern[j] == pattern[t]) {
j++;
t++;
nextTable[j] = t;
} else {
t = nextTable[t];
}
}
return ;
}
int KMP(string text, string pattern) {
GetNextTable(pattern);
int n = text.size();
int m = pattern.size();
int i = 0;
int j = 0;
int number = 0; //记录匹配次数
while (i < n) {
if (j == -1 || text[i] == pattern[j]) { //当前字符匹配成功
i++;
j++;
} else {
j = nextTable[j]; //当前字符匹配失败
}
if (j == m) { //模式串匹配成功
number++;
j = nextTable[j];
}
}
return number;
}
int main() {
int caseNumber;
scanf("%d", &caseNumber);
while (caseNumber--) {
string pattern, text;
cin >> pattern >> text;
printf("%d\n", KMP(text, pattern));
}
return 0;
}