Let $A_P = (Q,\Sigma,\delta,0,\{m\})$ the string matching automaton for pattern $P \in \Sigma^m$, that is
- $Q = \{0,1,\dots,m\}$
- $\delta(q,a) = \sigma_P(P_{0,q}\cdot a)$ for all $q\in Q$ and $a\in \Sigma$
with $\sigma_P(w)$ the length of the longest prefix of $P$ that is a Suffix of $w$, that is
$\qquad \displaystyle \sigma_P(w) = \max \left\{k \in \mathbb{N}_0 \mid P_{0,k} \sqsupset w \right\}$.
Now, let $\pi$ the prefix function from the Knuth-Morris-Pratt algorithm, that is
$\qquad \displaystyle \pi_P(q)= \max \{k \mid k < q \wedge P_{0,k} \sqsupset P_{0,q}\}$.
As it turns out, one can use $\pi_P$ to compute $\delta$ quickly; the central observation is:
Assume above notions and $a \in \Sigma$. For $q \in \{0,\dots,m\}$ with $q = m$ or $P_{q+1} \neq a$, it holds that
$\qquad \displaystyle \delta(q,a) = \delta(\pi_P(q),a)$
But how can I prove this?
For reference, this is how you compute $\pi_P$:
m ← length[P ]
π[0] ← 0
k ← 0
for q ← 1 to m − 1 do
while k > 0 and P [k + 1] =6 P [q] do
k ← π[k]
if P [k + 1] = P [q] then
k ← k + 1
end if
π[q] ← k
end while
end for
return π