Problem 42: Periods of Lyndon Word Prefixes |
A Lyndon word is a non-empty self-minimal word, that is, it is alphabetically smaller than all its non-empty proper suffixes. The dual self-maximal words share some of their features. Lyndon words have useful properties for the design of matching algorithms and for the analysis of methods such as testing the cyclic equivalence of two words (Problem 37). The present problem deals with a remarkable simple solution to compute their prefix periods.
Let $\tper$ denote the table of periods of prefixes of the word $x$. It is defined on non-empty prefix lengths $\ell$, $\ell=1,\dots,|x|$, by $$\tper[\ell] = \mbox{ smallest period of } x[0\dd \ell-1].$$ For the word $x = \sa{aabababba}$ we get
$i$ | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
$x[i]$ | $\sa{a}$ | $\sa{a}$ | $\sa{b}$ | $\sa{a}$ | $\sa{b}$ | $\sa{a}$ | $\sa{b}$ | $\sa{b}$ | $\sa{a}$ |
$\ell$ | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
$\tper[\ell]$ | 1 | 1 | 3 | 3 | 5 | 5 | 7 | 8 | 8 |
\begin{algorithmic} \STATE $period[1]\leftarrow 1$ \STATE $p\leftarrow 1$ \FOR{$\ell\leftarrow 2$ \TO $|x|$} \IF{$x[\ell-1] \neq x[\ell-1-p]$} \STATE $p\leftarrow \ell$ \ENDIF \STATE $period[\ell]\leftarrow p$ \ENDFOR \RETURN{$period$} \end{algorithmic}