KMP算法伪码

来源:互联网 发布:淘宝西铁城手表真假 编辑:程序博客网 时间:2024/06/03 11:28

KMP-MATCHER(T, P)n <- length[T]m <- length[P]pi <- COMPUTE-PREFIX-FUNCTION(P)  // pi is just the pattern of P stringq <- 0for i <- 1 to n     do while q > 0 and P[q+1] != T[i]        do q <- pi[q]    if P[q+1] = T[i]        then q <- q+1    if q = m        then print "pattern occurs with shift" i-m        q <- pi[q]

COMPUTE-PREFIX-FUNCTION(P)m <- length[P]pi[1] <- 0k <- 0for q <- 2 to m     do while  k > 0 and P[k+1] != P[q]        do k <- pi[k]    if P[k+1] = P[i]        then k <- k+1    pi[q] <- kreturn pi




0 0
原创粉丝点击