forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKMP.cpp
More file actions
31 lines (28 loc) · 806 Bytes
/
KMP.cpp
File metadata and controls
31 lines (28 loc) · 806 Bytes
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
#include <iostream>
#include <vector>
#include <string>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
vector<int> StringMatchKMP(string text, string pattern) {
int n = text.length(), m = pattern.length();
// pi[q] = max { k : k < q and Pk is a suffix of Pq }
vector<int> pi(m, -1);
int k = -1;
FOR(q,1,m-1) {
while (k >= 0 && pattern[k + 1] != pattern[q]) k = pi[k];
if (pattern[k + 1] == pattern[q]) ++k;
pi[q] = k;
}
vector<int> res;
k = -1;
REP(i,n) {
while (k >= 0 && pattern[k + 1] != text[i]) k = pi[k];
if (pattern[k + 1] == text[i]) ++k;
if (k == m - 1) {
res.push_back(i - m + 1);
k = pi[k];
}
}
return res;
}