Data Structures & AlgorithmsVery hardGATE CS · DSA
KMP failure function (lps[i] = length of the longest proper prefix of p[0..i] that is also a suffix). What is printed?
KMP failure function (lps[i] = length of the longest proper prefix of p[0..i] that is also a suffix). What is printed?
def lps(p):
l = [0] * len(p); k = 0
for i in range(1, len(p)):
while k and p[i] != p[k]: k = l[k - 1]
if p[i] == p[k]: k += 1
l[i] = k
return l
print(lps("aaabbaaaab"))- A. [0, 1, 2, 0, 0, 1, 2, 3, 1, 0]
- B. [0, 1, 0, 2, 0, 1, 2, 3, 3, 4]
- C. [0, 1, 2, 0, 0, 1, 2, 3, 3, 4]
- D. [0, 1, 2, 0, 0, 2, 1, 3, 3, 4]
Correct answer: C. [0, 1, 2, 0, 0, 1, 2, 3, 3, 4]
On a mismatch k falls back to lps[k − 1] (not straight to 0), which keeps partial matches alive. Result: [0, 1, 2, 0, 0, 1, 2, 3, 3, 4].
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
- The array [1, 2, 3, 4, 5, 6, 7, 8] is rotated RIGHT by 3 positions and then LEFT by 2 positions. What is the final array?
- Kadane's algorithm runs on [4, 0, 8, −3, −2, −2, −3, 2, −6]. What is the maximum sum of a non-empty contiguous subarray?
- How many characters must be deleted in total from two strings so that they become anagrams of each other? What is printed? python from…
- What does this loop-counting code print? (Python) python n, c = 33, 0 i = 1 while i < n: j = i while j < n: c += 1 j = 2 i += 1 print(c)
- Evaluate an infix expression of single digits with two stacks (operands and operators, usual precedence, left-associative). What is…
- One Lomuto partition (pivot = LAST element; i marks the end of the 'less than pivot' region; when a[j] < pivot, increment i and swap a[i]…