-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathz-algorithm.cpp
More file actions
78 lines (62 loc) · 1.13 KB
/
z-algorithm.cpp
File metadata and controls
78 lines (62 loc) · 1.13 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
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
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double dbl;
#define fr(x,a,b) for(ll x=a;x<b;x++)
#define rf(x,a,b) for(ll x=a;x>b;x--)
#define pii pair<ll,ll>
#define PB push_back
#define MP make_pair
#define mod 1000000007
#define gmax LLONG_MAX
#define gmin LLONG_MIN
#define INF 2e9
#define N 100001
#define MAX(a,b,c) max(max(a,b),c)
#define MIN(a,b,c) min(min(a,b),c)
#define SZ(s) s.size()
#define MS(x,v) memset(x,v,sizeof(x))
string pattern,text,s;
ll z[2*N];
void construct_z_array(){
s="";
s+=pattern;
s+='$';
s+=text;
ll n=SZ(s);
ll l=0,r=0,k;
z[0]=-1; // useless
fr(i,1,n){
if(i>r){
l=r=i;
while(r<n && s[r-l]==s[r]) r++;
z[i]=r-l;
r--;
}
else{
k=i-l;
if(z[k]<r-i+1) z[i]=z[k];
else{
l=i;
while(r<n && s[r-l]==s[r]) r++;
z[i]=r-l;
r--;
}
}
}
}
ll search(){
ll cnt=0;
fr(i,1,SZ(s)) if(z[i]==SZ(pattern)) cnt++;
return cnt;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>text;
cin>>pattern;
construct_z_array();
cout<<search();
return 0;
}