-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathz_algo.cpp
More file actions
53 lines (53 loc) · 945 Bytes
/
Copy pathz_algo.cpp
File metadata and controls
53 lines (53 loc) · 945 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
42
43
44
45
46
47
48
49
50
51
52
53
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int z[100005];
void z_algo(string s)
{
int sz=s.size();
int L=0,R=0,k;
for(int i=1; i<sz; i++)
{
if(i>R)
{
L=R=i;
while(R<sz&&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<sz&&s[R-L]==s[R])
{
R++;
}
z[i]=R-L;
R--;
}
}
}
}
int main()
{
int i,j,k,l,n,m;
string s;
cin>>s;
z_algo(s);
for(i=0; i<s.size(); i++)
{
cout<<z[i]<<" ";
}
cout<<endl;
return 0;
}