Introduction to Algorithms (Rolling Hashes, Amorted Analysis)

Rolling Hashes

We have a big document, aka a long string, and we're trying to find a small string inside it.

N: "the fox is in the hat"

k: "the" -> h(k)

w: "the"

        h(w) == h(k) && w == k

h

input: 1000000 character string

output:  2^32

Rolling Hash

  • pop()
  • append(x)
  • slide(new, old): n = (n*base - old*base^k + old) mod p = ((n mod p)*base - old*(base^k mod p) + new) mod p
  • precompute (base^k mod p)

猜你喜欢

转载自blog.csdn.net/Da_tianye/article/details/82222264