Printing Longest Common Subsequence | Set 2 ( Printing All ) | Dynamic Programming implementation of LCS problem ; Maximum string length ; Returns set containing all LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] ; construct a set to store possible LCS ; If we reaches end of either string , return a empty set ; If the last characters of X and Y are same ; recurse for X [ 0. . m - 2 ] and Y [ 0. . n - 2 ] in the matrix ; append current character to all possible LCS of substring X [ 0. . m - 2 ] and Y [ 0. . n - 2 ] . ; If the last characters of X and Y are not same ; If LCS can be constructed from top side of the matrix , recurse for X [ 0. . m - 2 ] and Y [ 0. . n - 1 ] ; If LCS can be constructed from left side of the matrix , recurse for X [ 0. . m - 1 ] and Y [ 0. . n - 2 ] ; merge two sets if L [ m - 1 ] [ n ] == L [ m ] [ n - 1 ] Note s will be empty if L [ m - 1 ] [ n ] != L [ m ] [ n - 1 ] ; Returns length of LCS for X [ 0. . m - 1 ] , Y [ 0. . n - 1 ] ; Build L [ m + 1 ] [ n + 1 ] in bottom up fashion ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  100 NEW_LINE int L [ N ] [ N ] ; set < string > findLCS ( string X , string Y , int m , int n ) { set < string > s ; if ( m == 0 n == 0 ) { s . insert ( " " ) ; return s ; } if ( X [ m - 1 ] == Y [ n - 1 ] ) { set < string > tmp = findLCS ( X , Y , m - 1 , n - 1 ) ; for ( string str : tmp ) s . insert ( str + X [ m - 1 ] ) ; } else { if ( L [ m - 1 ] [ n ] >= L [ m ] [ n - 1 ] ) s = findLCS ( X , Y , m - 1 , n ) ; if ( L [ m ] [ n - 1 ] >= L [ m - 1 ] [ n ] ) { set < string > tmp = findLCS ( X , Y , m , n - 1 ) ; s . insert ( tmp . begin ( ) , tmp . end ( ) ) ; } } return s ; } int LCS ( string X , string Y , int m , int n ) { for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( i == 0 j == 0 ) L [ i ] [ j ] = 0 ; else if ( X [ i - 1 ] == Y [ j - 1 ] ) L [ i ] [ j ] = L [ i - 1 ] [ j - 1 ] + 1 ; else L [ i ] [ j ] = max ( L [ i - 1 ] [ j ] , L [ i ] [ j - 1 ] ) ; } } return L [ m ] [ n ] ; } int main ( ) { string X = " AGTGATG " ; string Y = " GTTAG " ; int m = X . length ( ) ; int n = Y . length ( ) ; cout << " LCS ▁ length ▁ is ▁ " << LCS ( X , Y , m , n ) << endl ; set < string > s = findLCS ( X , Y , m , n ) ; for ( string str : s ) cout << str << endl ; return 0 ; }