|
ADS
Algorithms and Data Structures
|
Three classic problems on the palindromes of a sequence \( s \) of \( n \) items, and the eertree. More...
Files | |
| file | ADS/string/Palindromes.h |
| Three classic problems on the palindromes of a sequence \( s \) of \( n \) items, and the eertree. | |
Classes | |
| class | ADS::Eertree |
| the eertree (palindromic tree) of a string, built a byte at a time More... | |
Functions | |
| template<class S> requires std::is_class_v< S > | |
| std::pair< std::size_t, std::size_t > | ADS::longestPalindrome (const S &s) |
| where a longest palindromic substring of \( s \) starts and its length, the leftmost one among the longest; \( ( 0 , 0 ) \) for the empty \( s \) | |
| template<class S> requires std::is_class_v< S > | |
| S | ADS::longestPalindromicSubstring (const S &s) |
| the leftmost of the longest palindromic substrings of \( s \) | |
| std::string | ADS::longestPalindromicSubstring (const std::string &s) |
| the substring of longestPalindromicSubstring() above, on a std::string (or a literal) | |
| template<class S> requires std::is_class_v< S > | |
| S | ADS::longestPalindromicSubsequence (const S &s) |
| a longest palindromic subsequence of \( s \) | |
| std::string | ADS::longestPalindromicSubsequence (const std::string &s) |
| the subsequence of longestPalindromicSubsequence() above, on a std::string (or a literal) | |
| template<class S> requires std::is_class_v< S > | |
| std::vector< S > | ADS::palindromicPartition (const S &s) |
| the fewest palindromes into which \( s \) can be cut, in their order; none for the empty \( s \) | |
| std::vector< std::string > | ADS::palindromicPartition (const std::string &s) |
| the pieces of palindromicPartition() above, on a std::string (or a literal) | |
| std::vector< std::string > | ADS::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 \) | |
Three classic problems on the palindromes of a sequence \( s \) of \( n \) items, and the eertree.
The definitions, the invariants and the costs of the area are in Theory of strings.
|
nodiscard |
where a longest palindromic substring of \( s \) starts and its length, the leftmost one among the longest; \( ( 0 , 0 ) \) for the empty \( s \)
In pseudocode (Manacher's algorithm, beyond the book), \( [ l , r ) \) the rightmost palindrome found so far, from which a center inside it starts with the radius of its mirror; \( r \) only moves forward, hence \( \Theta( n ) \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{longestPalindrome}( s ) \cr 2 & \quad l \gets 0;\ r \gets 0\quad \triangleright\ \text{the odd } \text{palindromes, centered at } i \cr 3 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ n - 1\ \textbf{do} \cr 4 & \qquad \textbf{if}\ i < r\ \textbf{then}\ k \gets \min\{ d_1[ l + r - 1 - i ] , r - i \}\ \textbf{else}\ k \gets 1 \cr 5 & \qquad \textbf{while}\ i + k < n\ \textbf{and}\ i \ge k \ \textbf{and}\ s[ i - k ] = s[ i + k ]\ \textbf{do}\ k \gets k + 1 \cr 6 & \qquad d_1[ i ] \gets k;\ \textbf{if}\ i + k > r\ \textbf{then}\ l \gets i + 1 - k;\ r \gets i + k \cr 7 & \qquad \text{keep } ( i + 1 - k , 2 k - 1 ) \cr 8 & \quad l \gets 0;\ r \gets 0\quad \triangleright\ \text{the even } \text{palindromes, right half from } i \cr 9 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ n - 1\ \textbf{do} \cr 10 & \qquad \textbf{if}\ i < r\ \textbf{then}\ k \gets \min\{ d_2[ l + r - i ] , r - i \}\ \textbf{else}\ k \gets 0 \cr 11 & \qquad \textbf{while}\ i + k < n\ \textbf{and}\ i \ge k + 1 \ \textbf{and}\ s[ i - k - 1 ] = s[ i + k ]\ \textbf{do}\ k \gets k + 1 \cr 12 & \qquad d_2[ i ] \gets k;\ \textbf{if}\ i + k > r\ \textbf{then}\ l \gets i - k;\ r \gets i + k \cr 13 & \qquad \textbf{if}\ k > 0\ \textbf{then}\ \text{keep } ( i - k , 2 k ) \cr 14 & \quad \textbf{return}\ \text{the leftmost of the longest kept} \end{array}} \]
|
nodiscard |
a longest palindromic subsequence of \( s \)
In pseudocode (chapter 13), the table filled by decreasing \( i \) and read back from both ends:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{longestPalindromicSubsequence}( s )\quad \triangleright\ \mathit{DP}[ i ][ j ] \text{ the longest in } s_i \cdots s_j \cr 2 & \quad \textbf{for}\ i \gets n - 1\ \textbf{downto}\ 0\ \textbf{do} \cr 3 & \qquad \mathit{DP}[ i ][ i ] \gets 1 \cr 4 & \qquad \textbf{for}\ j \gets i + 1\ \textbf{to}\ n - 1\ \textbf{do} \cr 5 & \qquad\quad \mathit{DP}[ i ][ j ] \gets 2 + \mathit{DP}[ i + 1 ][ j - 1 ] \text{ if } s_i = s_j \text{ (an empty range counts } 0 \text{),} \cr & \qquad\qquad \text{else } \max\{ \mathit{DP}[ i ][ j - 1 ] , \mathit{DP}[ i + 1 ][ j ] \} \cr 6 & \quad ( i , j ) \gets ( 0 , n - 1 );\ L \gets ()\quad \triangleright \ \text{the left half, read back from the table} \cr 7 & \quad \textbf{while}\ i < j\ \textbf{do} \cr 8 & \qquad \textbf{if}\ s_i = s_j\ \textbf{then}\ \text{append } s_i \text{ to } L;\ ( i , j ) \gets ( i + 1 , j - 1 ) \cr 9 & \qquad \textbf{else if}\ \mathit{DP}[ i ][ j - 1 ] > \mathit{DP}[ i + 1 ][ j ]\ \textbf{then}\ j \gets j - 1\ \textbf{else}\ i \gets i + 1 \cr 10 & \quad \textbf{return}\ L \text{, then } s_i \text{ if } i = j \text{, then } L \text{ reversed} \end{array}} \]
|
inlinenodiscard |
the subsequence of longestPalindromicSubsequence() above, on a std::string (or a literal)
|
nodiscard |
the leftmost of the longest palindromic substrings of \( s \)
In pseudocode (Manacher's algorithm, beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{longestPalindromicSubstring}( s ) \cr 2 & \quad ( a , \ell ) \gets \mathrm{longestPalindrome}( s ); \ \textbf{return}\ s[ a .. a + \ell - 1 ] \end{array}} \]
|
inlinenodiscard |
the substring of longestPalindromicSubstring() above, on a std::string (or a literal)
|
inlinenodiscard |
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}} \]
|
nodiscard |
the fewest palindromes into which \( s \) can be cut, in their order; none for the empty \( s \)
In pseudocode (chapter 13, with the table of the palindromes replaced by the expansion about the centers), the centers by increasing \( h = a + b - 1 \) for the palindrome \( s[ a .. b ) \), so that \( D[ a ] \) is final when it is used:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{palindromicPartition}( s )\quad \triangleright\ D[ b ] \text{ the fewest pieces of } s_0 \cdots s_{b-1} \cr 2 & \quad D[ 0 ] \gets 0;\ D[ b ] \gets \infty \text{ for } b > 0 \cr 3 & \quad \textbf{foreach}\ \text{center } h = 0 , \ldots , 2 n - 2 \text{, between letters when } h \text{ is odd}\ \textbf{do} \cr 4 & \qquad [ a , b ) \gets \text{the shortest palindrome about } h \text{ (none if } h \text{ is odd and its two letters differ)} \cr 5 & \qquad \textbf{loop}\quad \triangleright \ \text{each palindrome about } h \text{, growing by one letter on each side} \cr 6 & \qquad\quad \textbf{if}\ D[ a ] + 1 < D[ b ]\ \textbf{then}\ ( D[ b ] , \mathit{from}[ b ] ) \gets ( D[ a ] + 1 , a ) \cr 7 & \qquad\quad \textbf{if}\ a > 0 \text{ and } b < n \text{ and } s_{a - 1} = s_b\ \textbf{then}\ ( a , b ) \gets ( a - 1 , b + 1 ) \ \textbf{else}\ \textbf{break} \cr 8 & \quad \textbf{return}\ \text{the pieces found by following } \mathit{from} \text{ back from } n \end{array}} \]
|
inlinenodiscard |
the pieces of palindromicPartition() above, on a std::string (or a literal)