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.length
let pi[m] be a new array
pi[1] = 0
k = 0
for 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] = k
return 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]