Data Structures & AlgorithmsVery hardCodeforces Div2 D · GATE CS

Using the Z-function (z[i] = length of the longest common prefix of s and s[i..], with z[0] = n), what is the sum of all z-values of "aabxaabxcaabxaabxay"?

  1. A. 22
  2. B. 19
  3. C. 41
  4. D. 8

Correct answer: C. 41

Non-zero z-values are z[0] = 19 and 1, 4, 1, 8, 1, 5, 1, 1 at later positions, total 41. Setting z[0] = 0, a common convention error, gives 22; 19 is just n and 8 counts the non-zero positions instead of summing them.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic