[Introduction to algorithms-29] classical problem of Dynamic Planning 02: Longest Common subsequence (LCS)

Source: Internet
Author: User

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?
  1. /**
  2. * Creation Time: 9:00:13, January 1, September 3, 2014
  3. * Project name: Test
  4. * @ Author Cao yanfeng Peking University
  5. * @ Since JDK 1.6.0 _ 21
  6. * Class description: Longest Common subsequence (LCS)
  7. */
  8. Public staticvoid main (string [] ARGs ){
  9. // The method stub automatically generated by todo
  10. String x = "abcbdabcbacabc ";
  11. String y = "bdcabacababcb ";
  12. Int temp = getlcslength (x, y );
  13. System. Out. println ("\ n length:" + temp );
  14. }
  15. Public staticint getlcslength (string X, string y ){
  16. Char [] xarray = x. tochararray ();
  17. Char [] yarray = Y. tochararray ();
  18. Int M = xarray. length;
  19. Int n = yarray. length;
  20. Int [] [] btable = newint [m] [N];
  21. /* Ctable [I] [J] records xarray [0 ~ I], yarray [0 ~ J] the length of the longest common subsequence */
  22. Int [] [] ctable = newint [M + 1] [n + 1];
  23. For (INT I = 0; I <m; I ++ ){
  24. For (Int J = 0; j <n; j ++ ){
  25. If (xarray [I] = yarray [J]) {
  26. Ctable [I + 1] [J + 1] = ctable [I] [J] + 1;
  27. Btable [I] [J] = 2; // equal to 2
  28. } Else if (ctable [I] [J + 1]> = ctable [I + 1] [J]) {
  29. Ctable [I + 1] [J + 1] = ctable [I] [J + 1];
  30. Btable [I] [J] = 1;
  31. } Else {
  32. Ctable [I + 1] [J + 1] = ctable [I + 1] [J];
  33. Btable [I] [J] = 3;
  34. }
  35. }
  36. }
  37. System. Out. Print ("the maximum sub-array is :");
  38. Printlcs (xarray, btable, M-1, n-1 );
  39. Return ctable [m] [N];
  40. }
  41. /* The optimal output path is the longest common subsequence */
  42. Public staticvoid printlcs (char [] xarray, int [] [] btable, int I, Int J ){
  43. If (I =-1 | j =-1 ){
  44. Return;
  45. }
  46. If (btable [I] [J] = 2 ){
  47. Printlcs (xarray, btable, I-1, J-1 );
  48. System. Out. Print (xarray [I]);
  49. } Else if (btable [I] [J] = 1 ){
  50. Printlcs (xarray, btable, I-1, J );
  51. } Else {
  52. Printlcs (xarray, btable, I, J-1 );
  53. }
  54. }
  55. }
/*** 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)

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.