查看 KMP 算法,并对 KMP 中计算后缀-前缀计数表的特定行感到困惑。
algorithm kmp_table:输入:一个字符数组,W(要分析的单词)一个整数数组,T(要填充的表格)输出:什么都没有(但在操作期间,它填充表格)
define variables:
an integer, pos ← 2 (the current position we are computing in T)
an integer, cnd ← 0 (the zero-based index in W of the next
character of the current candidate substring)
(the first few values are fixed but different from what the algorithm
might suggest)
let T[0] ← -1, T[1] ← 0
while pos is less than the length of W, do:
(first case: the substring continues)
if W[pos - 1] = W[cnd],
let cnd ← cnd + 1, T[pos] ← cnd, pos ← pos + 1
(second case: it doesn't, but we can fall back)
otherwise, if cnd > 0, let cnd ← T[cnd]
(third case: we have run out of candidates. Note cnd = 0)
otherwise, let T[pos] ← 0, pos ← pos + 1
以上直接取自维基百科。cnd > 0
如果为什么 set ,我有点困惑cnd := T[cnd]
,不应该将 cnd 重置回 0 就好像我们重新开始一样?