|
ADS
Algorithms and Data Structures
|
Four classic problems on the subsequences of sequences \( X \), \( Y \), \( Z \) of lengths \( n \), \( m \), \( l \), relatives of the longest common subsequence, each by dynamic programming on the prefixes. More...
Files | |
| file | ADS/string/SubsequenceProblems.h |
| Four classic problems on the subsequences of sequences \( X \), \( Y \), \( Z \) of lengths \( n \), \( m \), \( l \), relatives of the longest common subsequence [see LongestCommonSubsequence.h], each by dynamic programming on the prefixes. | |
Functions | |
| template<class S> requires std::is_class_v< S > | |
| bool | ADS::isInterleaving (const S &X, const S &Y, const S &Z) |
| whether \( Z \) interleaves \( X \) and \( Y \) keeping the order of the items of each | |
| bool | ADS::isInterleaving (const std::string &X, const std::string &Y, const std::string &Z) |
| isInterleaving() above on std::string (or literals) | |
| template<class S> requires std::is_class_v< S > | |
| std::size_t | ADS::scsLength (const S &X, const S &Y) |
| the length of a shortest common supersequence of \( X \) and \( Y \) | |
| std::size_t | ADS::scsLength (const std::string &X, const std::string &Y) |
| scsLength() above on std::string (or literals) | |
| template<class S> requires std::is_class_v< S > | |
| S | ADS::shortestCommonSupersequence (const S &X, const S &Y) |
| a shortest common supersequence of \( X \) and \( Y \) | |
| std::string | ADS::shortestCommonSupersequence (const std::string &X, const std::string &Y) |
| shortestCommonSupersequence() above on std::string (or literals) | |
| template<class S> requires std::is_class_v< S > | |
| std::uint64_t | ADS::subsequenceOccurrences (const S &P, const S &T) |
| the number of occurrences of \( P \) as a subsequence of \( T \), modulo \( 2^{64} \): 1 for the empty \( P \), 0 if \( P \) is longer than \( T \) | |
| std::uint64_t | ADS::subsequenceOccurrences (const std::string &P, const std::string &T) |
| subsequenceOccurrences() above on std::string (or literals) | |
| template<class S> requires std::is_class_v< S > | |
| S | ADS::lcs3 (const S &X, const S &Y, const S &Z) |
| a longest common subsequence of \( X \), \( Y \) and \( Z \) | |
| std::string | ADS::lcs3 (const std::string &X, const std::string &Y, const std::string &Z) |
| lcs3() above on std::string (or literals) | |
Four classic problems on the subsequences of sequences \( X \), \( Y \), \( Z \) of lengths \( n \), \( m \), \( l \), relatives of the longest common subsequence, each by dynamic programming on the prefixes.
The definitions, the invariants and the costs of the area are in Theory of strings.
|
nodiscard |
whether \( Z \) interleaves \( X \) and \( Y \) keeping the order of the items of each
In pseudocode (chapter 13, beyond the book), the row \( i \) of the table in \( D[ 0 .. m ] \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{isInterleaving}( X , Y , Z ) \cr 2 & \quad \textbf{if}\ |Z| \ne n + m\ \textbf{then return false} \cr 3 & \quad D[ 0 ] \gets \textbf{true};\ \textbf{for}\ j \gets 1 \ \textbf{to}\ m\ \textbf{do}\ D[ j ] \gets D[ j - 1 ]\ \textbf{and} \ Y[ j - 1 ] = Z[ j - 1 ] \cr 4 & \quad \textbf{for}\ i \gets 1\ \textbf{to}\ n\ \textbf{do} \cr 5 & \qquad D[ 0 ] \gets D[ 0 ]\ \textbf{and}\ X[ i - 1 ] = Z[ i - 1 ] \cr 6 & \qquad \textbf{for}\ j \gets 1\ \textbf{to}\ m\ \textbf{do} \cr 7 & \qquad\quad D[ j ] \gets ( D[ j ]\ \textbf{and}\ X[ i - 1 ] = Z[ i + j - 1 ] )\ \textbf{or}\ ( D[ j - 1 ]\ \textbf{and}\ Y[ j - 1 ] = Z[ i + j - 1 ] ) \cr 8 & \quad \textbf{return}\ D[ m ] \end{array}} \]
|
inlinenodiscard |
isInterleaving() above on std::string (or literals)
|
nodiscard |
a longest common subsequence of \( X \), \( Y \) and \( Z \)
In pseudocode (section 13.7 in three dimensions, beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{lcs3}( X , Y , Z ) \cr 2 & \quad D[ 0 .. n ][ 0 .. m ][ 0 .. l ] \gets 0 \cr 3 & \quad \textbf{foreach}\ ( i , j , k ) \in [ 1 , n ] \times [ 1 , m ] \times [ 1 , l ] \text{ in increasing order}\ \textbf{do} \cr 4 & \qquad \textbf{if}\ X[ i - 1 ] = Y[ j - 1 ] = Z[ k - 1 ] \ \textbf{then}\ D[ i ][ j ][ k ] \gets D[ i - 1 ][ j - 1 ][ k - 1 ] + 1 \cr 5 & \qquad \textbf{else}\ D[ i ][ j ][ k ] \gets \max\{ D[ i - 1 ][ j ][ k ] , D[ i ][ j - 1 ][ k ] , D[ i ][ j ][ k - 1 ] \} \cr 6 & \quad \textbf{return}\ \text{the subsequence read back from } D[ n ][ m ][ l ] \text{, as } \mathrm{lcs}() \text{ does} \end{array}} \]
|
inlinenodiscard |
lcs3() above on std::string (or literals)
|
nodiscard |
the length of a shortest common supersequence of \( X \) and \( Y \)
In pseudocode (chapter 13, beyond the book), one row \( L \) of the table of the LCS:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{scsLength}( X , Y ) \cr 2 & \quad L[ 0 .. m ] \gets 0 \cr 3 & \quad \textbf{for}\ i \gets 1\ \textbf{to}\ n\ \textbf{do} \cr 4 & \qquad \mathit{diag} \gets 0 \cr 5 & \qquad \textbf{for}\ j \gets 1\ \textbf{to}\ m\ \textbf{do} \cr 6 & \qquad\quad \mathit{up} \gets L[ j ] \cr 7 & \qquad\quad L[ j ] \gets \mathit{diag} + 1 \text{ if } X[ i - 1 ] = Y[ j - 1 ] \text{, else } \max\{ \mathit{up} , L[ j - 1 ] \} \cr 8 & \qquad\quad \mathit{diag} \gets \mathit{up} \cr 9 & \quad \textbf{return}\ n + m - L[ m ] \end{array}} \]
|
inlinenodiscard |
scsLength() above on std::string (or literals)
|
nodiscard |
a shortest common supersequence of \( X \) and \( Y \)
In pseudocode (chapter 13, beyond the book), the table of the LCS read back as lcs() does, keeping also the items left out:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{shortestCommonSupersequence}( X , Y ) \cr 2 & \quad L \gets \mathrm{lcsTable}( X , Y );\ i \gets n;\ j \gets m;\ W \gets (\,) \cr 3 & \quad \textbf{while}\ i > 0\ \textbf{and}\ j > 0\ \textbf{do} \cr 4 & \qquad \textbf{if}\ X[ i - 1 ] = Y[ j - 1 ]\ \textbf{then} \ \text{prepend } X[ i - 1 ] \text{ to } W;\ i \gets i - 1;\ j \gets j - 1 \cr 5 & \qquad \textbf{else if}\ L[ i - 1 ][ j ] \ge L[ i ][ j - 1 ] \ \textbf{then}\ \text{prepend } X[ i - 1 ] \text{ to } W;\ i \gets i - 1 \cr 6 & \qquad \textbf{else}\ \text{prepend } Y[ j - 1 ] \text{ to } W;\ j \gets j - 1 \cr 7 & \quad \textbf{return}\ X[ 0 .. i - 1 ] \cdot Y[ 0 .. j - 1 ] \cdot W \end{array}} \]
|
inlinenodiscard |
shortestCommonSupersequence() above on std::string (or literals)
|
nodiscard |
the number of occurrences of \( P \) as a subsequence of \( T \), modulo \( 2^{64} \): 1 for the empty \( P \), 0 if \( P \) is longer than \( T \)
In pseudocode (chapter 13, beyond the book), \( C[ j ] \) the occurrences of \( P[ 0 .. j - 1 ] \) in the prefix of \( T \) read so far, updated from the right:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{subsequenceOccurrences}( P , T ) \cr 2 & \quad C[ 0 ] \gets 1;\ C[ 1 .. m ] \gets 0 \cr 3 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ |T| - 1\ \textbf{do} \cr 4 & \qquad \textbf{for}\ j \gets \min\{ m , i + 1 \}\ \textbf{downto}\ 1 \ \textbf{do} \cr 5 & \qquad\quad \textbf{if}\ T[ i ] = P[ j - 1 ]\ \textbf{then}\ C[ j ] \gets ( C[ j ] + C[ j - 1 ] ) \bmod 2^{64} \cr 6 & \quad \textbf{return}\ C[ m ] \end{array}} \]
|
inlinenodiscard |
subsequenceOccurrences() above on std::string (or literals)