-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathKMP.cpp
More file actions
61 lines (50 loc) · 1.32 KB
/
KMP.cpp
File metadata and controls
61 lines (50 loc) · 1.32 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
#include <bits/stdc++.h>
using namespace std;
const int N = 100100;
// KMP longest match array.
int F[N];
/**
* KMP failure function.
*
* @param pat the pattern string.
* @param cur the current char to get its failure index.
* @param len the length of the longest suffix ending at the previous character
* matching a prefix of the pattern.
*
* @return the length of the longest suffix ending the given character matching a prefix of the pattern.
*/
int failure(const char* pat, char cur, int len) {
while (len > 0 && cur != pat[len]) {
len = F[len - 1];
}
return len + (cur == pat[len]);
}
/**
* Computes the length of the longest suffix ending at the i-th character
* that match a prefix of the string, and fills the results in the global "F" array.
*
* Complexity: O(n)
*
* @param pat the pattern string to compute its KMP.
*/
void KMP(const char* pat) {
for (int i = 1; pat[i]; ++i) {
F[i] = failure(pat, pat[i], F[i - 1]);
}
}
/**
* Sample driver program.
*/
int main() {
string pattern, text;
cin >> pattern >> text;
KMP(pattern.c_str());
int l = 0;
for (int i = 0; i < text.size(); ++i) {
l = failure(pattern.c_str(), text[i], l);
if (l == pattern.size()) {
cout << "Found substring" << endl;
}
}
return 0;
}