假设此时 $pat$ 向下滑动的 $k$ 个字符(也即 $pat$ 末尾端的 $subpat$ 与其最右边的合理重现的距离),这样我们的注意力应该沿着 $string$ 向后滑动 $k+m$ 个字符,这段距离我们称之为 $delta_2(j)$ :
-假定 $rpr(j)$ 为 $subpat=pat[j+1\dots patlastpos]$ 在 $pat[j]$ 上失配时的最右边合理重现的位置,$rpr(j) \lt j$(这里只给出简单定义,在下文的算法设计章节里会有更精确的讨论),那么显然 $k=j-rpr(j),\ m=patlastpos-j$ 。
+假定 $rpr(j)$ 为 $subpat=pat[j+1\dots patlastpos]$ 在 $pat[j]$ 上失配时的最右边合理重现的位置,$rpr(j) < j$(这里只给出简单定义,在下文的算法设计章节里会有更精确的讨论),那么显然 $k=j-rpr(j),\ m=patlastpos-j$ 。
所以有:
原因是 $pat[j]$ 本身是失配字符,所以 $pat$ 向下滑动 $k$ 个字符后,在后缀匹配过程中仍然会在 $pat[k-1]$ 处失配。
还要注意两个限制条件:
-1. $k \lt j$ 。因为当 $k=j$ 时,有 $pat[k]=pat[j]$ , 在 $pat[j]$ 上失配的字符也会在 $pat[k]$ 上失配。
+1. $k < j$ 。因为当 $k=j$ 时,有 $pat[k]=pat[j]$ , 在 $pat[j]$ 上失配的字符也会在 $pat[k]$ 上失配。
2. 考虑到 $delta_2(patlastpos)= 0$ ,所以规定 $rpr(patlastpos) = patlastpos$ 。
由于理解 $rpr(j)$ 是实现 BoyerMoore 算法的核心,所以我们使用如下两个例子进行详细说明:
下面若干样例展示了对于不同字符串的 Z 函数:
-- $z(\mathtt{aaaaa}) = [0, 4, 3, 2, 1]$
-- $z(\mathtt{aaabaab}) = [0, 2, 1, 0, 2, 1, 0]$
-- $z(\mathtt{abacaba}) = [0, 0, 1, 0, 3, 0, 1]$
+- $z(\mathtt{aaaaa}) = [0, 4, 3, 2, 1]$
+- $z(\mathtt{aaabaab}) = [0, 2, 1, 0, 2, 1, 0]$
+- $z(\mathtt{abacaba}) = [0, 0, 1, 0, 3, 0, 1]$
## 朴素算法
## 复杂度分析
-对于内层 `while` 循环,每次执行都会使得 $r$ 向后移至少 $1$ 位,而 $r\lt n-1$ ,所以总共只会执行 $n$ 次。
+对于内层 `while` 循环,每次执行都会使得 $r$ 向后移至少 $1$ 位,而 $r< n-1$ ,所以总共只会执行 $n$ 次。
对于外层循环,只有一遍线性遍历。
## 练习题目
-- [CF126B Password](http://codeforces.com/problemset/problem/126/B)
-- [UVA # 455 Periodic Strings](http://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=396)
-- [UVA # 11022 String Factoring](http://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=1963)
-- [UVa 11475 - Extend to Palindrome](http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2470)
-- [LA 6439 - Pasti Pas!](https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=588&page=show_problem&problem=4450)
-- [Codechef - Chef and Strings](https://www.codechef.com/problems/CHSTR)
-- [Codeforces - Prefixes and Suffixes](http://codeforces.com/problemset/problem/432/D)
+- [CF126B Password](http://codeforces.com/problemset/problem/126/B)
+- [UVA # 455 Periodic Strings](http://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=396)
+- [UVA # 11022 String Factoring](http://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=1963)
+- [UVa 11475 - Extend to Palindrome](http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2470)
+- [LA 6439 - Pasti Pas!](https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=588&page=show_problem&problem=4450)
+- [Codechef - Chef and Strings](https://www.codechef.com/problems/CHSTR)
+- [Codeforces - Prefixes and Suffixes](http://codeforces.com/problemset/problem/432/D)
* * *
- **本页面主要译自博文 [Z-функция строки и её вычисление](http://e-maxx.ru/algo/z_function) 与其英文翻译版 [Z-function and its calculation](https://cp-algorithms.com/string/z-function.html) 。其中俄文版版权协议为 Public Domain + Leave a Link;英文版版权协议为 CC-BY-SA 4.0。**
+ **本页面主要译自博文 [Z-функция строки и её вычисление](http://e-maxx.ru/algo/z_function) 与其英文翻译版 [Z-function and its calculation](https://cp-algorithms.com/string/z-function.html) 。其中俄文版版权协议为 Public Domain + Leave a Link;英文版版权协议为 CC-BY-SA 4.0。**