Problem 75: Testing Overlaps in a Binary Word |
The goal of the problem is to design an efficient algorithm to test whether a binary word contains an overlap factor. An overlap is a factor whose exponent is larger than $2$. A word contains an overlap if equivalently it has a factor of the form $auaua$, where $a$ is a letter and $u$ is a word.
The Thue-Morse word $\mu^\infty(\sa{a})$ is an example of an infinite overlap-free word. It is generated by the Thue-Morse morphism $\mu$ (defined by $\mu(\sa{a})=\sa{ab}$ and $\mu(\sa{b})=\sa{ba}$) that preserves overlap-freeness of words.
For a binary word $x$ we define its decomposition $uyv=x$, formally a triple $(u,y,v)$, in which $|u|$ is the smallest position on $x$ of a longest factor $y$ that belongs to $\{\sa{ab},\sa{ba}\}^+$. The decomposition is called a Restivo-Salemi (RS) factorisation if $u,v\in \{\mv,\sa{a},\sa{b},\sa{aa},\sa{bb}\}$. RS-factorisations are transformed into words in $\{\sa{ab},\sa{ba}\}^*$ by the partial functions $f$ or $g$ as follows ($c$ and $d$ are letters and the bar function exchanges letters $\sa{a}$ and $\sa{b}$): \[f(uyv)=\left\{\begin{array}{ll} y & \mbox{if } u=v=\mv \cr \bar{c}cy & \mbox{if } u=c \mbox{ or } cc \mbox{ and } v=\mv \cr yd\bar{d} & \mbox{if } u=\mv \mbox{ and } v=d \mbox{ or } dd \cr \bar{c}cyd\bar{d} & \mbox{if } u=c \mbox{ or } cc \mbox{ and } v=d \mbox{ or } dd \end{array}\right. \] \[g(uyv)=\left\{\begin{array}{ll} y & \mbox{if } u=v=\mv \cr \bar{c}cy & \mbox{if } u=c \mbox{ or } cc \mbox{ and } v=\mv \cr yd\bar{d} & \mbox{if } u=\mv \mbox{ or } c \mbox{ or } cc \mbox{ and } v=d \mbox{ or } dd \end{array}\right. \]
\begin{algorithmic} \WHILE{$|x|\gt 6$} \COMMENT{below $c$ and $d$ are letter variables} \STATE $uyv\leftarrow \mbox{RS-factorisation}(x)$ \IF{$uyv$ is not an RS-factorisation} \RETURN{False} \ENDIF \IF{$[u=cc$ \AND $(ccc$ \OR $cc\bar{c}cc\bar{c}c \mbox{ prefix of } uy)]$ \OR $[v=dd$ \AND $(ddd$ \OR $d\bar{d}dd\bar{d}dd \mbox{ suffix of } uy)]$} \RETURN{False} \ENDIF \IF{$(u=c$ \OR $u=cc)$ \AND $(v=d$ \OR $v=dd)$ \AND $uyv \mbox{ is a square}$} \STATE $x\leftarrow \mu^{-1}(g(uyv))$ \ELSE \STATE $x\leftarrow \mu^{-1}(f(uyv))$ \ENDIF \ENDWHILE \RETURN{True} \end{algorithmic}