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.
A chef prepared 6 different dishes, and the average rating for those dishes was 18 out of 100. For the next 4 dishes, the chef improved significantly, a...
The average of the first 7 non-zero multiples of 18 is:
The average of 5 numbers is 32. The sum of four of them is 120. What is the fifth number ?
Ram played his first 4 matches with an average of 5 goals per match. However, he couldn't score any goals in the following 3 matches. How many goals doe...
- The average of 6 values is 40. If two more values with an average of 'x' are added, the overall average increases by 20%. Find the value of 'x'.
What is the average of odd numbers greater than 10 but less than 30?
In a family of 8, the men eat on average 70 kg of food and women eat on an average 51 kg of food. The men and women are equal in number. A woman joined ...
The average age of a group of 10 students is 30 years. Two persons of age 40 years and 35 years, left the group and 2 persons of age 50 years and 95 yea...
Find the average of all the numbers between 9 and 31, which are divisible by 5?
The average of 25 numbers is 39.4. The average of the first 12 numbers is 36.5, and the average of the last 12 numbers is 40.5. Determine the value of t...