summaryrefslogtreecommitdiff
path: root/string/kmp.cpp
blob: 282019e68f70b5e242be38f5a7a7606b6668f155 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
vector<int> kmpPreprocessing(const string& sub) {
	vector<int> b(sub.size() + 1);
	b[0] = -1;
	int i = 0, j = -1;
	while (i < (int)sub.size()) {
		while (j >= 0 && sub[i] != sub[j]) j = b[j];
		i++; j++;
		b[i] = j;
	}
	return b;
}
vector<int> kmpSearch(const string& s, const string& sub) {
	vector<int> pre = kmpPreprocessing(sub), result;
	int i = 0, j = 0;
	while (i < (int)s.size()) {
		while (j >= 0 && s[i] != sub[j]) j = pre[j];
		i++; j++;
		if (j == (int)sub.size()) {
			result.push_back(i - j);
			j = pre[j];
	}}
	return result;
}