Longest common sub-sequence LCS

Source: Internet
Author: User

1  PackageDynamicprogram;2 3 Importorg.junit.Test;4 5 6  Public classLCS {7 8     //enter two series to find the longest common sub-sequence9     //B and C two arrays one is used to save the result of the calculation, one to save the pathTen     //Array B uses 1 for top left, 2 for top, and 3 for left One      Public voidLcslength (int[] Rows,int[] cols,int[] [] B,int[] [] c) { A         intRowslength =rows.length; -         intColslength =cols.length; -          for(inti = 1; i < rowslength; ++i) { the              for(intj = 1; J < Colslength; ++j) { -                 if(Rows[i] = =Cols[j]) { -C[I][J] = c[i-1][j-1] + 1; -B[I][J] = 1; +                 } -                 Else if(C[i][j-1] > C[i-1][j]) { +C[I][J] = c[i][j-1]; AB[I][J] = 2; at                 } -                 Else { -C[I][J] = c[i-1][j]; -B[I][J] = 3; -                 } -             } in         } -     } to      +      Public voidPrintlcs (int[] [] B,int[] Rows,intIintj) { -         if(i = = 0 | | j = 0) { the             return; *         } $         if(B[i][j] = = 1) {Panax NotoginsengSystem.out.println ((Char) rows[i]); -Printlcs (b, rows, i-1, j-1); the         } +         Else if(B[i][j] = = 2) { APrintlcs (b, rows, I, j-1); the         } +         Else  -Printlcs (b, rows, i-1, j); $              $     } -      - @Test the      Public voidTest () { -         //index value for subscript 0 is temporarily unusedWuyi         int[] cols = {-1, ' A ', ' B ', ' C ', ' B ', ' D ', ' A ', ' B '}; the         int[] rows = {-1, ' B ', ' D ', ' C ', ' A ', ' B ', ' a '}; -         //preparatory work Wu         //in fact, the spatial complexity of the algorithm can be further optimized -         intRowslength =rows.length; About         intColslength =cols.length; $         //declares an array of two m rows n columns, because the array's subscript 0 is not used, otherwise the length of the array is required +1 -         //used to record some additional path information. -         int[] Path =New int[rowslength][colslength]; -         //used to record sub-problems, that is, the length of the oldest sequence A         int[] len =New int[rowslength][colslength]; +          for(intj = 0; J < Rowslength; ++j) theLen[j][0] = 0; -          for(inti = 0; i < colslength; ++i) $Len[0][i] = 0; the lcslength (rows, cols, path, Len); the         //System.out.println (len[rowslength-1][colslength-1]); the         //Note that this printout of an optimal LCS reverse-sequence output thePrintlcs (path, rows, RowsLength-1, colsLength-1); -     } in}

Longest common sub-sequence 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.