字符串哈希
字符串哈希
1. 哈希函数定义
$$ \begin{align} \text{hash}(n) & = \sum_{i=1}^{n} s_i \times B^{N-i} \mod M \\ & = \text{hash}(n-1) \times B + s_n \mod M \\ & = (s_1 \times B^{N-1} + s_2 \times B^{N-2} + \cdots + s_N) \mod M \end{align} $$
hash(l,r) = hash(r) − hash(l−1) × Br − l + 1 mod M
B = 131, M ∈ {998244353, 1000000007}
2. 例题
1. leetcode28 Find the Index of the First Occurrence in a String
python
1 | |
2. CF1200E Compress Words
I want to order pizza→Iwantorderpizzasample please ease in out→sampleaseinout
cpp
1 | |
字符串哈希
https://blog.fredbill.eu.org/algo/string/strhash/