Java Longest ascending subsequence

Source: Internet
Author: User

The longest ascending subsequence is the deformation of the longest common subsequence.

As long as the string is sorted in ascending order and the original string is the longest common subsequence.

The following provides a tool class that can pass in any form of an array. (To add a new type of array, construct the method yourself).

Package Com.leejuen.string;import Java.lang.reflect.array;import Java.util.arrays;public class LCS{private Integer Len;private object Str1;private Object str2; LCS (String a,string b) {str1 = A.tochararray (); str2 = B.tochararray ();} LCS (char[] a,char[] b) {str1 = A;STR2 = b;} LCS (int[] a,int[] b) {str1 = A;STR2 = b;} public int Getlcs () {if (len==null) INI (); return Len;} private void Ini () {int str1_len = array.getlength (str1); int str2_len = Array.getlength (STR2); int[][] dp = new Int[str1_len      +1][STR2_LEN+1]; Initialize Dp,java The default data is 0 so you do not have to assign a value of (int i=1;i<=str1_len;i++) {for (int j=1;j<=str2_len;j++) {Object TMP1 = Array.get ( STR1, i-1); Object tmp2 = Array.get (str2, j-1); if (Tmp1.equals (TMP2)) {dp[i][j] = dp[i-1][j-1]+1;} ELSE{DP[I][J] = Math.max (Dp[i][j-1], dp[i-1][j]);}} len = Dp[str1_len][str2_len];} Here is the test: print 4, 5/*public static void Main (string[] args) {//Classic longest common subsequence String a = "Bdcaba"; String B = "Abcbdab"; System.out.println (A, B) Getlcs ());//Longest ascending subsequence int[] cc1 = {3,1,5,6,2,7,9};int[] Cc2 = Arrays.CopyOf (CC1, cc1.length); Arrays.sort (CC2); System.out.println (New LCS (CC1,CC2). Getlcs ());} */}


Java Longest ascending 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.