MCPcopy Create free account
hub / github.com/Abhishek-Saini/educational / z_function

Function z_function

cses/string/String_Functions.cpp:40–52  ·  view source on GitHub ↗

Z Algorithm

Source from the content-addressed store, hash-verified

38
39// Z Algorithm
40vector<int> z_function(string s) {
41 int n = (int) s.length();
42 vector<int> z(n);
43 for (int i=1, l=0, r=0; i<n; ++i) {
44 if (i <= r)
45 z[i] = min (r-i+1, z[i-l]);
46 while (i+z[i] < n && s[z[i]] == s[i+z[i]])
47 ++z[i];
48 if (i+z[i]-1 > r)
49 l = i, r = i+z[i]-1;
50 }
51 return z;
52}
53
54// KMP LCP Array
55vector<int> prefix_function(string s) {

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected