blob: 4ac7afdc5a18da9f99092a48316ce5fa51f31c6b (
plain)
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
|
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
using std::cin;
using std::string;
using std::vector;
vector<int> find_pattern(const string& pattern, const string& text)
{
vector<int> result;
string s = pattern + "$" + text;
vector<int> prefixes(s.size());
int border = 0;
int ps = pattern.size();
for (int i = 1; i < s.size(); i++) {
while ((border > 0) && (s[i] != s[border]))
border = prefixes[border - 1];
prefixes[i] = border = ((s[i] == s[border]) ? (border + 1) : 0);
if (border == ps)
result.push_back(i - 2 * ps);
}
return result;
}
int main() {
string pattern, text;
cin >> pattern;
cin >> text;
vector<int> result = find_pattern(pattern, text);
for (int i = 0; i < result.size(); ++i) {
printf("%d ", result[i]);
}
printf("\n");
return 0;
}
|