Java implementation of "The longest ascending subsequence, the maximal continuous subsequence sequence and the longest common substring" __java

Source: Internet
Author: User
Tags first string
I. Description of the problemThis is a three-way DP problem. The longest ascending sequence: Looking for some number in a column, which satisfies: arbitrary two numbers a[i] and a[j], if i<j, must have a[i]<a[j], so the longest subsequence is called the Longest ascending (ascending) subsequence.

Set Dp[i] Represents the length of the longest increment subsequence ending with I, the state transition equation is: dp[i] = max{dp[j]+1}, 1<=j<i,a[j]<a[i]. Time complexity is O (n*n);

Consider two numbers of a[x] and A[y],x<y and A[x]<a[y], and Dp[x]=dp[y] then which one should we choose. Obviously A[x] because it has more potential, that is, we can replace a[y with a[x], which means we need to maintain a data structure to store the elements of a possible incrementing sequence, and we need to replace them at some point. So we can use a list to store, and in the search for a replacement location with a binary lookup to achieve, so the time complexity of O (Nlogn).


Maximal sequential subsequence and: Looking for some number in a column, these numbers satisfy: Any two number a[i] and a[j], if i+1=j, must have A[i]<a[j, and ∑ () the largest. What needs to be clear is the meaning of State representation in the state transition equation. Because contiguous, dp[i] represents a continuous value that should end with the I position element, not the maximum value.


Longest common substring: the most common contiguous substring in two strings.

Find the longest common substring of two strings, which requires continuous in the original string. In fact, this is a sequential decision-making problem, you can use dynamic programming to solve. We use a two-dimensional matrix to record the intermediate results. How does this two-dimensional matrix structure? Just for example: "Bab" and "Caba" (of course, we can now see the longest common substring is "ba" or "AB")
b A B
C 0 0 0
A 0 1 0
B 1 0 1
A 0 1 0
We can find the longest common substring by looking at the longest diagonal line of the matrix.
But looking for the longest diagonal diagonally 1 on a two-dimensional matrix is also a cumbersome and time-consuming thing to do: when the matrix is filled 1 o'clock let it equal its upper left corner element plus 1.
b A B
C 0 0 0
A 0 1 0
B 1 0 2
A 0 2 0
The largest element in this matrix is the length of the longest common substring.

So we need to maintain a two-dimensional array, the length of the first string, and the length of the second string for the number of columns. To intercept the substring, we need the start and end index of the substring. With the previous analysis, because the DP records the length of a node, we can start and end the index of the record substring by maintaining a length and ending index. Second, Java code

/** * Maximum Increment sub sequence * * @param nums * @return/public int getlis (int[] nums) {if (Nums==null | | nums.leng
		th==0) {return 0;
		int max = 1;
		Int[] dp = new Int[nums.length];
			for (int i = 0; i < nums.length i++) {dp[i] = 1;
					for (int j = 0; J < i; J +) {if (Nums[j]<nums[i]) {Dp[i] = Math.max (dp[i), dp[j]+1);
				max = Math.max (Dp[i], max);
    }} return max; /** * Maximum Increment subsequence * * @param nums * @return/public int getlis (int[] nums) {if (Nums = null | | nums.lengt
		H = = 0) {return 0;
		} arraylist<integer> dp = new arraylist<> ();
			for (int item:nums) {if (dp.size () = 0 | | | dp.get (dp.size ()-1) < item) {Dp.add (item);
			else {int i = Collections.binarysearch (DP, item);//insert position Dp.set (i < 0?-i-1: I, item);
	} return Dp.size (); /** * Max contiguous subsequence and * * @param nums * @return/public int getmaxsubarray (int[] nums) {if (Nums = =null | |
		Nums.length = = 0) {return 0;
		int maxendinghere = 0;
		int maxsofar = Integer.min_value;
			for (int i = 0; i < nums.length i++) {if (Maxendinghere < 0) {maxendinghere = 0;
			} Maxendinghere + = Nums[i];
		Maxsofar = Math.max (Maxsofar, Maxendinghere);
	return Maxsofar; /** * Longest Common substring * * @param a * @param b * @return/public string Getlcs (string A, string b) {if (A = = null | | b = = NULL | | A.length () = = 0 | |
		B.length () = = 0) {return null;
		} int[][] dp = new Int[a.length ()][b.length ()];
		int endhere = 0;
		int maxlen = 0; for (int i = 0; i < a.length (); i++) {for (int j = 0; J < B.length (); j + +) {if (i = 0 | | j = = 0) {DP I [j] = A.charat (i) = = B.charat (j)?
				1:0;
				else {Dp[i][j] = A.charat (i) = = B.charat (j)? Dp[i-1][j-1] + 1:0;
					} if (Dp[i][j] > maxlen) {maxlen = Dp[i][j];
				Endhere = i; }} return A.substring (Endhere-maxlen + 1, ENDHere + 1); }


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.