-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathz_algorithm.cpp
More file actions
49 lines (43 loc) · 1.2 KB
/
z_algorithm.cpp
File metadata and controls
49 lines (43 loc) · 1.2 KB
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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// Searches for all occurrences of pattern in string
// TC: O(n) -> O(s+p)
///////////////////// start yanking ////////////////////
vector<ll> z_function(string s) {
vector<ll> z(s.size());
for (ll i = 1, l = 0, r = 0; i < s.size(); i++) {
if (i <= r) {
z[i] = min(r-i+1, z[i-l]);
}
while (i+z[i] < s.size() && s[z[i]] == s[i+z[i]]) {
z[i]++;
}
if (i+z[i]-1 > r) {
l = i, r = i+z[i]-1;
}
}
return z;
}
vector<ll> z_search(const string &str, const string &pat) {
string sep = "%"; // separator must NOT be present in string or pattern
string concat = pat + sep + str;
vector<ll> z = z_function(concat), indices;
for (ll i = 0; i < str.size(); i++) {
if (z[i+pat.size()+1] == pat.size()) {
indices.push_back(i);
}
}
return indices;
}
///////////////////// stop yanking /////////////////////
int main() {
string str, pat;
getline(cin, str);
getline(cin, pat);
auto indices = z_search(str, pat); // called
for (auto &index : indices) {
cout << index << " ";
}
return 0;
}