next up previous
Next: Pseudocode Up: CSE 2320: Algorithms and Previous: Prefix Array

Prefix Array

\(\pi[q]\) answers the question:

If we have matched P[1..q] in T, but P[q+1] does not match, then what is the longest prefix of P, P[1..k], that is a suffix of P[1..q]?

We can then start matching again from P[k+1].



\(\pi[q]\) = max{k $\mid$ k < q and P[1..k] is a suffix of P[1..q]}



Example


\begin{displaymath}P \;=\; \begin{array}{ccccc} 1 & 2 & 3 & 4 & 5 \\
y & o & y & o & s \\ \end{array}\end{displaymath}


\begin{displaymath}\pi \;=\; \begin{array}{ccccc} 1 & 2 & 3 & 4 & 5 \\
0 & 0 & 1 & 2 & 0 \\ \end{array}\end{displaymath}



Copyright © 1998 The University of Texas at Arlington