forked from ncduy0303/Competitive-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZ-algo (Z function).cpp
More file actions
51 lines (43 loc) · 1.33 KB
/
Z-algo (Z function).cpp
File metadata and controls
51 lines (43 loc) · 1.33 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
50
51
// 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)
// 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 + 5;
const ll MOD = 1e9 + 7;
const ll INF = 1e9;
// f[i] = length of the longest common prefix between s and the substring s[i...n-1]
vector<int> z_func(string s) {
int n = s.size();
vector<int> f(n); // f[0] = 0 by default
for (int i = 1, l = 0, r = 0; i < n; i++) {
if (i <= r) f[i] = min(r - i + 1, f[i - l]);
while (i + f[i] < n && s[f[i]] == s[i + f[i]]) f[i]++;
if (i + f[i] - 1 > r) l = i, r = i + f[i] - 1;
}
return f;
}
int cnt_occ(string s, string t) {
string ts = t + "#" + s;
int n = t.size(), m = s.size(), nm = ts.size();
auto f = z_func(ts);
int res = 0;
for (int i = n + 1; i < nm; i++) res += (f[i] == n);
return res;
}
void solve() {
string s, t; cin >> s >> t;
cout << cnt_occ(s, t) << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int tc = 1;
// cin >> tc;
for (int t = 1; t <= tc; t++) {
// cout << "Case #" << t << ": ";
solve();
}
}