|
Problem 23: Border Table to Maximal Suffix |
|
The maximal suffix of a word, its alphabetically greatest suffix, helps designing optimal text searches and periodicity tests. The problem introduces a computation of it based on the border table algorithm.
\begin{algorithmic}
\STATE $ms \leftarrow 0$
\STATE $border[0] \leftarrow -1$
\FOR{$i \leftarrow 0$ \TO $|x|-1$}
\STATE $\ell \leftarrow border[i-ms]$
\WHILE{$\ell\geq 0$ \AND $x[ms+\ell]\neq x[i]$}
\IF{$x[ms+\ell] \lt x[i]$}
\STATE $ms \leftarrow {i-\ell}$
\ENDIF
\STATE $\ell \leftarrow border[\ell]$
\ENDWHILE
\STATE $border[i-ms+1] \leftarrow \ell+1$
\ENDFOR
\RETURN $ms$
\end{algorithmic}
The following pictures show how the maximal suffix evolves according to the appended letter $\sa{a}$, $\sa{b}$, $\sa{c}$ or $\sa{d}$. Note that if the periodicity of the maximal suffix changes, the maximal suffix is of the form $va$ where $v$ is a border of the initial maximal suffix and $a$ is the new letter.