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"?
- A. 22
- B. 19
- C. 41
- 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.
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
- 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?
- Let t = "ABCC" (its letters may repeat). What is the length of the shortest substring of "ADOBECODEBANCXY" that contains every letter of t…
- Among all cyclic rotations of "bcabca" (there are n = 6 of them, including the original), what is the 0-based index i of the…