Monotonic incrementing longest-gen sequence time limit: 3000 MS | memory limit: 65535 KB
Difficulty: 4
-
Description
Returns the length of the longest incrementing sub-sequence of a string.
For example, the longest incremental sub-sequence of dabdbf is abdf and the length is 4.
Input
The first line has an integer of 0 <n <20, indicating that n strings are to be processed.
Next n rows, each line has a string, the length of the string will not exceed 10000
-
Output
Length of the longest incrementing sub-sequence of the output string
-
Sample Input
-
-
3
Aaa
Ababc
Abklmncdefg
-
-
Sample output
-
-
1
3
7
Code:
For example, num [I] stores the input number. f [I] indicates the number of monotonically increasing numbers for this number, including
The state equation is dp [I] = max (dp [I], dp [I-1] + 1)
What I wrote is to look for something smaller than I in the position I. In fact, it is the same as finding the I in the first position.
If you find a dp value greater than the current dp value, replace it
#include"stdio.h"#include"string.h"int main(){int t,i,j,max,len;char str[10010];int length[10010];scanf("%d",&t);while(t--){scanf("%s",str);len = strlen(str);for(i=0;i<len;i++)length[i] = 1;for(i=1;i<len;i++){for(j=i-1;j>=0;j--){if(str[i]>str[j] && length[i]<=length[j]+1)length[i] = length[j]+1; }}max = 0;for(i=0;i<len;i++){if(max<length[i])max = length[i];}printf("%d\n",max);}}