Data Structures & AlgorithmsVery hardICPC regional · string round
Problem: What is the KMP prefix-function array pi for the string 'ababaca'?
Problem: What is the KMP prefix-function array pi for the string 'ababaca'?
s = 'ababaca'
pi = [0] * len(s)
for i in range(1, len(s)):
k = pi[i - 1]
while k and s[i] != s[k]:
k = pi[k - 1]
if s[i] == s[k]:
k += 1
pi[i] = k
print(pi)What is printed?- A. [0, 0, 1, 2, 3, 1, 0]
- B. [0, 0, 1, 2, 3, 0, 1]
- C. [0, 1, 0, 1, 2, 0, 1]
- D. [0, 0, 1, 2, 2, 0, 1]
Correct answer: B. [0, 0, 1, 2, 3, 0, 1]
pi[i] is the longest proper border of s[0..i]. At index 4 ('ababa') the border 'aba' has length 3, and at index 6 the border is 'a' of length 1.
More Coding Interview Problems questions
- Problem: What is the lexicographically smallest rotation of 'bcabca'? python s = 'bcabca' print(min(s[i:] + s[:i] for i in range(len(s))))…
- Problem: Items (weight, value) are (2,3), (3,4), (4,5), and each may be used at most once. Capacity W = 7. Run the code below to find the…
- Problem: How many ways can a staircase of 8 steps be climbed if each move climbs 1, 3 or 4 steps? Run the DP below. python ways = [0] 9…
- Problem: Jobs are given as (deadline, profit): (2,100), (1,19), (2,27), (1,25), (3,15). Each job takes one unit slot and must finish by…
- Problem: Compute C(10, 3) mod 7 using Lucas' theorem, where 7 is prime. What is the result?
- Problem: What is 3^100 mod 10?