Data Structures & AlgorithmsVery hardGATE CS · ICPC regional
For the pattern "aabaabaaab" (0-based), what is the value of the KMP prefix function π at the LAST index? π[i] is the length of the longest proper prefix of p[0..i] that is also a suffix of it.
- A. 3
- B. 2
- C. 7
- D. 8
Correct answer: A. 3
Compute π with the standard failure loop: on a mismatch fall back through π[k−1] before comparing again. Reporting the value one index early, or the complement length, or the number of nonzero entries are the typical slips.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- How many times does the pattern "aaa" occur in the text "aaaabaaaaab", counting OVERLAPPING occurrences (occurrences may share letters)?
- 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…
- What is the length of the longest substring of "pwwkewfabcdbbca" with no repeated character?
- How many DISTINCT substrings (by content, non-empty) does "abcab" have?
- A polynomial hash is H(s) = (Σ ord(s[i]) · B^(n−1−i)) mod M, computed left to right as h = (h·B + ord(c)) mod M, with B = 31 and M =…
- How many DISTINCT palindromic substrings (by content, length at least 1) does "abaaba" have?