-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy path4.6.cpp
71 lines (63 loc) · 1.6 KB
/
4.6.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
67
68
69
70
71
/*
* 题目名称:Number Sequence
* 题目来源:HDU 1711
* 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711
* 代码作者:杨泽邦(炉灰)
*/
#include <iostream>
#include <cstdio>
using namespace std;
const int MAXM = 10000 + 10;
const int MAXN = 1000000 + 10;
int nextTable[MAXM];
int pattern[MAXM];
int text[MAXN];
void GetNextTable(int m) { //创建next表
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(int n, int m) {
GetNextTable(m);
int i = 0;
int j = 0;
while (i < n && j < m) {
if (j == -1 || text[i] == pattern[j]) { //当前字符匹配成功
i++;
j++;
} else {
j = nextTable[j]; //当前字符匹配失败
}
}
if (j == m) { //模式串匹配成功
return i - j + 1;
} else { //模式串匹配失败
return -1;
}
}
int main() {
int caseNumber;
scanf("%d", &caseNumber);
while (caseNumber--) {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i < n; ++i) {
scanf("%d", &text[i]);
}
for (int j = 0; j < m; ++j) {
scanf("%d", &pattern[j]);
}
printf("%d\n", KMP(n, m));
}
return 0;
}