【发布时间】:2013-08-06 13:31:58
【问题描述】:
这是我在 ItoA 中看到的伪代码:
1 m = P.length
2 let pi[1...m] be a new array
3 pi[1] = 0
4 k=0
5 for q=2 to m
6 while k > 0 and P[k+1] != P[q]
7 k = pi[k]
8 if P[k+1] == P[q]
9 k = k+1
10 pi[q] = k
11 return pi
我的疑问是为什么在第 6 行我们使用k = pi[k] 而不是k--,这在我看来应该是检查长度为k 的前缀的方式(因为如果P[k+1] != P[q] 表示前缀为lenght k+1 那也是后缀不能实现)也可以是后缀,就是和P[q]比较,我也觉得如果我们这样做,运行时间会保持不变。
【问题讨论】:
标签: preprocessor string-matching prefix knuth-morris-pratt