1. Longest ascending subsequence
2. Longest common sub-sequence
For the two sequence x={x1,x2,x3...xi ...},y={y1,y2,y3 ...}
3. Longest ascending common sub-sequence
The following is an O (n^2) algorithm Rujia the longest public ascending subsequence (LCIs)
Preparatory knowledge: The basic idea of dynamic programming, Lcs,lis.
Problem: String A, String B, LCIs of A and B (the longest public ascending subsequence).
First we can see that this problem has quite a lot of overlapping sub-problems. So we thought of doing it with DP. What is DP's primary task? Define the state.
The definition state F[i][j] represents the length of the LCIs, which is the end of the first I-character B-string of a string upper with the b[j].
Why is this not the other State definition? The most important reason is that I only have this, and another reason is that I know this definition can get squared algorithm. And the reason I'm just going to do this is that the state definition is too good to be useful. I'll say that later.
Let's take a look at this state. It may seem a bit tricky to think about what states this state can move to, and if you reverse the mindset, it's much easier to look at what states the optimal values of this state depend on. What state does this state depend on?
First, there are f[i][j]=f[i-1][j] in a[i]!=b[j]. Why is it? Because F[i][j] is the end of b[j] LCIs, if F[i][j]>0 then explain a[1]. A[i] must have a character a[k] equals b[j] (if f[i][j] equals 0? There is no effect on the value of the assignment). Because A[k]!=a[i], then A[i] did not contribute to f[i][j, so we do not consider it can still derive f[i][j] the optimal value. So in the case of a[i]!=b[j] there must be f[i][j]=f[i-1][j]. This point refers to the processing method of LCS.
What if A[I]==B[J]? First of all, this is equal to the minimum guaranteed length of 1 lcis. Then we need to find a lcis that is the longest and can get b[j] to the end of it. Where was the longest lcis? First of all, we're going to find the first dimension of the F-array must be i-1. Because I had already taken it with B[J], it was not used. And it can't be i-2, because i-1 is necessarily better than i-2. What about the second dimension? Then you need to enumerate b[1]. B[j-1], because you don't know which is the longest and which is less than b[j]. There is another problem here, but it is impossible not to pair it. In the case of a[i]==b[j], need not consider the decision of F[i][j]=f[i-1][j]? The answer is no need. Because if B[J] is not paired with A[i, it is the same as the previous a[1]. A[J-1] Pairing (assuming f[i-1][j]>0, equal to 0 is not considered), so must not and A[i] pairing superior. (Why is it necessary?) Because B[j] and a[i] are paired with Max (F[i-1][k]) +1, and the previous I ' Pairing is Max (F[i ' -1][k]) +1. Apparently there's f[i][j]>f[i '][j],i ' >i)
So we came to the state transition equation:
A[I]!=B[J]: f[i][j]=f[i-1][j]
A[I]==B[J]: F[i][j]=max (f[i-1][k]) +1 1<=k<=j-1&&b[j]>b[k]
It is not difficult to see, this is a time complexity of O (n^3) of the DP, a distance from the square.
However, the most critical of this algorithm is that if you follow a reasonable recursion order, Max (F[i-1][k]) values can be obtained by maintaining the update of a max variable when we visit f[i][k]. How do you get it? The order of the first recursion must be the first dimension of the State in the outer layer loop, and the second dimension in the inner layer loop. That is, F[1][len (b)] to calculate f[2][1].
If we follow this recursion order we can add a max variable to 0 at the beginning of each outer loop and start the inner loop. MAX=F[I-1][J] When A[i]>b[j]. If the loop is a[i]==b[j], it makes f[i][j]=max+1.
The final answer is F[len (a)][1]. Maximum value of F[len (a)][len (b)].
Reference code:
#include <cstdio>#include<cstring>intf[1005][1005],a[1005],b[1005],i,j,t,n1,n2,max;intMain () {scanf ("%d",&t); while(t--) {scanf ("%d%d",&n1,&n2); for(i=1; i<=n1;i++) scanf ("%d",&A[i]); for(i=1; i<=n2;i++) scanf ("%d",&B[i]); Memset (F,0,sizeof(f)); for(i=1; i<=n1;i++) {Max=0; for(j=1; j<=n2;j++) {F[i][j]=f[i-1][j]; if(a[i]>b[j]&&max<f[i-1][J]) max=f[i-1][j]; if(A[i]==b[j]) f[i][j]=max+1; }} Max=0; for(i=1; i<=n2;i++)if(Max<f[n1][i]) max=F[n1][i]; printf ("%d\n", Max); }}
4. Maximum sub-order and
(1) Improved exhaustive method O (n^2)
intMaxsubsum (int*arr,intN) { intMaxsum =0, Thissum; for(inti =0; I < n; i++) {thissum=0;//Thissum reduces repetitive calculations in O (n^3) for(intj = i; J < N; J + +) {thissum+=Arr[j]; Maxsum=Max (maxsum, thissum); } } returnmaxsum;}
(2) Sub-rule O (NLOGN)
#include <stdio.h>#include<iostream>using namespacestd;intMaxsubsum (int*arr,intLeftintRight ) { if(left = =Right )return(Arr[left] <0) ?0: Arr[left]; intlmaxsum, Rmaxsum, cmaxsum; intCenter = (left + right)/2; Lmaxsum=maxsubsum (arr, left, center); Rmaxsum= Maxsubsum (arr, center+1, right); //the leftmost sequence with the right end node on the left. intLlmaxsum =0, Llthissum =0; for(inti = center; I >= left; i--) {llthissum+=Arr[i]; Llmaxsum=Max (llmaxsum, llthissum); } //the oldest sequence with the left node on the right. intRrmaxsum =0, Rrthissum =0; for(inti = center+1; I <= right; i++) {rrthissum+=Arr[i]; Rrmaxsum=Max (rrmaxsum, rrthissum); } //the oldest sequence that spans the left and right partsCmaxsum = Llmaxsum +rrmaxsum; returnMax (Max (Lmaxsum, rmaxsum), cmaxsum);}intMain () {intarr[ A] = {-6,2,4, -7,5,3,2, -1,6, -9,Ten, -2}; printf ("ans =%d\n", Maxsubsum (arr,0, One));}
(3) Dynamic planning method O (N)
intMaxsubsum (int*arr,intN) { intMaxsum =0, Thissum =0; for(inti =0; I < n; i++) {thissum+=Arr[i]; if(Thissum <0)//indicates that this segment is not likely to be prefixed with the maximum ascending subsequenceThissum =0; Maxsum= Max (maxsum, thissum);//However, the maximum ascending subsequence in this segment may have an effect on the result } returnmaxsum;}
Lis/lcs/lcis