Data Structures & AlgorithmsVery hardGATE CS · ICPC regional

What is the sum of the Z-function values Z[i] for i = 1 to n-1 of the string aaabaaab (Z[i] is the length of the longest common prefix of the string and its suffix starting at i)?

  1. A. 11
  2. B. 8
  3. C. 13
  4. D. 10

Correct answer: D. 10

Each Z[i] is the longest common prefix of s and s[i..]; the box [l, r] reuse trick saves work but does not change the values. Counting Z[0] as n inflates the sum.

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

Exams that ask this topic