from Introduction to Algorithm
KMP-MATCHER(T,P)
n = T.length m = P.length pi = COMPUTE-PREFIX-FUNCTION(P) for i = 1 to n while q>0 and P[q+1] != T[i] q = pi[q] if P[q+1] == T[i] q
= q + 1 if q == m print " Pattern occurs with shift" i-m q
= pi[q]
COMPUTE-PREFIX-FUNCTION(P)
m = P.lengthlet pi[m] be a new arraypi[1]
= 0k = 0for q = 2 to m while
k > 0 && P[k+1] != P[q] k = pi[q] if P[k+1] == P[q] k
= k + 1 pi[q] = kreturn pi
i.e
let P = "ababbabbabbababbabb"
then pi = [0, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 3, 4, 5, 6, 7, 8]
1337

被折叠的 条评论
为什么被折叠?



