Longest common child sequence (longest Common Sequence)

Source: Internet
Author: User
definition of the problem:Sub sequence
X= (A, B, C, B, D, b) z= (b, C, D, b) is a subsequence example of x w= (b, D, A) is not a subsequence of X's subroutine common subsequence
Z is a common subsequence of sequence x and y if z is a subsequence of X and also a sub sequence of Y. Maximum common child sequence (LCS) problem

Input: X = (x1, x2, ..., xn), Y = (y1, y2, ..., ym)
Output: Z = Longest common child sequence for x and y

Brute Force method: enumerates each of the subsequence Z of x, checks whether Z is a subsequence of y, T (n) =o (mx2n).

Dynamic Planning:

To optimize a child structure:
Set x = (x1, x2, ..., xn), y = (y1, y2, ..., ym) are two sequences, z= (z1, Z2, ..., Zn) are the X and Y LCS, we have: if xm = yn, then Zk = XM = yn, Zk-1 is the Xm-1 and Yn-1 LCS , that is, Lcsxy = lcsxm-1yn-1+ (xm = yn). If Xm≠yn, and ZK≠XM, then Z is the LCS for Xm-1 and y, that is, lcsxy= lcsxm-1y if Xm≠yn, and Zk≠yn, then Z is the LCS for X and Yn-1, that is lcsxy= LCSXYn-1

Recursive equation: c[i, J] = XI and YJ of LCS length of LCS
C[i, J] = 0, if i=0 or j=0 c[i, j] = C[i-1, j-1] + 1, if I, j>0 and XII = YJJ c[i, j] = Max (C[i, j-1), C[i-1, J]), if I, j>0 and XIIYJJ

Basic idea:
in the case of known c[i-1, J-1], C[i, j-1], C[i-1, J], the C[i, J, is computed according to the recursive equation.

Data structure: C[0:m,0:n]: c[i,j] is the length of the LCS of Xi and YJ B[1:m,1:n]: b[i,j] is a pointer to the table entry for the C table corresponding to the optimal solution of the selected child problem when calculating c[i,j]

Pseudo code:

Lcs-length (x, Y)
    m = length (x);
    n = Length (Y); for
    i = 1 to M do 
        c[i,0] = 0;
    For j = 1 to n do 
        c[0,j] = 0;
    For i = 1 to M doing for
        j = 1 to n do
            If XI = YJ Then 
                c[i,j] = c[i-1,j-1] + 1;
                b[i,j] = "↖";
            Else If c[i-1,j]>=c[i,j-1] Then
                c[i,j] = c[i-1,j]; 
                B[I,J] = "↑";
            Else C[i,j] = c[i,j-1];
                 B[I,J] = "←";
Return C and B.
C + + code:
#include <iostream> #include <vector> #include <string> using namespace std;

Vector<vector<int>> B, C;
    void LCS (String str1, string str2, int len1, int len2) {b.resize (len1 + 1);
    C.resize (len1 + 1);
        for (int i = 0; i < len1 + 1; i++) {b[i].resize (len2 + 1, 0);
    C[i].resize (len2 + 1, 0); for (int i = 1; I <= len1. i++) {for (int j = 1; J <= Len2; j +) {if str1[
                I-1] = = Str2[j-1]) {c[i][j] = c[i-1][j-1] + 1;
            B[I][J] = 0;
                else if (C[i-1][j] >= c[i][j-1]) {c[i][j] = c[i-1][j];
            B[I][J] = 1;
                else {C[i][j] = c[i][j-1];
            B[I][J] =-1; }} void Printlcs (Vector<vector<int>> B, string str1, int i, int j) {if (i = = 0 | | j
  = = 0)      Return
        if (b[i][j] = = 0) {Printlcs (b, str1, i-1, j-1);
    cout << str1[i-1] << "";
    else if (b[i][j] = = 1) {Printlcs (b, str1, I-1, J);
    else {Printlcs (b, str1, I, j-1);
    int main () {string str1 = ' abcdef ';

    String str2 = "ACEF";
    int len1 = Str1.size ();
    int len2 = Str2.size ();
    B.resize (len1 + 1);

    C.resize (len2 + 1);

    LCS (STR1, STR2, Len1, len2);

    cout << C[len1][len2] << Endl;
    Printlcs (b, str1, LEN1, len2);
return 0; }

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.