Dynamic programming---The longest common subsequence

Source: Internet
Author: User

1, problem description
A subsequence of a given sequence is a sequence that is obtained after deleting several elements in the sequence. Specifically, if the given sequence x={x1,x2,..., XM}, then another sequence of Z={Z1,Z2,..., the zk},x sub-sequence refers to the existence of a strictly incrementing subscript sequence {i1,i2,..., ik} so that for all j=1,2,... K has zj=xij for example, sequence Z={b,c,d , B} is a subsequence of sequence x={a,b,c,b,d,a,b}, and the corresponding increment subscript sequence is {2,3,5,7}.
Given two sequences x and Y, when another sequence z is both a subsequence of X and a sub-sequence of y, it is said that Z is the common subsequence of the sequence x and Y, that the common subsequence can have multiple, and that the longest common subsequence is the longest of the x and Y sequences.
2. Algorithm Description:
When XM =yn, find the longest common subsequence of Xm-1 and Yn-1, and then add XM (=yn) at the end of it to get the longest common subsequence of x and Y. When XM!=yn, you must solve two sub-problems, that is, to find a longest common subsequence of Xm-1 and Y and X and Yn-1 One of the longest common subsequence sequences. The older of the two common subsequence sequences is the longest common subsequence of x and Y.

C[I][J] The length of the longest common subsequence storing XI and YJ, From[i][j] representing the length of the c[i][j] is derived from which case;

3. Algorithm time complexity analysis
The length complexity of the common sub-sequence is m*n, the sub-sequence is obtained according to the Storage state table and the output complexity is m*n, so the final time complexity is m*n.

The code is as follows:

ImportJava.util.Scanner; Public classLCS { Public Static voidMain (string[] args) {//Abcdefgie//BCDFGEScanner input =NewScanner (system.in); String as=Input.nextline (); String BS=Input.nextline (); Char[] A =As.tochararray (); Char[] B =Bs.tochararray (); int[] from =Init (A, b);        Comlength (A,b,from); //PrintArray (from);Printcom (a,from,a.length-1,b.length-1); }     Public Static voidPrintArray (int[] from) {//enter the given array to print it         for(inti=0; I<from.length; i++){             for(intj=0; J<from[0].length; J + +) {System.out.print (From[i][j]+" ");        } System.out.println (); }    }     Public Static int[] Init (Char[]a,Char[]b) {//creating an array of sources        int[] from =New int[A.length][b.length];  for(inti=0; i<a.length; i++){             for(intj=0; j<b.length; J + +) {From[i][j]=0; }        }        returnFrom ; }     Public Static voidComlength (Char[]a,Char[]b,int[][]from) {//assigning values to the from state array         intm =a.length; intn =b.length; int[][]C =New int[M][n];  for(inti=0; i<m; i++){              for(intj=0; j<n; J + +) {C[i][j]=0; }         }          for(intI=1; i<m; i++){              for(intJ=1; j<n; J + +){                 if(A[i]==b[j]) {//The representative derives from the 1th case: The last one of the two strings of the comparison is equalC[i][j]=c[i-1][j-1]+1; FROM[I][J]=1; }Else if(C[i][j-1]>=c[i-1][j]) {//The representative derives from the 2nd case: The last one of the last equal and longest common subsequence of the two strings compared is not equal to the last one of the array of a character sequences;C[i][j]=c[i][j-1]; FROM[I][J]=2; }Else{//The representative derives from the 3rd case: The last of the last unequal and longest common subsequence of the two strings compared is not equal to the last one of the B array character sequences;C[i][j]=c[i-1][j]; FROM[I][J]=3; }             }         }//PrintArray (c);//System.out.println ();    }     Public Static voidPrintcom (Char[]a,int[][]from,intMintN) {//outputs the longest common subsequence based on the from state array          if(m==0|n==0)return; if(from[m][n]==1) {printcom (a,from,m-1,n-1);          System.out.print (A[m]); }Else if(from[m][n]==2) {printcom (A,from,m,n-1); }Else{printcom (a,from,m-1, N); }    }}
View Code

Dynamic programming---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.