|
ADS
Algorithms and Data Structures
|
The longest common subsequence (LCS) of \( P \) and \( T \), the reality check of section 13.7 of the book. More...
Files | |
| file | ADS/string/LongestCommonSubsequence.h |
| The longest common subsequence (LCS) of \( P \) and \( T \), the reality check of section 13.7 of the book: \( \mathit{DP}[ i ][ j ] \), its length for the prefixes \( P( i ) \) and \( T( j ) \), is. | |
Functions | |
| template<class S> | |
| std::vector< std::vector< int > > | ADS::lcsTable (const S &P, const S &T) |
| the table of the lengths of the LCS of the prefixes of \( P \) and \( T \) | |
| template<class S> | |
| int | ADS::lcsMemo (const S &P, const S &T, long *solved=nullptr) |
| the length of the LCS by the memoization of the book, \( -1 \) marking the subproblems not solved yet; the number of the ones solved goes to *solved, if given | |
| template<class S> | |
| S | ADS::lcs (const S &P, const S &T) |
| a longest common subsequence, read back from the table | |
| template<class S> | |
| std::vector< std::pair< char, typename S::value_type > > | ADS::diff (const S &P, const S &T) |
| the differences from \( P \) to \( T \): ( '-' , p ) for an item of \( P \) only, ( '+' , t ) for an item of \( T \) only, ( ' ' , x ) for a common one | |
| template<class S> | |
| std::tuple< std::size_t, std::size_t, std::size_t > | ADS::longestCommonSubstring (const S &P, const S &T) |
| the longest common substring: its length and where it ends in \( P \) and \( T \), the first one found by rows of \( P \); \( ( 0 , 0 , 0 ) \) if they share nothing | |
The longest common subsequence (LCS) of \( P \) and \( T \), the reality check of section 13.7 of the book.
The definitions, the invariants and the costs of the area are in Theory of strings.
| std::vector< std::pair< char, typename S::value_type > > ADS::diff | ( | const S & | P, |
| const S & | T ) |
the differences from \( P \) to \( T \): ( '-' , p ) for an item of \( P \) only, ( '+' , t ) for an item of \( T \) only, ( ' ' , x ) for a common one
In pseudocode (exercise 13.10), the table read back as lcs() does, the items of \( T \) preferred on a tie:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{diff}( P , T )\quad \triangleright \ \text{the lines kept, added (+) and removed (-)} \cr 2 & \quad \mathit{DP} \gets \mathrm{lcsTable}( P , T );\ ( i , j ) \gets ( m , n );\ D \gets () \cr 3 & \quad \textbf{while}\ ( i , j ) \ne ( 0 , 0 )\ \textbf{do}\quad \triangleright\ D \text{ built from its end} \cr 4 & \qquad \textbf{if}\ i , j > 0 \text{ and } P[ i - 1 ] = T[ j - 1 ] \ \textbf{then}\ \text{put } ( \text{' '} , P[ i - 1 ] ) \text{ at the front of } D;\ ( i , j ) \gets ( i - 1 , j - 1 ) \cr 5 & \qquad \textbf{else if}\ i > 0 \text{ and } ( j = 0 \text{ or } \mathit{DP}[ i - 1 ][ j ] > \mathit{DP}[ i ][ j - 1 ] ) \ \textbf{then} \cr 6 & \qquad\quad \text{put } ( \text{'-'} , P[ i - 1 ] ) \text{ at the front of } D;\ i \gets i - 1 \cr 7 & \qquad \textbf{else}\ \text{put } ( \text{'+'} , T[ j - 1 ] ) \text{ at the front of } D;\ j \gets j - 1 \cr 8 & \quad \textbf{return}\ D \end{array}} \]
| S ADS::lcs | ( | const S & | P, |
| const S & | T ) |
a longest common subsequence, read back from the table
In pseudocode (section 13.7):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{lcs}( P , T ) \cr 2 & \quad \mathit{DP} \gets \mathrm{lcsTable}( P , T );\ ( i , j ) \gets ( m , n );\ X \gets () \cr 3 & \quad \textbf{while}\ i > 0\ \textbf{and}\ j > 0\ \textbf{do}\quad \triangleright\ X \text{ built from its end} \cr 4 & \qquad \textbf{if}\ P[ i - 1 ] = T[ j - 1 ]\ \textbf{then} \ \text{put } P[ i - 1 ] \text{ at the front of } X;\ ( i , j ) \gets ( i - 1 , j - 1 ) \cr 5 & \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 6 & \quad \textbf{return}\ X \end{array}} \]
| int ADS::lcsMemo | ( | const S & | P, |
| const S & | T, | ||
| long * | solved = nullptr ) |
the length of the LCS by the memoization of the book, \( -1 \) marking the subproblems not solved yet; the number of the ones solved goes to *solved, if given
In pseudocode (section 13.7), \( \mathit{DP} \) all \( -1 \) at first and the call \( \mathrm{rec}( m , n ) \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{rec}( i , j )\quad \triangleright \ \mathit{DP} \text{ starts at } -1 \text{, not yet computed} \cr 2 & \quad \textbf{if}\ i = 0\ \textbf{or}\ j = 0\ \textbf{then return}\ 0 \cr 3 & \quad \textbf{if}\ \mathit{DP}[ i ][ j ] \ge 0\ \textbf{then return} \ \mathit{DP}[ i ][ j ] \cr 4 & \quad \mathit{DP}[ i ][ j ] \gets 1 + \mathrm{rec}( i - 1 , j - 1 ) \text{ if } P[ i - 1 ] = T[ j - 1 ] \text{, else } \max\{ \mathrm{rec}( i , j - 1 ) , \mathrm{rec}( i - 1 , j ) \} \cr 5 & \quad \textbf{return}\ \mathit{DP}[ i ][ j ] \end{array}} \]
| std::vector< std::vector< int > > ADS::lcsTable | ( | const S & | P, |
| const S & | T ) |
the table of the lengths of the LCS of the prefixes of \( P \) and \( T \)
In pseudocode (section 13.7):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{lcsTable}( P , T )\quad \triangleright \ \mathit{DP}[ i ][ j ] \text{ for the prefixes of } i \text{ and } j \text{ letters} \cr 2 & \quad \mathit{DP}[ 0 .. m ][ 0 .. n ] \gets 0 \cr 3 & \quad \textbf{for}\ i \gets 1\ \textbf{to}\ m\ \textbf{do} \cr 4 & \qquad \textbf{for}\ j \gets 1\ \textbf{to}\ n\ \textbf{do} \cr 5 & \qquad\quad \textbf{if}\ P[ i - 1 ] \ne T[ j - 1 ]\ \textbf{then} \ \mathit{DP}[ i ][ j ] \gets \max\{ \mathit{DP}[ i ][ j - 1 ] , \mathit{DP}[ i - 1 ][ j ] \} \cr 6 & \qquad\quad \textbf{else}\ \mathit{DP}[ i ][ j ] \gets 1 + \mathit{DP}[ i - 1 ][ j - 1 ]\quad \triangleright \ \text{the common last letter} \cr 7 & \quad \textbf{return}\ \mathit{DP} \end{array}} \]
| std::tuple< std::size_t, std::size_t, std::size_t > ADS::longestCommonSubstring | ( | const S & | P, |
| const S & | T ) |
the longest common substring: its length and where it ends in \( P \) and \( T \), the first one found by rows of \( P \); \( ( 0 , 0 , 0 ) \) if they share nothing
In pseudocode (exercise 13.2), two rows of the table:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{longestCommonSubstring}( P , T )\quad \triangleright\ \mathit{DP}[ i ][ j ] \text{ the longest common suffix} \cr 2 & \quad ( \mathit{best} , e_i , e_j ) \gets ( 0 , 0 , 0 ) \cr 3 & \quad \textbf{foreach}\ i \in [ 1 , m ] , j \in [ 1 , n ] \text{, by rows}\ \textbf{do} \cr 4 & \qquad \mathit{DP}[ i ][ j ] \gets 0 \text{ if } P[ i - 1 ] \ne T[ j - 1 ] \text{, else } 1 + \mathit{DP}[ i - 1 ][ j - 1 ] \cr 5 & \qquad \textbf{if}\ \mathit{best} < \mathit{DP}[ i ][ j ] \ \textbf{then}\ ( \mathit{best} , e_i , e_j ) \gets ( \mathit{DP}[ i ][ j ] , i , j )\quad \triangleright\ \text{the first longest} \cr 6 & \quad \textbf{return}\ ( \mathit{best} , e_i , e_j ) \end{array}} \]