Compute-Prefix-Function(P, m)
π[1..m] = {0}
π[1] = 0
k = 0
for q = 2 to m:
while k > 0 and P[k + 1] != P[q]:
k = π[k]
if P[k + 1] == P[q]:
k = k + 1
π[q] = k
return π
KMP-Matcher(T, P, n, m)
π = Compute-Prefix-Function(P, m)
q = 0
for i = 1 to n:
while q > 0 and P[q + 1] != T[i]:
q = π[q]
if P[q + 1] == T[i]:
q = q + 1
if q == m:
print("El patrón ocurre con desplazamiento " + (i - m))
q = π[q]