Hash Tables: String Search--Data Structure

本文介绍了两种字符串匹配算法:朴素算法和Rabin-Karp算法。朴素算法通过遍历目标字符串中的每个位置来检查是否与模式字符串相匹配。Rabin-Karp算法则使用哈希函数加速匹配过程,减少不必要的比较。此外,还讨论了如何通过预计算哈希值进一步优化算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Naive Algorithm

For each position i from 0 to |T| − |P|,check character-by-character whetherT[i..i+ |P| −1] = Por not. If yes, append i to the result. 


AreEqual(S1,S2)

if|S1|≠ |S2|:

  return False

for i from 0 to|S1|−1:

  if S1[i]̸=S2[i]:

  return False
return True

FindPatternNaive(T, P)

result empty list
for i from 0 to |T|−|P|:

  if AreEqual(T[i..i+|P|−1],P):

    result.Append(i)

return result

Rabin-Karp’s Algorithm 


RabinKarp(T, P)

pbig prime, xrandom(1,p1)

result empty list
pHash PolyHash(Ppx)
for i from 0 to |T|−|P|:

  tHash PolyHash(T[i..i+|P|−1]px)

  if pHash !=tHash:

    continue
  if AreEqual(T[i..i+|P|−1],P):

    result.Append(i)

return result


Improving 

PrecomputeHashes(T,|P|,p,x)

H array of length |T|−|P|+1

S T[|T| − |P|..|T| − 1]

H[|T|−|P|]PolyHash(S,p,x)y1

for i from 1 to|P|:

  y (y× x)mod p

for i from|T|−|P|−1 down to 0:
  H[i](xH[i+ 1] +T[i]yT[i+ |P|])mod p

return H

O(|P|+|P|+|T| − |P|) =O(|T|+ |P|

RabinKarp(T, P)

pbig prime, xrandom(1,p1)

result empty list
pHash PolyHash(P,p,x)
H PrecomputeHashes(T,|P|,p,x)

for i from 0 to|T|−|P|:

  if pHash ̸=H[i]:

    continue

  if AreEqual(T[i..i+|P|−1],P):

    result.Append(i)

return result

Improved Running Time

h(P)is computed in O(|P|)

Precompute Hashes runs in O(|T|+ |P|)
Total time spent in AreEqual is O(q|P|)on average where q is the number of occurrences ofP in T

Average running time O(|T|+ (q+ 1)|P|)

Usually q is small, so this is much less than O(|T||P|


Conclusion

Hash tables are useful for storing Sets and Maps
Possible to search and modify hash tables in O(1)on average!

Must use good hash families and randomization
Hashes are also useful while working with strings and texts

There are many more applications indistributed systems and data science 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值