retagged by
11,840 views
2 2 votes
For X=  BDCABA and Y=ABCBDAB find length of lcs and no of such  lcs..(solve it using table method)

2 Answers

0 0 votes

Given sequences are BDCABA & ABCBDAB.

ALGO:  

 LCS(m,n)

  {   1 + L(m-1,n-1)   if A[m] = B[n]         // if first character match then take it 

      max( LCS(m-1,n), LCS(m,n-1)  )   else   

  }


BDCABA & ABCBDAB : First character mismatch 

      then two cases   :

1)  BDCABA & BCBDAB : we hv removed Ist char from second string..

         Here Ist character match

 LCS = 1+ LCS( DCABA & CBDAB)

                    Now D & C mismatch again take two(can apply algo)..

              Here to do quickly we observe manually that longest possible now is 3 characters(we can use algo also..) - So, i get  CAB ,CBA,DAB  

which when combined with B gives BCAB,BCBA,BDAB


2)   DCABA & ABCBDAB :  Here we hv removed first B from Ist sequence

                            Now, D & A first characters again mismatch.

   two parts again:

          2a) CABA & ABCBDAB : no common subsequence of length 4 possible here

          2b) DCABA & BCBDAB : similarly here too - no possibility for a subsequence

                                                   of length 4

edited by
Position:
Show:

Related questions

0 0 votes
2 answers 2 answers
3.3k
3.3k views
gmrishikumar asked Jan 22, 2019
3,299 views
Consider two strings A = "anandarmy" and B = "algorithms". Let ‘y’ be the length of the longest common subsequence (not necessarily contiguous) between A and B and let ‘x...
3 3 votes
1 1 answer
1.8k
1.8k views
Bongbirdie asked May 17, 2017
1,847 views
For finding longest common subsequence(LCS), standard sources mention that the recursive procedure consisting of the recursive tree occupies O(m+n) space( WITHOUT applyin...
2 2 votes
0 0 answers
626
626 views
shaurya vardhan asked Nov 6, 2017
626 views
We wish to find the longest common palindromic subsequence in a string. Read the above code snippet and choose the missing statements.a)S1: DP[i][j]=0; S2: DP[i][j] =1+DP...