|
ADS
Algorithms and Data Structures
|
the eertree (palindromic tree) of a string, built a byte at a time More...
#include <ADS/string/Palindromes.h>
Public member functions | |
| Eertree (void) | |
| the eertree of the empty string: the two roots, of length \( -1 \) and 0 | |
| Eertree (std::string_view s) | |
| the eertree of \( s \), by add() on each of its bytes | |
| bool | add (unsigned char c) |
| adds the byte \( c \) at the end; whether the longest palindromic suffix of the new string occurs there for the first time | |
| std::size_t | distinct (void) const |
| the number of the distinct nonempty palindromes of the string | |
| std::size_t | suffixLength (void) const |
| the length of the longest palindromic suffix of the string | |
| std::size_t | size (void) const |
| the length \( n \) of the string | |
Friends | |
| std::vector< std::string > | palindromicFactorization (std::string_view s) |
| the fewest palindromes into which \( s \) can be cut, in their order, by the series of the eertree, in \( O( n \log n ) \); none for the empty \( s \) | |
the eertree (palindromic tree) of a string, built a byte at a time
| ADS::Eertree::Eertree | ( | void | ) |
the eertree of the empty string: the two roots, of length \( -1 \) and 0
|
explicit |
the eertree of \( s \), by add() on each of its bytes
| bool ADS::Eertree::add | ( | unsigned char | c | ) |
adds the byte \( c \) at the end; whether the longest palindromic suffix of the new string occurs there for the first time
In pseudocode (Rubinchik and Shur, beyond the book), \( \mathrm{ext}( v , c ) \) the first palindromic suffix on the links from \( v \) that is preceded by \( c \), and \( \mathit{last} \) the longest palindromic suffix:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{add}( c ) \cr 2 & \quad \text{append } c \text{ to } s;\ v \gets \mathrm{ext}( \mathit{last} , c ) \cr 3 & \quad \textbf{if}\ v \text{ has the edge } c \text{ to } w \ \textbf{then}\ \mathit{last} \gets w;\ \textbf{return false} \cr 4 & \quad u \gets \text{a new node};\ \mathit{len}( u ) \gets \mathit{len}( v ) + 2 \cr 5 & \quad \textbf{if}\ \mathit{len}( u ) = 1\ \textbf{then} \ \mathit{link}( u ) \gets \text{the root of length } 0 \cr 6 & \quad \textbf{else}\ \mathit{link}( u ) \gets \text{the child } c \text{ of } \mathrm{ext}( \mathit{link}( v ) , c ) \cr 7 & \quad \mathit{diff}( u ) \gets \mathit{len}( u ) - \mathit{len}( \mathit{link}( u ) ) \cr 8 & \quad \mathit{series}( u ) \gets \mathit{series}( \mathit{link}( u ) ) \text{ if } \mathit{diff}( u ) = \mathit{diff}( \mathit{link}( u ) ) \text{, else } \mathit{link}( u ) \cr 9 & \quad \text{add the edge } c \text{ from } v \text{ to } u; \ \mathit{last} \gets u;\ \textbf{return true} \end{array}} \]
|
inlinenodiscard |
the number of the distinct nonempty palindromes of the string
|
inlinenodiscard |
the length \( n \) of the string
|
inlinenodiscard |
the length of the longest palindromic suffix of the string
|
friend |
the fewest palindromes into which \( s \) can be cut, in their order, by the series of the eertree, in \( O( n \log n ) \); none for the empty \( s \)
In pseudocode (Fici, Gagie, Kärkkäinen and Kempa, and Rubinchik and Shur, beyond the book), \( \mathit{best}( v ) \) the least \( D \) before a palindrome of the series of \( v \) that ends at \( i \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{palindromicFactorization}( s ) \cr 2 & \quad E \gets \text{an empty eertree};\ D[ 0 ] \gets 0 \cr 3 & \quad \textbf{for}\ i \gets 1\ \textbf{to}\ n\ \textbf{do} \cr 4 & \qquad E.\mathrm{add}( s[ i - 1 ] );\ D[ i ] \gets \infty \cr 5 & \qquad \textbf{for}\ v \gets \mathit{last}\ \textbf{while} \ \mathit{len}( v ) > 0\ \textbf{do}\ v \gets \mathit{series}( v ) \cr 6 & \qquad\quad L \gets \mathit{len}( \mathit{series}( v ) ) + \mathit{diff}( v );\ \mathit{best}( v ) \gets D[ i - L ] \cr 7 & \qquad\quad \textbf{if}\ \mathit{diff}( v ) = \mathit{diff}( \mathit{link}( v ) )\ \textbf{then}\ \mathit{best}( v ) \gets \min\{ \mathit{best}( v ) , \mathit{best}( \mathit{link}( v ) ) \} \cr 8 & \qquad\quad D[ i ] \gets \min\{ D[ i ] , \mathit{best}( v ) + 1 \} \cr 9 & \quad \textbf{return}\ \text{the pieces read back from } n \end{array}} \]