Longest Common subsequence (LCS) Problem

Source: Internet
Author: User
/*** Step 1: Check whether the problem is a dynamic planning problem. * first, we need to prove that the longest public subsequence problem is a dynamic planning problem, that is, it meets the two characteristics of the Dynamic Planning Algorithm: optimal sub-structure and overlapping sub-problems * optimal sub-structure: * Note: xi = <x1... xi> that is, the first I character (1 ≤ I ≤ m) (prefix) YJ = <y1... YJ> that is, the first J characters (1 ≤ j ≤ n) (prefix) of the Y sequence * assume z = <z1... ZK ≥ LCS (x, y ). * If XM = yn (the last character is the same), the question is to calculate the length of the Xm-1 and the Yn-1 of LCS (X, Y) is equal to the LCS (Xm-1, Yn-1) length plus 1 ). * If XM = YN, the question is to find the Xm-1 and Y LCs and X and Yn-1 LCS. The length of LCS (X, Y) is: the length of max {LCS (Xm-1, Y), the length of LCS (x, Yn-1 }. * Since the above when XM =yn, finding the length of LCS (Xm-1, Y) and LCS (x, Yn-1) are not mutually independent of each other: both require the length of LCS (Xm-1, Yn-1. * The LCS of the other two sequences contains the LCS of the prefix of the two sequences, so the problem has the optimal sub-structure. * Overlap subproblem: * when calculating the longest common subsequence of X and Y, the longest common subsequence of x and Yn-1 and Xm-1 and Y may be calculated, * The two subproblems both contain a common subproblem, that is, the longest common subsequence for the calculation of Xm-1 and Yn-1. Therefore, the longest common subsequence problem has the subproblem overlapping nature. ** Step 2: Establish a recursive formula * use C [I] [J] to record the length of the longest common subsequence of sequence XI and YJ. Xi = <x1, x2 ,..., Xi>, YJ = <Y1, Y2 ,..., YJ>. Build a recursive relationship as follows: * 0 if I = 0 | j = 0 * C [I] [J] = C [I-1] [J-1] + 1 if I, j> 0 & X [I] = Y [J] * max (C [I] [J-1], C [I-1] [J]) If I, j> 0 & X [I]! = Y [J]; */public class LCS {/** Step 3: Calculate the optimal value * The Dynamic Programming Algorithm for calculating the maximum length of Public subsequences lcs_length (x, y) in sequence X = <x1, x2 ,..., XM> and Y = <Y1, Y2 ,..., YN> as input. * Two arrays C [0 .. m, 0 .. n] and B [1 .. m, 1 .. n] are output. C [I, j] stores the length of the longest common subsequence of Xi and YJ, and B [I, j] records indicate C [I, j] The value is obtained by the solution of a subproblem. * This is used to construct the longest common subsequence. Finally, the length of the longest common subsequences of X and Y is recorded in C [M, N. * Array B can be omitted here. In fact, the value of the array element C [I, j] is determined only by one of the three values c [I-1, J-1], C [I-1, J] and C [I, J-1, the array element B [I, j] is only used to indicate the value of C [I, j. * Therefore, in the algorithm LCS, We can temporarily judge the value of C [I, j] from C [I-1, J-1] by means of array B instead of array C itself, which numeric element in C [I-1, J] and C [I, J-1] is determined at the cost of hour (1) time. * @ Param x * @ Param y * @ return */Public int [] [] lcslength (char X [], char y []) {int M = x. length; int n = y. length; int [] [] C = new int [M + 1] [n + 1]; for (INT I = 0; I <m + 1; I ++) c [I] [0] = 0; For (Int J = 0; j <n + 1; j ++) C [0] [J] = 0; for (INT I = 1; I <= m; I ++) {for (Int J = 1; j <= N; j ++) {// I, j starts from 1, so I-1 and J-1 are used below to make it possible to start from array 0 element if (X [I-1] = Y [J-1]) {c [I] [J] = C [I-1] [J-1] + 1;} else if (C [I-1] [J]> = C [I] [J-1]) {c [I] [J] = C [I-1] [J];} Else {C [I] [J] = C [I] [J-1] ;}} return C;}/** Step 4: construct the longest common subsequence * The LCS function is used to construct the longest common subsequence. It uses the C array obtained in the calculation of the optimal value to quickly * construct the sequence X = <x1, x2 ,..., XM> and Y = <Y1, Y2 ,..., The longest common subsequence of YN>. * @ Param C * @ Param x * @ Param I * @ Param J */Public void LCS (int c [] [], char X [], int I, Int J) {if (I = 0 | j = 0) return; If (C [I] [J] = (C [I-1] [J-1] + 1 )) {LCS (C, X, I-1, J-1); // note that the length of C is greater than X 1 system. out. println (X [I-1]);} else if (C [I] [J] = C [I-1] [J]) {LCS (C, X, I-1, j);} else {LCS (C, X, I, J-1);} // test public static void main (string ARGs []) {char X [] = {'A', 'B', 'C', 'B', 'D', 'A', 'B '}; char y [] = {'B', 'D', 'C', 'A', 'B', 'A'}; LCS = new LCS (); int [] [] C = LCS. lcslength (x, y); LCS. LCS (C, X, X. length, Y. length );}}

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.