Dynamic Programming---Realize the length of the largest common subsequence and output the largest substring (Java language description)

Source: Internet
Author: User

Reference Blog Address: http://blog.csdn.NET/biangren/article/details/8038605

http://blog.csdn.net/njr465167967/article/details/51675424Dynamic Programming Method Divide-and-conquer method is to divide the problem into some independent problems, solve the problem recursively, then merge the solution of the child problem and get the solution of the original problem. Often, however, complex problems can not be easily decomposed into several sub problems, but will decompose a series of child problems, that is, each child problem contains a common child problem. If we simply use the method of decomposing the big problem into the sub problem and the solution of the big problem, the problem solving time will increase according to the scale of the problem. The dynamic programming method solves only once for each child problem, saves its result in a table, and asks the longest common character subcode sequence of two character sequences from "problem"

Problem Description: The subsequence of a character sequence is a sequence of characters that is formed from a given sequence of characters that is arbitrarily (not necessarily continuous) to remove several characters (which may not be removed). The given sequence of characters is x= "X0,x1,...,xm-1", the sequence y= "Y0,y1,...,yk-1" is a subsequence of x, and there is a strictly ascending subscript sequence of x <i0,i1,...,ik-1&gt, which makes for all j=0,1,...,k-1, has xij=yj. For example, x= "Abcbdab", y= "bcdb" is a subsequence of X.

Consider how the longest common subsequence problem can be decomposed into sub problems, set a= "A0,a1,...,am-1", b= "B0,b1,...,bm-1", and z= "Z0,z1,...,zk-1" as their longest common subsequence. It is not difficult to prove the following nature:

(1) If am-1=bn-1, then zk-1=am-1=bn-1, and "Z0,z1,...,zk-2" is a longest common subsequence of "a0,a1,...,am-2" and "b0,b1,...,bn-2";

(2) If the am-1!=bn-1 is zk-1!=am-1, it means that "z0,z1,...,zk-1" is a longest common subsequence of "a0,a1,...,am-2" and "b0,b1,...,bn-1";

(3) If the am-1!=bn-1 is zk-1!=bn-1, it means that "z0,z1,...,zk-1" is the longest common subsequence of "a0,a1,...,am-1" and "b0,b1,...,bn-2".

In this way, when looking for the common subsequence of A and B, if there is a am-1=bn-1, then further solves a child problem, looks for "a0,a1,...,am-2" and "b0,b1,...,bm-2" one longest common subsequence, if am-1!=bn-1, then solves two child problems, discovers "the A0, One of the longest common subsequence of the A1,...,am-2 "and" b0,b1,...,bn-1 "and" a0,a1,...,am-1 "and" b0,b1,...,bn-2 ", and then the longest common subsequence of the older of both. Rather than recalculate the answers every time you encounter each child problem.
Solving:

The introduction of a two-dimensional array c[][], using c[i][j] to record the length of the LCS of X[i] and Y[j, B[i][j] Records c[i][j is obtained by the value of which child problem to determine the direction of the search.
We are recursive computations from the bottom up, then the c[i-1][j-1],c[i-1][j] and c[i][j-1] are calculated before the c[i,j is computed. At this point, we can calculate the y[j according to x[i] = Y[j] or x[i]!= c[i][j].
The question is recursively written:
Backtracking outputs the longest common subsequence process: algorithm analysis:
Because each call moves at least one step up or to the left (or up and to the left), the maximum number of times (M + N) is encountered when i = 0 or j = 0, and the return begins. The time complexity of the algorithm is θ (M +n) when the return time is the same as the recursive call, and the number of steps is the same.
The Java implementation code is as follows:

Package com.hbut.test;
 /** * Created by HP on 2017/6/6.
        * * Public class LCS {public static void main (string[] args) {String str1 = "Abcbdab";

        String str2 = "Bdcaba";
        System.out.println (Getmaxsubstringlen (str1, str2));
        Int[][] B = gettrace (str1, str2); String Smax = Str1.length () > Str2.length ()? STR1:STR2; Select the longest string because you want to remove the largest substring string smin = Str1.length () < Str2.length ()? STR1:STR2;
    Select the smallest string print (b, Smax, Smax.length (), smin.length ());                  public static int Getmaxsubstringlen (string x, string y) {int xlen = x.length () + 1;

        Plus 1 is because the first initialization is 0 int ylen = y.length () + 1; int rlen = xlen > Ylen?       Xlen:ylen; Large string for row int cLen = Xlen < Ylen?       Xlen:ylen;            Small string for column int[][] C = new Int[rlen][clen]; Matrix C save State for (int i = 1; i < Rlen; i++) {for (int j = 1; j < CLen; J +) {if ( X.chaRAt (i-1) = = Y.charat (j-1)) {//equal, by oblique diagonal +1 c[i][j] = c[i-1][j-1] + 1;
                else if (C[i-1][j] >= c[i][j-1]) {//unequal, select larger c[i][j] = c[i-1][j];
                else {C[i][j] = c[i][j-1];               }} return c[xlen-1][ylen-1]; The last one is the longest public sequence number}//The state of the record value, making it easy to backtrack back to the publicly static int[][] Gettrace (string x, string y) {int xlen = x
        . Length () + 1;

        int ylen = y.length () + 1; int rlen = xlen > Ylen?
        Xlen:ylen; int cLen = Xlen < Ylen?
        Xlen:ylen;
        Int[][] C = new Int[rlen][clen];
        Int[][] B = new Int[rlen][clen];  for (int i = 1; i < Rlen. i++) {for (int j = 1; j < CLen; j) {if (X.charat (i-1) = =
                    Y.charat (j-1)) {C[i][j] = c[i-1][j-1] + 1;
                B[I][J] = 2; 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] = 0;
    }} return B;
        //recursive implementation backtracking, then print out the longest common subsequence (discontinuous subsequence) public static void print (int[][] B, String s, int i, int j) {//recursive termination condition
        if (i = = 0 | | | j = = 0) {return;
            } if (b[i][j] = = 2) {print (b, S, i-1, j-1);
        System.out.print (S.charat (i-1) + "");
        else if (b[i][j] = = 1) {print (b, S, I-1, J);
        else if (b[i][j] = = 0) {print (b, S, I, j-1);
 }
    }


}

Output Result: 4
b C B A
Related Article

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.