GATE Computer ScienceHardGATE CS (PYQ pattern)

For the pattern P = abcabdabcabcabd, the KMP failure (prefix) function π[i] is the length of the longest proper prefix of P[0..i] that is also a suffix of it. What is the sum π[0] + π[1] + … + π[14]?

  1. A. 33
  2. B. 38
  3. C. 36
  4. D. 41

Correct answer: C. 36

π = [0, 0, 0, 1, 2, 0, 1, 2, 3, 4, 5, 3, 4, 5, 6]. At index 5 (d) the border is lost (0), it rebuilds through abcab (5) at index 10, and at index 11 the match abcab is followed by d, so the algorithm falls back to the border ab (length 2) and extends it with c to 3. The entries sum to 36.

More Algorithms questions

Exams that ask this topic