FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Competitive-Coding/Z Algorithm.cpp at master · genalt/Competitive-Coding · GitHub
genalt
/
Competitive-Coding
Public
forked from
Ashishgup1/Competitive-Coding
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
Competitive-Coding
/
Z Algorithm.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
26 lines (22 loc) · 796 Bytes
Breadcrumbs
Competitive-Coding
/
Z Algorithm.cpp
Copy path
File metadata and controls
26 lines (22 loc) · 796 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
//
Logic = https://cp-algorithms.com/string/z-function.html
//
The Z-function for this string is an array of length n where the i-th element is equal to the greatest number
//
of characters starting from the position i that coincide with the first characters of s.
vector<
int
>
z_function
(string &s)
{
int
n=s.
size
();
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;
}
//
Sample Problem 1: http://codeforces.com/problemset/problem/127/D
//
Sample Solution 1: http://codeforces.com/contest/127/submission/39791421
//
Sample Problem 2: http://codeforces.com/contest/432/problem/D
//
Sample Solution 2: http://codeforces.com/contest/432/submission/39791526
Back
|
FazBrowse Home
|
New Git URL