lcs-longest common sub-sequence

Source: Internet
Author: User

The longest common substring (longest Common Subsequence,lcs), the difference between a subsequence and a substring: a substring is a contiguous paragraph.
Brute Force Method(For each sub-sequence of s, check whether it is a subsequence of T, S has a 2^m subsequence, T has a 2^n subsequence, s length is m,t length is n)
The time complexity is O (2^n * 2^m).
Dynamic Programming Method(Top-down) time complexity and space complexity are all O (m*n)
(The optimal sub-structure means that the global optimal solution contains the local optimal solution, the problem can be decomposed into sub-problem resolution), generally can be solved by four steps: to find the structure of the optimal solution, recursive definition of the optimal value of the solution, the value of the optimal solution is calculated by the bottom-up method, and an optimal solution is constructed by the
First, we find the optimal substructure,
Sets the longest common subsequence Z of the sequence s and T, then:
If sm=tn, then Zk=sm=tn, and Zk-1 is the longest common subsequence of Sm-1 and Tn-1;
If Sm≠tn and ZK≠SM, then Z is the longest common subsequence of Sm-1 and T;
If Sm≠tn and Zk≠tn, then Z is the longest common sub-sequence of S and Tn-1.
Uppercase indicates the front-to-back sequence.
The recursive relationship is:
If s[m] = = T[n], then l[m][n]=l[m-1][n-1]+1;
If S[M]. = T[n], then l[m][n]=max{l[m-1][n], l[m][n-1]}.
F (x) =⎧⎩⎨0c[i−1][j−1]max{c[i-1][j], c[i][j-1]}if i=0 or j=0if i,j>0, and Si=tjif i,j>0, and Si≠tj f (x) = \begin{cases} 0& \text{if i=0 or j=0}\\ c[i-1][j-1]& \text{if i,j>0, and}s_{i}=t_{j}\\ \text{max{c[i-1][j], C[i][j-1]}}&amp ; \text{if i,j>0, and}s_{i} \neq t_{j} \end{cases}
To solve this problem, we need to ask three questions: LCS (sm-1,tn-1) +1;lcs (sm-1,t), LCS (s,tn-1), Max{lcs (Sm-1,t), LCS (S,tn-1)}.
When calculating the longest common subsequence of S and T, it is necessary to calculate the longest common subsequence of S and tn-1 and Sm-1 and T, which two sub-problems contain a common sub-problem, the longest common subsequence of tn-1 and Sm-1.
It can be seen from the recursive structure that LCS have overlapping properties of sub-problems. However, the calculation time increases with the length index, taking into account the common sub-Problem of O (m*n), so the bottom-up dynamic programming is used to improve the efficiency.
Attach the code to run the environment for jdk1.8:

Import Java.util.Stack;
    public class LCS {public static int m, n;
    public static char[] s, t;
    public static int[][] DP;
    public static int[][] B; public static void Main (string[] args) {//TODO auto-generated method Stub s = new char[]{' A ', ' B ', ' C ', '
        B ', ' D ', ' A ', ' B '};
        t = new char[]{' B ', ' D ', ' C ', ' A ', ' B ', ' A '};
        m = s.length;
        n = t.length;
        DP = new INT[M+1][N+1];
        b = new Int[m+1][n+1];
        System.out.println (Dplength ());
    LCS ();
                public static int Dplength () {for (int i = 1, i < m+1; i++) {for (int j = 1; j < N+1; J + +) {
                    if (s[i-1] = = T[j-1]) {dp[i][j] = dp[i-1][j-1] + 1;
                B[I][J] = 0;
                    }else if (dp[i][j-1] >= dp[i-1][j]) {dp[i][j] = dp[i][j-1];
                B[I][J] = 1;
                    }else{Dp[i][j] = dp[i-1][j];
B[I][J] = 2;                }}} return dp[m][n];
        } public static void LCs () {stack<character> Stack = new stack<character> ();
        int i = m, j = N;
                while (i > 0 && J > 0) {if (b[i][j] = = 0) {i--;
                j--;
            Stack.push (S[i]);
            }else if (b[i][j] = = 1) {j--;
            }else{i--;
        }} while (!stack.isempty ()) {System.out.print (Stack.pop () + "");
 }
    }
}

may be more than one of the longest common sub-sequences, at this point, you want to use an array or container to save, and then write

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.