-
Notifications
You must be signed in to change notification settings - Fork 1
/
z-function.cpp
145 lines (127 loc) · 3.64 KB
/
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/***************************************************
* Problem Name : z-function.cpp
* Problem Link : Basic Code
* Verdict : done
* Date : 2020-12-03
* Problem Type : String
* Author Name : Saikat Sharma
* University : CSE, MBSTU
***************************************************/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <sstream>
#include <vector>
#include <queue>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <cstdlib>
#include <deque>
#include <stack>
#include <bitset>
#include <cassert>
#include <map>
#include <set>
#include <cassert>
#include <iomanip>
#include <random>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define __FileRead freopen ("input.txt", "r", stdin)
#define __FileWrite freopen ("output.txt", "w", stdout)
#define SET(a,v) memset(a,v,sizeof(a))
#define SZ(v) (int)v.size()
#define pii pair<ll,ll>
#define pll pair <ll, ll>
#define debug cout <<"######\n"
#define debug1(x) cout <<"### " << x << " ###\n"
#define debug2(x,y) cout <<"# " << x <<" : "<< y <<" #\n"
#define nl cout << "\n";
#define sp cout << " ";
#define sl(n) scanf("%lld", &n)
#define sf(n) scanf("%lf", &n)
#define si(n) scanf("%d", &n)
#define ss(n) scanf("%s", n)
#define pf(n) scanf("%d", n)
#define pfl(n) scanf("%lld", n)
#define all(v) v.begin(), v.end()
#define srt(v) sort(v.begin(), v.end())
#define r_srt(v) sort(v.rbegin(), v.rend())
#define rev(v) reverse(v.rbegin(), v.rend())
#define Sqr(x) ((x)*(x))
#define Mod(x, m) ((((x) % (m)) + (m)) % (m))
#define Max3(a, b, c) max(a, max(b, c))
#define Min3(a, b, c) min(a, min(b, c))
#define pb push_back
#define mk make_pair
#define F first
#define S second
#define MAX 200005
#define INF 1000000009
#define MOD 1000000007
template<class T>
using min_heap = priority_queue<T, std::vector<T>, std::greater<T>>;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
tree_order_statistics_node_update>;
/************************************ Code Start Here ******************************************************/
// How many time a pattern occur in a text
int Z[MAX];
// Z[i] (i>0, Z[0]=0) is the value of maximum sub string that start from i
// and also a prefix of that string
void z_funtion (string str) {
int n = (int) str.size();
int L = 0, R = 0;
for (int i = 1; i < n; i++) {
if (i > R) {
L = R = i;
while (R < n && str[R - L] == str[R]) {
R++;
}
Z[i] = R - L;
R--;
} else {
int k = i - L;
if (Z[k] < R + i - 1) {
Z[i] = Z[k];
} else {
L = i;
while (R < n && str[R - L] == str[R]) {
R++;
}
Z[i] = R - L;
R--;
}
}
}
}
int main () {
//~ __FastIO;
string text, patten;
cin >> text;
cin >> patten;
string str = patten + "$" + text;
z_funtion (str);
int cnt = 0;
for (int i = (int) patten.size(); i < (int) str.size(); i++) {
if (Z[i] == (int) patten.size() ) {
cnt++;
}
}
cout << cnt << "\n";
return 0;
}
//~ resources
//~ https://ivanyu.me/blog/2013/10/15/z-algorithm/
//~ https://codeforces.com/blog/entry/3107
//~ https://www.hackerearth.com/practice/algorithms/string-algorithm/z-algorithm/tutorial/