-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKMP.cpp
63 lines (58 loc) · 1.28 KB
/
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
52
53
54
55
56
57
58
59
60
61
62
63
#include <bits/stdc++.h>
using namespace std;
void computeLPS(int m, string& ptr, vector<int>& lps) {
int len = 0, i = 1;
while (i < m) {
if (ptr[i] == ptr[len]) {
len++;
lps[i] = len;
i++;
} else {
if (len != 0) {
len = lps[len-1];
} else {
i++;
}
}
}
}
vector<int> KMP(string& str, string& ptr) {
int n = str.length(), m = ptr.length();
vector<int> lps(m);
computeLPS(m, ptr, lps);
vector<int> pos;
int i = 0, j = 0;
while (i < n) {
if (ptr[j] == str[i]) {
i++, j++;
} else {
if (j != 0) {
j = lps[j - 1];
} else {
i++;
}
}
if (j == m) {
pos.push_back(i - j);
j = lps[j - 1];
}
}
return pos;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tc;
cin >> tc;
while (tc--) {
string str, ptr;
cin >> str >> ptr;
vector<int> ans = KMP(str, ptr);
int sz = ans.size();
cout << sz << '\n';
for (int i = 0; i < sz; i++) {
cout << ans[i] + 1 << " \n"[i == sz - 1];
}
}
return 0;
}