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