在 RK 算法的 Top Coder 代码中:
// correctly calculates a mod b even if a < 0
function int_mod(int a, int b)
{
return (a % b + b) % b;
}
function Rabin_Karp(text[], pattern[])
{
// let n be the size of the text, m the size of the
// pattern, B - the base of the numeral system,
// and M - a big enough prime number
if(n < m) return; // no match is possible
// calculate the hash value of the pattern
hp = 0;
for(i = 0; i < m; i++)
hp = int_mod(hp * B + pattern[i], M);
// calculate the hash value of the first segment
// of the text of length m
ht = 0;
for(i = 0; i < m; i++)
ht = int_mod(ht * B + text[i], M);
if(ht == hp) check character by character if the first
segment of the text matches the pattern;
// start the "rolling hash" - for every next character in
// the text calculate the hash value of the new segment
// of length m; E = (Bm-1) modulo M
for(i = m; i < n; i++) {
ht = int_mod(ht - int_mod(text[i - m] * E, M), M);
ht = int_mod(ht * B, M);
ht = int_mod(ht + text[i], M);
if(ht == hp) check character by character if the
current segment of the text matches
the pattern;
}
}
上面写着
不幸的是,仍有一些情况下,我们必须为文本中的每个起始位置运行“naive”方法的整个内部循环——例如,在字符串“aaaaaaaaaaaaaaaaaaaaaaaa”中搜索模式“aaa”时——所以在在最坏的情况下,我们仍然需要 (n * m) 次迭代。
但是算法不会在第一次迭代本身就停止 - 就像它会看到前三个字母是与针匹配的“a”一样?