Question
In the Knuth-Morris-Pratt (KMP) algorithm, the Longest
Proper Prefix Suffix (LPS) array lps[] is crucial. When pat[i] and pat[len] match, len is incremented and lps[i] is set to len. What happens when pat[i] and pat[len] *do not* match and len is not 0? void computeLPSArray(char* pat, int M, int* lps) {   int len = 0;   lps[0] = 0;   int i = 1;   while (i < M) {     if (pat[i] == pat[len]) {       len++;       lps[i] = len;       i++;     } else { // (pat[i] != pat[len])       if (len != 0) {         _________; // Line to complete       } else { // if (len == 0)         lps[i] = 0;         i++;       }     }   } }Solution
• Concept: The Knuth-Morris-Pratt (KMP) algorithm uses a precomputed Longest Proper Prefix Suffix (LPS) array to efficiently search for a pattern within a text. The LPS array lps[i] stores the length of the longest proper prefix of pat[0...i] that is also a suffix of pat[0...i]. • Code Analysis: o The while loop builds the lps array. o When pat[i] == pat[len], len is incremented, and lps[i] is set to the new len. o When pat[i] != pat[len] and len != 0, it means the current character pat[i] does not extend the current prefix-suffix match. We need to "fall back" to a shorter possible prefix-suffix match. • Explanation of Correct Answer (B): len = lps[len - 1]; o When pat[i] does not match pat[len], and len is not 0, it means we had a prefix of length len that matched a suffix. Since pat[i] doesn't extend this match, we look for the next shortest proper prefix of pat[0...len-1] that is also a suffix of pat[0...len-1]. The length of this next shortest match is precisely stored in lps[len - 1]. By setting len = lps[len - 1], we effectively shift our comparison point in the pattern to try and find a shorter matching prefix.
Which initiative was launched by Atal Innovation Mission NITI Aayog in collaboration with the Innovation Centre Denmark (ICDK) at the Royal Danish Emba...
The Digital India Programme is an ambitious initiative launched by the Government of India on July 1, 2015, with the aim of transforming India into a di...
Which of the following are key focus areas of the Skill India Programme?
1.        Vocational training
2.        En...
Which of the following is not one of the beneficiaries of PMMSY?
NABARD is entrusted with the task of acting as the National Implementing Agency (NIE) of which of the following funds?
In the Union Budget 2025–26, the fiscal deficit target for FY26 is set at 4.4% of GDP. From a fiscal policy standpoint, this target primarily reflects...
Under the PM Awas Yojana Urban 2.0, what is the primary mechanism through which the government plans to facilitate affordable housing loans?
What is the primary purpose of the Performance Grading Index for Districts (PGI-D) report?Â
Consider the following statements regarding the Skill India Programme:
1.        It aims to provide training in various vocational sk...
What is the objective of PM krishi sinchayee Yojana, which was launched in 2015-16.
(i)Â Â Â Â Â Â To achieve convergence of investments in i...