-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathz_algorithm.cpp
More file actions
41 lines (38 loc) · 950 Bytes
/
z_algorithm.cpp
File metadata and controls
41 lines (38 loc) · 950 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
39
40
41
void prefix_table(vector<int> &table,string s,int n){
int left=0,right=0;
for(int k=1;k<n;k++){
if(k>right){
left=right=k;
while(right<n && s[right]==s[right-left])
right++;
table[k]=right-left;
right--;
}
else{
int k1=k-left;
if(table[k1]<right-k+1)
table[k]=table[k1];
else{
left=k;
while(right<n && s[right]==s[right-left])
right++;
table[k]=right-left;
right--;
}
}
}
}
int zAlgorithm(string p, string s, int m, int n)
{
string temp=s+"$"+p;
vector<int> table(n+m+1);
prefix_table(table,temp,n+m+1);
int count=0;
for(int i=0;i<n+m+1;i++){
//cout<<table[i]<<" ";
if(table[i]==n)
count++;
}
//cout<<endl;
return count;
}