-
Notifications
You must be signed in to change notification settings - Fork 9
/
kmp.cpp
51 lines (44 loc) · 980 Bytes
/
kmp.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
// kmp.cpp
// Andrei Racila; Jan. 2, 2019
#include <bits/stdc++.h>
using namespace std;
vector<int> comp_shifts(string P) {
int p = P.length();
vector<int> shifts(p);
for (int q = 1; q < p; q++) {
int k = shifts[q - 1];
while (k > 0 && P[k] != P[q])
k = shifts[k - 1];
if (P[k] == P[q])
k++;
shifts[q] = k;
}
return shifts;
}
int kmp(string P, string T) {
vector<int> shifts = comp_shifts(P);
int n = T.length();
int m = P.length();
int occurrences = 0;
int q = 0;
for (int i = 0; i < n; i++) {
while (q && P[q] != T[i])
q = shifts[q - 1];
if (P[q] == T[i])
q++;
if (q == m) {
occurrences++;
q = shifts[q - 1];
}
}
return occurrences;
}
int main() {
string T; cin >> T; // text to search
int Q; cin >> Q; // number of queries
for (int i = 0; i < Q; i++) {
string P; cin >> P; // pattern to search for in text T
cout << kmp(P, T) << endl; // prints number of occurrences of pattern P in text T
}
return 0;
}