ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
LongestCommonSubsequence.h File Reference

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. More...

#include "ADS/Contract.h"
#include <algorithm>
#include <cstddef>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#include "ADS/string/impl/LongestCommonSubsequence.tpp"

Go to the source code of this file.

Namespaces

namespace  ADS

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

Detailed description

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.

\[ \mathit{DP}[ i ][ j ] = \left\{ \begin{array}{ll} 0 & i = 0 \mbox{ or } j = 0 , \cr \mathit{DP}[ i - 1 ][ j - 1 ] + 1 & p_{i-1} = t_{j-1} , \cr \max\{ \mathit{DP}[ i - 1 ][ j ] , \mathit{DP}[ i ][ j - 1 ] \} & \mbox{otherwise,} \end{array} \right. \]

\( \Theta( m n ) \) time. lcsMemo() is the memoized version of the book, which solves only the subproblems that it reaches. diff() reads the differences from the table in \( O( m + n ) \), as the Unix command does on the lines of two files (exercise 13.10): the items of \( P \) only marked '-', the items of \( T \) only '+', the common ones ' '. The longest common substring, contiguous, has \( \mathit{DP}[ i ][ j ] = \mathit{DP}[ i - 1 ][ j - 1 ] + 1 \) if \( p_{i-1} = t_{j-1} \) and 0 otherwise, its length the largest entry (exercise 13.2).

The contracts [see Contract.h]: lcs() gives a subsequence of both strings as long as the table says (checked at level 3), and longestCommonSubstring() a substring of both. The operations are defined in LongestCommonSubsequence.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, section 13.7, https://algoritmicamente.it/materiali/libro/capitoli/cap13.pdf
J. W. Hunt, M. D. McIlroy, An algorithm for differential file comparison, Computing Science Technical Report 41, Bell Laboratories, 1976
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa