Java Dynamic programming implements the longest common subsequence and the longest common substring __java

Source: Internet
Author: User

Dynamic Programming Method

Often encountering complex problems cannot be easily decomposed into several child problems, but a series of child problems will be decomposed. The problem solving time can be increased by the scale of the problem by simply using the method of decomposing the big problem into a child problem and synthesizing the solution of the problem.

In order to save time for repeating the same sub problem, an array is introduced, whether or not they are useful for the final solution, and the solution of all the child problems is in the array, which is the basic method of dynamic programming.

"Problem" to find the longest common character subcode sequence of two character sequences

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>, 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, the implication of "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, the implication "z0,z1,...,zk-1" is one of 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.

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 child sequence 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 is in the same direction as the recursive call and the number of steps is the same.

Java Code Implementation:

[Java] View plain copy public class lcsproblem    {       public  static void main (String[] args)        {            //preserves the empty string to GetLength () the integrity of the method (s) or not to preserve             //but in the GetLength () method must be additional initialization c[][] first Row first column             string[] x = {"",  "A",  "B",  "C",  "B",  "D",   "A",  "B"};           string[] y = { "" ", " B ", " D ", " C ", " a ", " B ", " a "};                       int[][] b =  GetLength (x, y);                     &nbSp; display (b, x, x.length-1, y.length-1);       }        /**       *  @param  x        *  @param  y       *  @return   Returns an array of records that determine the direction of the search        */       public static int[][]  getlength (string[] x, string[] y)        {            int[][] b = new int[x.length][y.length];            int[][] c = new int[x.length][ y.length];                       for (int i=1; i<x.length; i++)             {               for (int j=1; j <y.length; j++)                {                    // Corresponding to the first property                     if ( x[i] == y[j])                     {                        c[i][j] = c[i-1][j-1] + 1;                         b[i][j] = 1;                    }                    //corresponding to the second or third nature                     else if (C[i-1][j] >= c[i][j-1])                     {                         c[i][j] = c[i-1][j];                        b[i][j] = 0;                    }                    //corresponding to the second or third nature                    else                    {                        c[ i][j] = c[i][j-1];                        b[i][j] = -1;                    }                }            }                          return b;       }        //backtracking The basic realization, takes the recursive way        public static void display (Int[][] b, &NBSP;STRING[]&NBSP;X,&NBSP;INT&NBSP;I,&NBSP;INT&NBSP;J)        {           if (i == 0 | |  j == 0)                 return;                       if (b[i][j] == 1)            {                display (b, x,  i-1, j-1);                System.out.print (x[i] +  " ");           }    &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;ELSE&NBSp;if (b[i][j] == 0)            {                display (B,&NBSP;X,&NBSP;I-1,&NBSP;J);            }            else if (b[i][j] == -1)            {                display (b, x, i,  j-1);           }       }   }  

Longest common substring: similar to the eldest child sequence, only the common substring requirement must be contiguous.
The Java implementation code is as follows:

[Java] View Plain copy public class stringcompare {       //in the dynamic Programming matrix generation method, For each generated row, the previous line is no longer available, so just use a one-dimensional array instead of a commonly used two-bit array        public static void  getlcstring (CHAR[]&NBSP;STR1,&NBSP;CHAR[]&NBSP;STR2)  {            int len1, len2;           len1  = str1.length;           len2 =  str2.length;           int maxLen = len1  > len2 ? len1 : len2;               int[] max = new int[maxLen];//  save an array of oldest string lengths             int[] maxIndex = new int[maxLen];//  Save the oldest oldest string array of maximum indices    &NBSP;&NBsp;      int[] c = new int[maxlen];              int i, j;            for  (i = 0; i < len2; i++)  {                for  (j = len1 - 1;  j >= 0; j--)  {                    if  (Str2[i] == str1[j])  {                         if  ((i == 0)  | |   (j == 0))                             c[j] = 1;                        else                            c[j] = c[j  - 1] + 1;//at this time c[j-1] or the value in the last loop because it has not yet been assigned                     } else {                        c[j] =  0;                    }                       //  if is greater than that temporarily only one is the longest, and to put the back of the Qing 0;                    if  (c[j] > max[0])  {       &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;MAX[0]  = c[j];                        maxIndex[0] = j;                           for   (int k = 1; k < maxlen; k++)  {                             max[k] = 0;                            maxindex[k] =  0;                        }                    }                    //  There are multiple substrings of the same length                     else if  (c[j] == max[0])  {                        for   (int k = 1; k < maxlen; k++)  {                             if  (max[k] == 0)  {                                 max[k]  = c[j];                                maxindex[k] =  j;                                break; //  Add one to the back and exit the loop.                             }                        }                    }                }                for  (int temp : c)  {                    system.out.print (temp);                }                system.out.println ();            }           //Print the eldest son string          &

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.