Lecture 9: Hashing II: Table Doubling,Karp-Rabin
散列表应该有多大?
理想状态:
根据需要改变大小.
(重散列)Rehashing
增长:
散列表长度成倍增长是个好选择.
删除:
字符串匹配(String Matching)
Simple Algorithm:
Karp-Rabin Algorithm:
python代码:
def karp_rabin(T, P):
n = len(T)
m = len(P)
d = 256
q = 101
h = d ** (m - 1) % q
p = 0
t = 0
for i in range(m):
p = (d*p + ord(P[i])) % q
t = (d*t + ord(T[i])) % q
for s in range(n - m + 1):
print(s,p,t)
if p == t and T[s:s+m] == P:
return s
if s < n - m:
t = ( d * ( t - ord( T[s] ) * h ) + ord( T[s + m] ) ) % q
来源:oschina
链接:https://my.oschina.net/u/106593/blog/604576