Also called KMP algorithm and Knuth-Morris-Pratt algorithm.
fresh numbers
The test's questions that a run answers, asked again on new numbers whenever you like. The answer, its working and the reason for every other choice come from knuth-morris-pratt's own run on the numbers shown.
choose one
1question 1
Knuth-Morris-Pratt looks for rrn in rrrrrrn. How many comparisons does it make, its table included?
the answer, and why
the answer is C
The table for rrn: r matches r, 1; n differs from r, falls back to nothing and differs from r again, 2. The search: r and r match, n is not r, 3. Then, 4 times, the 2 r's matched end with a border r, so the pattern moves on 1 place: its second r matches the same text r, and the next letter is compared, 2 each time. 3 + 3 + 8 is 14.
if you chose another
AThat counts the search but leaves out the table's 3.
BThat is brute force, 3 at each place: no table, but it compares the text's r's again.
Each search looks for imim in a text of eight letters. Which makes Knuth-Morris-Pratt compare the most, its table included?
the answer, and why
the answer is D
The method never goes back in the text, but a letter that differs is compared again after each fall back. mimiimmm, imim takes 12 comparisons, its table included, the most of the four.
if you chose another
AThat takes 9 comparisons, its table included, fewer than 12.
BThat takes 11 comparisons, its table included, fewer than 12.
CThat takes 9 comparisons, its table included, fewer than 12.