6 questions. Covers Rolling hash in Python.
Questions 1 to 5 of 6
RollingHash
hash(start, end)
python class RollingHash: def __init__(self, text, base, mod): self.mod = mod self.prefix = [0] * (len(text) + 1) self.power = [1] * (len(text) + 1) for i, ch in enumerate(text): self.prefix[i + 1] = (self.prefix[i] * base + ord(ch)) % mod self.power[i + 1] = (self.power[i] * base) % mod def hash(self, start, end): return (self.prefix[end] - self.prefix[start] * self.power[end - start]) % self.mod
hash
% self.mod
(-7) % 5
python rh = RollingHash('abcab', 10, 1_000_000_007) print(rh.hash(3, 5)) print(rh.hash(0, 3)) print(rh.hash(3, 5) == rh.hash(0, 2))
5 question(s) left on this page.