17-monotonically increasing the eldest-son sequence
Memory limit: 64MB time limit: 3000ms special Judge:no
accepted:21 submit:49
Title Description: Find the length of the longest increment subsequence of a string
For example: dabdbf the longest increment subsequence is abdf, length is 4 input description:
The first line is an integer 0<n<20, which indicates that there are n strings to process the next n rows, each line has a string that is no longer than 10000
Output Description:
The length of the longest increment subsequence of the output string
Sample input:Copy
3aaaababcabklmncdefg
Sample output:
137
Analysis (Dynamic planning):
①, the maximum length required for the whole, we can be considered from the maximum length of the local;
②, from left to right, each encounter a point from the first to start traversing to the point, to see if this point as the prefix is the maximum value
③, State equation: dp[i] = max (Dp[i], d[j] + 1);
Steps:
①, from left to right, traverse each point in turn;
②, on the basis of the point and then from the front to the back through dp[i] = max (Dp[i], d[j] + 1) to obtain the maximum value of the point
Core code:
1 for(inti =0; I < n; ++i)2 {3Dp[i] =1;//initialize each DP[MAXN];4 for(intj =0; J < I; ++j)5 if(S[j] < s[i]) dp[i] = max (Dp[i], dp[j] +1);//find out all S[j] ==> dp[i] maximum values that meet the criteria6Ans =Max (ans, dp[i]);7}
C + + code implementation (AC):
1#include <iostream>2#include <algorithm>3#include <cmath>4#include <cstring>5#include <cstdio>6#include <queue>7#include <Set>8#include <map>9#include <stack>Ten One using namespacestd; A Const intMAXN =10010; - - intMain () the { - intT; -scanf"%d", &t); - while(T--) + { - CharS[MAXN]; +scanf"%s", s); A intLen = strlen (s), ans =-0x3f3f3f3f, DP[MAXN]; at for(inti =0; i < Len; ++i) - { -Dp[i] =1; - for(intj =0; J < I; ++j) - if(S[j] <S[i]) -Dp[i] = max (Dp[i], dp[j] +1); inAns =Max (ans, dp[i]); - } toprintf"%d\n", ans); + } - return 0; the}
※ Analysis (algorithm) "recommended":
①, find out the sequence of Jiangzi: From left to right the arrangement is increment by Ascⅱ code;
②, and each group of adjacent points Ascⅱ the smallest difference, and is the closest
Core code:
1CNT =0; temp[0] = s[0];2 for(inti =1; I < n; ++i)3 {4 if(temp[cnt] < s[i]) temp[++cnt] = S[i]//CNT + 1 is the request5 Else6 {7 for(intj =0; J <= CNT; ++j)8 {9 if(S[i] <=Temp[j])Ten { OneTEMP[J] =S[i]; A Break; - } - } the } -}
C + + code implementation (AC):
1#include <iostream>2#include <algorithm>3#include <cmath>4#include <cstring>5#include <cstdio>6#include <queue>7#include <Set>8#include <map>9#include <stack>Ten One using namespacestd; A Const intMAXN =10010; - - intMain () the { - intT; -scanf"%d", &t); - while(T--) + { - CharS[MAXN], TEMP[MAXN]; +scanf"%s", s); A at intLen = strlen (s), CNT =0; -temp[0] = s[0]; - for(inti =1; i < Len; ++i) - { - if(Temp[cnt] <S[i]) - { inTEMP[++CNT] =S[i]; - Continue; to } + - for(intj =0; J <= CNT; ++j) the { * if(S[i] <=Temp[j]) $ {Panax NotoginsengTEMP[J] =S[i]; - Break; the } + } A } theprintf"%d\n", CNT +1); + } - return 0; $}
Nyoj 17-monotonically increasing the eldest son sequence (dynamic programming, algorithm)