Write RabinKarp. The first line is a modulus p; the second line is the text; each following line is a pattern. For each pattern of length m compute its hash (c₀·31^(m−1) + c₁·31^(m−2) + … + c_(m−1)) mod p, then slide a window over the text, updating the window's hash in O(1) per step (remove the leading character, multiply by 31, add the new character). Whenever the hashes are equal, verify by comparing the characters.
Print the positions found, the number of windows, of hash matches and of spurious matches (equal hash, different text).
For the input
13
the quick brown fox jumps over the lazy dog
the
fox
cat
the program prints
"the" (hash 11): found at [0, 31]
windows 41, hash matches 4, spurious 2
"fox" (hash 1): found at [16]
windows 41, hash matches 4, spurious 3
"cat" (hash 8): found at []
windows 41, hash matches 4, spurious 4