简体   繁体   English

Rabin Karp字符串匹配算法复杂度

[英]Rabin karp string matching algorithm complexity

在rabin算法中取模如何帮助降低原生horners规则字符串匹配的复杂性。

I guess by Horners rule you mean treating a string as a number in some base ("abcd" = 'a' * p^3 + 'b' * p^2 + 'c' * p^1 + 'd' * p^0) and comparing strings as numbers, and by Rabin algorithm you mean the essentially the same thing but modulo some other number. 我想根据霍纳斯规则,您的意思是将字符串当作某个基数中的数字来对待(“ abcd” ='a'* p ^ 3 +'b'* p ^ 2 +'c'* p ^ 1 +'d'* p ^ 0)并将字符串作为数字进行比较,通过拉宾算法,您的意思基本上是相同的,只是对其他数字取模。

The thing is using Horners rule you can only compare short strings -- otherwise you get overflow (you can use big integers to avoid it, but that is where you lose in complexity. The number corresponding to a string of length n will have O(n) digits, so arithmetic operations won't be done in O(1)). 事情是使用霍纳斯规则,您只能比较短字符串-否则会溢出(可以使用大整数来避免它,但这就是您在复杂度上降低的地方。对应于长度为n的字符串的数字将为O( n)数字,因此不会在O(1))中进行算术运算。

And in Rabin-Karp algorithm the numbers corresponding to our strings will stay small since we take them modulo some other number. 在Rabin-Karp算法中,与字符串相对应的数字将保持很小,因为我们将它们取模为其他数字。 It may cause collisions, but if we are lucky, collisions are rare enough. 它可能会引起碰撞,但是如果幸运的话,碰撞很少发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM