Longest Common Sequence

Source: Internet
Author: User

I believe everyone is not familiar with the longest common subsequence. Here we will discuss two cases: subsequence is not continuous, and subsequence is continuous.

Question: string a = "abcbdab", string B = "bdcabc", find the longest common subsequence of A and B

(1)Subsequences are not consecutive.

Subsequences are discontinuous. The longest method used is dynamic planning. The general formula is:
X [I] [J] = x [I-1] [J-1] If a [I] = B [J]
X [I] [J] = max {x [I-1] [J], X [I] [J-1]} If a [I]! = B [J]

Code implementation:

View code

# Include <iostream>
# Include <stack>
Using namespace STD;
Const int n= 100;
Int C [N] [N]; // C [I] [J] record string a [1... i] and B [1... j] Longest public substring

Int LCS (char * a, char * B, int Lena, int lenb );

Void main ()
{
Char A [8] = {'', 'A', 'B', 'C', 'B', 'D', 'A', 'B '};
Char B [7] = {'', 'B', 'D', 'C', 'A', 'B', 'C '};
Int Lena = 7, lenb = 6;
Int Len = LCS (a, B, Lena, lenb );
Cout <Len <Endl;

Int I = Lena, j = lenb;
Stack <char> S;
While (I & J) // output the longest public substring
{
If (C [I] [J] = C [I-1] [J-1] + 1) // determine how C [I] [J] gets, and take different measures
{
S. Push (A [I]);
I --;
J --;
}
Else if (C [I] [J] = C [I-1] [J])
I --;
Else
J --;
}
While (! S. Empty ())
{
Cout <S. Top () <"";
S. Pop ();
}
Cout <Endl;
}

Int LCS (char * a, char * B, int Lena, int lenb)
{
Int I, J;
Memset (C, 0,100*100 );
For (I = 0; I <= Lena; I ++)
C [I] [0] = 0;
For (j = 0; j <= lenb; j ++)
C [0] [J] = 0;
For (I = 1; I <= Lena; I ++)
For (j = 1; j <= lenb; j ++)
{
If (A [I] = B [J])
C [I] [J] = C [I-1] [J-1] + 1;
Else
C [I] [J] = (C [I-1] [J]> C [I] [J-1])? C [I-1] [J]: C [I] [J-1];
}
Return C [Lena] [lenb];
}

(2) The string is continuous.
Method 1: Dynamic Planning

The string is continuous and can also be solved by dynamic planning.
X [I] [J] = x [I-1] [J-1] + 1 if a [I] = B [J]
X [I] [J] = 0 if a [I]! = B [J]

Code implementation:

View code

# Include <iostream>
# Include <stack>
Using namespace STD;
Const int n= 100;
Int C [N] [N]; // C [I] [J] records that contain a [I] and B [J], and the length of the Public substring ending with this

Void LCS (char * a, char * B, int Lena, int lenb, Int & taga, Int & tagb );
// Taga indicates the end position of the longest common string in string a, and tagb indicates the end position of the longest common string in string B.

Void main ()
{
Char A [8] = {'', 'A', 'B', 'C', 'B', 'D', 'A', 'B '};
Char B [7] = {'', 'B', 'D', 'C', 'A', 'B', 'C '};
Int Lena = 7, lenb = 6, taga = 0, tagb = 0;
LCS (a, B, Lena, lenb, taga, tagb );
Cout <taga <"" <tagb <"" <C [taga] [tagb] <Endl;
For (INT I = taga-C [taga] [tagb] + 1; I <= taga; I ++)
Cout <A [I] <"";
Cout <Endl;
Return;

}

Void LCS (char * a, char * B, int Lena, int lenb, Int & taga, Int & tagb)
{
Int I, J;
Int max = 0;
Memset (C, 0,100*100 );
For (I = 0; I <= Lena; I ++)
C [I] [0] = 0;
For (j = 0; j <= lenb; j ++)
C [0] [J] = 0;
For (I = 1; I <= Lena; I ++)
For (j = 1; j <= lenb; j ++)
{
If (A [I] = B [J])
C [I] [J] = C [I-1] [J-1] + 1;
Else
C [I] [J] = 0;
If (C [I] [J]> MAX) // the maximum length of the record and its subscripts
{
Max = C [I] [J];
Taga = I;
Tagb = J;
}
}
Return;
}

 

Method 2: generalized suffix tree

It is implemented using dynamic planning, and the time complexity is relatively high. If multiple strings are used together to find the longest common subsequence, the time complexity is a problem. The generalized suffix tree can be used to reduce time complexity, but the increase in space complexity is equivalent to the exchange of space for time. Previous sections on the generalized suffix tree have been discussed. To implement the generalized suffix tree, you need to pre-process the generalized suffix tree. each node in the tree must record the number of leaves of the tree with the node as the root. Assume that there are n strings, then find the node with N as the number of all leaves, and then find the node with the farthest root. The corresponding path is the longest common subsequence.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.