forked from ncduy0303/Competitive-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Z-algo (Z function).cpp
61 lines (51 loc) · 1.52 KB
/
Z-algo (Z function).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
// Given a string s (with length n) and a pattern p (with length m), find all the occurrence of p in s
// Time complexity: O(n + m)
// Build the Z function, Z[i] = the maximum length of the common prefix of substring [i..n] and the original string [1..n]
// Problem link: https://cses.fi/problemset/task/1753/
#include <bits/stdc++.h>
using namespace std;
#define ar array
#define ll long long
const int MAX_N = 1e5 + 1;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const ll LINF = 1e18;
int num;
vector<int> Z;
vector<int> Z_Func(string t) {
vector<int> Z(t.size());
int l = -1, r = -1;
Z[0] = t.size();
for (int i = 1; i < t.size(); i++) {
Z[i] = (i >= r) ? 0 : min(r - i, Z[i - l]);
while (i + Z[i] < t.size() && t[i + Z[i]] == t[Z[i]]) Z[i]++;
if (i + Z[i] > r) {
l = i;
r = i + Z[i];
}
}
return Z;
}
void Z_Algo(string s, string p) {
string t = p + "#" + s;
Z = Z_Func(t);
for (int i = p.size() + 1; i < t.size(); i++)
if (Z[i] == p.size()) // find a match at index i - p.size() - 1
num++;
}
void solve() {
string s, p; cin >> s >> p;
Z_Algo(s, p);
cout << num << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int tc; tc = 1;
for (int t = 1; t <= tc; t++) {
// cout << "Case #" << t << ": ";
solve();
}
}