File tree Expand file tree Collapse file tree 1 file changed +3
-3
lines changed
Contents/06.String/02.String-Single-Pattern-Matching Expand file tree Collapse file tree 1 file changed +3
-3
lines changed Original file line number Diff line number Diff line change @@ -32,17 +32,17 @@ RK 算法中的滚动哈希算法主要是利用了 **「Rabin fingerprint 思
32
32
33
33
比如 ` "cat" ` 的哈希值就可以表示为:
34
34
35
- $$ \begin{aligned} Hash(cat) &= c \times 26 \times 26 + a \times 26 + t \times 1 \cr &= 2 \times 26 \times 26 + 0 \times 26 + 19 \times 1 \cr &= 1371 \end{aligned} $$
35
+ $$ \begin{aligned} Hash(cat) &= c \times 26^2 + a \times 26^1 + t \times 26^0 \cr &= 2 \times 26^2 + 0 \times 26^1 + 19 \times 26^0 \cr &= 1371 \end{aligned} $$
36
36
37
37
这种按位计算哈希值的哈希函数有一个特点:在计算相邻子串时,可以利用上一个子串的哈希值。
38
38
39
39
比如说 $cat$ 的相邻子串为 ` "ate" ` 。按照刚才哈希函数计算,可以得出 ` "ate" ` 的哈希值为:
40
40
41
- $$ \begin{aligned} Hash(ate) &= a \times 26 \times 26 + t \times 26 + e \times 1 \cr &= 0 \times 26 \times 26 + 19 \times 26 + 4 \times 1 \cr &= 498 \end{aligned} $$
41
+ $$ \begin{aligned} Hash(ate) &= a \times 26^2 + t \times 26^1 + e \times 26^0 \cr &= 0 \times 26^2 + 19 \times 26^1 + 4 \times 26^0 \cr &= 498 \end{aligned} $$
42
42
43
43
如果利用上一个子串 ` "cat" ` 的哈希值计算 ` "ate" ` ,则 ` "ate" ` 的哈希值为:
44
44
45
- $$ \begin{aligned} Hash(ate) &= (Hash(cat) - c \times 26 \times 26) * 26 + e \times 26 \cr &= (1371 - 2 \times 26 \times 26 ) \times 26 + 4 \times 1 \cr &= 498 \end{aligned} $$
45
+ $$ \begin{aligned} Hash(ate) &= (Hash(cat) - c \times 26^2) \times 26 + e \times 26^0 \cr &= (1371 - 2 \times 26^2 ) \times 26 + 4 \times 26^0 \cr &= 498 \end{aligned} $$
46
46
47
47
可以看出,这两种方式计算出的哈希值是相同的。但是第二种计算方式不需要再遍历子串,只需要进行一位字符的计算即可得出整个子串的哈希值。这样每次计算子串哈希值的时间复杂度就降到了 $O(1)$。然后我们就可以通过滚动哈希算法快速计算出子串的哈希值了。
48
48
You can’t perform that action at this time.
0 commit comments