- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString_Matching.cpp
More file actions
Latest commit
38 lines (34 loc) · 740 Bytes
/
Copy pathString_Matching.cpp
File metadata and controls
38 lines (34 loc) · 740 Bytes
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
#include<iostream>
#include<unordered_set>
#include<algorithm>
#include<vector>
#defineintlonglong
usingnamespacestd;
vector<int> z_function(string s) {
int n = (int) s.length();
vector<int> z(n);
for (int i = 1, l = 0, r = 0; i < n; ++i) {
if (i <= r)
z[i] = min (r - i + 1, z[i - l]);
while (i + z[i] < n && s[z[i]] == s[i + z[i]])
++z[i];
if (i + z[i] - 1 > r)
l = i, r = i + z[i] - 1;
}
return z;
}
signedmain(){
string p, q;
cin>>p>>q;
string s=q;
s+='$';
s+=p;
auto vec=z_function(s);
int ans=0;
for(auto x : vec){
if(x==(int)q.length()){
ans++;
}
}
cout<<ans<<endl;
}