Problem description: sequence X = {x1, x2 ,..., Xn}, y = {y1, Y2 ,..., Yn}, when z = {Z1, Z2 ..., Zn} is a subset of X's strictly incrementing subscripts (which can be discontinuous) and a subset of Y's strictly incrementing subscripts (which can be discontinuous, then Z is the common subsequence of X and Y. For example, x = {A, B, C, B, D, a, B}, y = {B, D, C, A, B}, {B, c, a}, {B, C, B, A}, {B, d, a, B} are all common subsequences of X and Y. The longest common subsequence is longest common subsequence, which is the classic LCS.
Specific points: Char [] xarray and char [] yarray are character arrays with the length of M and N, respectively. Find their LCs
[Analysis] top-down analysis,Two-dimensional array ctable [I] [J] records xarray [0 ~ I], yarray [0 ~ J], ctable [m] [N] records xarray [0 ~ M], yarray [0 ~ N] the length of the longest common subsequence.
1) if xarray [m] = yarray [N], it indicates that the last element xarray [m] is an element in LCS, xarray [0 ~ M], yarray [0 ~ N] Longest Common subsequence = xarray [0 ~ M-1], yarray [0 ~ The longest common subsequence of n-1] is + 1, that is, ctable [m] [N] = ctable [M-1] [n-1].
2) If xarray [m]! = Yarray [N] indicates that xarray [m] And yarray [N] may all be elements in LCS, but not at the same time. If xarray [m] may be, xarray [0 ~ M], yarray [0 ~ N] Longest Common subsequence = xarray [0 ~ M], yarray [0 ~ N-1]. If yarray [N] may be, xarray [0 ~ M], yarray [0 ~ N] Longest Common subsequence = xarray [0 ~ M-1], yarray [0 ~ N. That is, ctable [m] [N] = max (ctable [M-1] [N], ctable [m] [n-1]).
The recursive state equation is:
Refer to introduction to algorithms p394 pseudocode. Java implementation is as follows:
[Java]View plaincopyprint?
- /**
- * Creation Time: 9:00:13, January 1, September 3, 2014
- * Project name: Test
- * @ Author Cao yanfeng Peking University
- * @ Since JDK 1.6.0 _ 21
- * Class description: Longest Common subsequence (LCS)
- */
- Public staticvoid main (string [] ARGs ){
- // The method stub automatically generated by todo
- String x = "abcbdabcbacabc ";
- String y = "bdcabacababcb ";
- Int temp = getlcslength (x, y );
- System. Out. println ("\ n length:" + temp );
- }
- Public staticint getlcslength (string X, string y ){
- Char [] xarray = x. tochararray ();
- Char [] yarray = Y. tochararray ();
- Int M = xarray. length;
- Int n = yarray. length;
- Int [] [] btable = newint [m] [N];
- /* Ctable [I] [J] records xarray [0 ~ I], yarray [0 ~ J] the length of the longest common subsequence */
- Int [] [] ctable = newint [M + 1] [n + 1];
- For (INT I = 0; I <m; I ++ ){
- For (Int J = 0; j <n; j ++ ){
- If (xarray [I] = yarray [J]) {
- Ctable [I + 1] [J + 1] = ctable [I] [J] + 1;
- Btable [I] [J] = 2; // equal to 2
- } Else if (ctable [I] [J + 1]> = ctable [I + 1] [J]) {
- Ctable [I + 1] [J + 1] = ctable [I] [J + 1];
- Btable [I] [J] = 1;
- } Else {
- Ctable [I + 1] [J + 1] = ctable [I + 1] [J];
- Btable [I] [J] = 3;
- }
- }
- }
- System. Out. Print ("the maximum sub-array is :");
- Printlcs (xarray, btable, M-1, n-1 );
- Return ctable [m] [N];
- }
- /* The optimal output path is the longest common subsequence */
- Public staticvoid printlcs (char [] xarray, int [] [] btable, int I, Int J ){
- If (I =-1 | j =-1 ){
- Return;
- }
- If (btable [I] [J] = 2 ){
- Printlcs (xarray, btable, I-1, J-1 );
- System. Out. Print (xarray [I]);
- } Else if (btable [I] [J] = 1 ){
- Printlcs (xarray, btable, I-1, J );
- } Else {
- Printlcs (xarray, btable, I, J-1 );
- }
- }
- }
/*** Creation Time: 9:00:13, January 1, September 3, 2014 * Project name: test * @ author Cao yanfeng Peking University * @ since JDK 1.6.0 _ 21 * class description: longest Common subsequence (LCS) */public static void main (string [] ARGs) {// The method stub automatically generated by todo string x = "abcbdabcbacabc "; string y = "bdcabacababcb"; int temp = getlcslength (x, y); system. out. println ("\ n length:" + temp);} public static int getlcslength (string X, string y) {char [] xarray = x. tochara Rray (); char [] yarray = y. tochararray (); int M = xarray. length; int n = yarray. length; int [] [] btable = new int [m] [N];/* ctable [I] [J] records xarray [0 ~ I], yarray [0 ~ The length of the longest common subsequence of J] */INT [] [] ctable = new int [M + 1] [n + 1]; for (INT I = 0; I <m; I ++) {for (Int J = 0; j <n; j ++) {If (xarray [I] = yarray [J]) {ctable [I + 1] [J + 1] = ctable [I] [J] + 1; btable [I] [J] = 2; // equal flag as 2} else if (ctable [I] [J + 1]> = ctable [I + 1] [J]) {ctable [I + 1] [J + 1] = ctable [I] [J + 1]; btable [I] [J] = 1 ;} else {ctable [I + 1] [J + 1] = ctable [I + 1] [J]; btable [I] [J] = 3 ;}} system. out. print ("maximum sub-array:"); printlcs (xarray, btable m-1, n-1); Return ctable [m] [N];} /* The optimal output path is the longest common subsequence */public static void printlcs (char [] xarray, int [] [] btable, int I, Int J) {if (I =-1 | j =-1) {return;} If (btable [I] [J] = 2) {printlcs (xarray, btable, i-1, J-1); system. out. print (xarray [I]);} else if (btable [I] [J] = 1) {printlcs (xarray, btable, I-1, J );} else {printlcs (xarray, btable, I, J-1 );}}}
**************************************** *
Console output:
Maximum sub-array: bcbacbacb
Length: 9
[Introduction to algorithms-29] classical problem of Dynamic Planning 02: Longest Common subsequence (LCS)