Rabin-Karp 算法的实施的两种方式
原文,中说到两种实施RK算法的方式,一种是:总和法,另一个是累计求和法。这到底是什么意思?里面提到的一个前最高数位和最大乘子的积(product of the previously highest digit and the highest multipler)在这里是什么意思?
Our implementation of Rabin-Karp can be called in two ways, for computing either a total sum
or an incremental sum. A total sum is computed when the sum is returned at once for a whole
string: this is how the sum is computed for a pattern or for the $m first characters of the text.
The incremental method uses an additional trick: before bringing in the next character using
Horner's rule, it removes the contribution of the highest "digit" from the previous round by
subtracting the product of the previously highest digit and the highest multiplier, $hipow. In
other words, we strip the oldest character off the back and load a new character on the front.
This trick rids us of always having to compute the checksum of $m characters all over again.
Both the total and the incremental ways use Horner's rule.
[解决办法]
第一种就是在初始给出一个字符串的时候,计算一个值
第二种是在已有值的基础上,向后移动,计算下一个字符串的值
在计算时需要把现在串的最高位去掉,在后面追加新的位
http://www.javaeye.com/topic/287105,这篇文章里讲的很明白,建议还是看中文吧,呵呵