-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathKMP.cpp
More file actions
40 lines (40 loc) · 863 Bytes
/
KMP.cpp
File metadata and controls
40 lines (40 loc) · 863 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
32
33
34
35
36
37
38
39
40
// KMP string matching
#include <bits/stdc++.h>
using namespace std;
vector<int>fail;
void build(string s) {
fail.push_back(-1);
int p1 = 0, p2 = -1;
while (p1 < (int)s.size()) {
if (p2 == -1 || s[p1] == s[p2]) {
p1++, p2++;
fail.push_back(p2);
}
else {
p2 = fail[p2];
}
}
}
int match(string s, string t) {
int p1 = 0, p2 = 0;
int ans = 0;
while (p1 < (int)s.size()) {
if (p2 == -1 || s[p1] == t[p2]) {
p1++, p2++;
if (p2 == (int)t.size()) {
ans++;
p2 = fail[p2];
}
}
else {
p2 = fail[p2];
}
}
return ans;
}
void solve() {
string s, t;
cin >> s >> t;
build(t); // build failure
cout << match(s, t) << '\n'; // t appear time in s
}