-
Notifications
You must be signed in to change notification settings - Fork 288
/
Copy pathAC_KMP_n.cpp
67 lines (59 loc) · 1.5 KB
/
AC_KMP_n.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
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_KMP_n.cpp
* Create Date: 2014-12-24 10:19:32
* Descripton: KMP
* just as HDU 2087: http://blog.csdn.net/hcbbt/article/details/17003191
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 0;
class Solution {
private:
int next[1000100];
public:
int strStr(char *haystack, char *needle) {
int hlen = strlen(haystack);
int nlen = strlen(needle);
int i, j;
if (hlen < nlen)
return -1;
if (nlen == 0)
return 0;
// KMP
// get next matching array
// int *next = new int(nlen + 1);
// Because the string will be 1 million, so we cannot use `int *next = new int(nlen + 1)`, it will exceed the memory in function
i = 0;
j = -1;
next[0] = -1;
while (i < nlen) {
if (j == -1 || needle[i] == needle[j]) {
i++;
j++;
next[i] = j;
} else
j = next[j];
}
// matching
i = 0;
j = 0;
while (i < hlen) {
if (j == -1 || haystack[i] == needle[j]) {
i++;
j++;
} else
j = next[j];
if (j == nlen)
return i - nlen;
}
return -1;
}
};
int main() {
char a[100], b[100];
Solution s;
while (cin >> a >> b)
cout << s.strStr(a, b) << endl;
return 0;
}