Rabin-Karp looks for ate in abdicate. The fingerprints agree only at places 2 and 6. How many letter comparisons does it make?
the answer, and why
the answer is B
Place 2, bdi: b is not a, 1. Place 6, ate: a, t and e match, 3. No other place compares. That is 4.
if you chose another
AThat compares all 3 letters at both places, but place 2 stops at its first letter.
CThat counts only the match, not the chance agreement at place 2.
DThat compares all 3 letters at all 6 places, the most any search of 8 letters and 3 could make.
2question 2
Each text has 10 letters and each pattern 5. Which makes Rabin-Karp compare the fewest letters?
the answer, and why
the answer is B
aabaa is aaaaa plus 676: its middle letter, worth 26 times 26, is b (2), not a (1). 676 leaves 5, so aabaa leaves 5, never aaaaa's 0. No place agrees, and no letter is compared.
if you chose another
Aaaaaa and bbbbb both leave 0, so every place agrees by chance and compares 1 letter, 6 in all.
Cbabab matches at places 2, 4 and 6, 5 letters each, and no other place agrees, so 15.
Dccccc's number is 3 times aaaaa's, so it leaves 0 too: every place agrees by chance and compares 1 letter, 6 in all.
3question 3
A text has 1,200 letters, all a. Rabin-Karp searches it for 600 a's. How many steps, fingerprints plus comparisons?
the answer, and why
the answer is B
1,200 - 600 + 1 is 601 places, so 602 fingerprints with the pattern's. Every place matches, 600 comparisons each, 360,600. Together 361,202.
B1 means no growth, but twice the text gives about twice the places, each one a fingerprint.
DThat is how a count growing as times times grows.
pen and paper
5question 5about 6 minutes
Rabin-Karp searches addenda for den. den's fingerprint is 10. The places' fingerprints, in order, are 3, 8, 10, 8 and 10.
At which places are letters compared, and how many at each?
Where does den stand?
How many steps, fingerprints plus comparisons, in all?
a hint
Compare letters only where a place's fingerprint is 10, from the left, until one differs or all 3 match.
a worked answer
Places 3 and 5 have 10. Place 3, den: d, e and n match, 3. Place 5, nda: n is not d, 1, an agreement by chance. So den stands at place 3. Fingerprints: den's and the 5 places', 6. Comparisons: 3 + 1, which is 4. Steps: 6 + 4, which is 10.
place
letters above
fingerprint
comparisons
1
add
3
0
2
dde
8
0
3
den
10
3, a match
4
end
8
0
5
nda
10
1, by chance
den under each place of addenda. Only places 3 and 5 share den's 10.
A text holds 1,001 letters, all a. The pattern has half as many, rounded up.
How many letters has the pattern, and how many fingerprints are worked out?
How many steps if it is a's ending in b?
How many if it is all a?
a hint
Questions 1 and 2 of the learn page's section 4 count each.
a worked answer
Half of 1,001 rounded up is 501 letters, so 1,001 - 501 + 1, which is 501 places, and 502 fingerprints with the pattern's. a's ending in b: no place agrees, 502 steps. All a: 501 comparisons at each of 501 places, 251,001, plus the 502 fingerprints, 251,503.
Question 2: how many steps, , on a text of a's and a pattern of a's half as long, rounded up?10 of 10 shown
1the move: a place's most
the line:
why it is allowed: Letters are compared only where the fingerprints agree, from the left, until one differs. The pattern has letters, so at most .
2the move: every place agrees and matches
the line:
place
above
fingerprint
compared
1
aaa
10
3
2
aaa
10
3
3
aaa
10
3
4
aaa
10
3
all
5: 4 places and aaa's
12
A text of 6 a's and the pattern aaa. Every fingerprint is 10, and every place is a match. 5 fingerprints and 12 comparisons.
why it is allowed: Every place has a's above it, the pattern's own letters, so its fingerprint too. Every place agrees, and all letters match.
guess it, then
3the move: multiply by the places
the line:
why it is allowed: Section 4's question 1, lines 1, 3 and 6, give places. This question's line 2 gives comparisons at each. For 6, 4 times 3, which is 12.
guess it, then
4the move: add the fingerprints
the line:
why it is allowed: A step is a fingerprint or a comparison. Section 4's question 1, lines 2, 3 and 6, give fingerprints, whatever the letters. For 6, 5 + 12, which is 17; for 7, 5 + 16, which is 21.
guess it, then
5the move: gather the places
the line:
why it is allowed: Line 4's is the places, , plus 1. The places taken times and once more make times, and the 1 stays. For 7, 5 + 16 is 4 + 1 + 4 times 4, which is 4 times 5, plus 1, 21.
guess it, then
6the move: a ceiling
the line:
why it is allowed: From 1 up, half of rounded up is at most , so plus 1 at most . Half rounded down is a whole number below , so plus 1 at most . Their product is at most twice , and the last 1 at most . For 6, 17 against 108.
guess it, then
7the move: read the bound
the line:
why it is allowed: By line 6, with the fixed multiple 3.
guess it, then
8the move: a floor
the line:
why it is allowed: Rounding up never makes a number smaller, so line 5's is more than . Rounding down takes off less than 1, so is more than too. Multiply, as times is ; the last 1 only adds. For 6, 17 against 9.
guess it, then
9the move: read the bound
the line:
why it is allowed: By line 8, with the fixed multiple a quarter.
guess it, then
10the move: both together
the line:
why it is allowed: Lines 7 and 9 bound by the same growth.
guess it, then
try it with a small : at , line 4 gives , which you can work out by hand