Some of the previous dynamic planning knowledge has forgotten that O (begin □lead) O has no choice, so I have to repeat the questions and start with a simple one.
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
One of the typical questions;
Solution 1:
Dynamic Planning. DP [I] indicates the longest ascending sequence length when the I-th element is the last element. Two examples
First
1 9, 10, 5, 6, and 7 are too expressive. Go to code analysis.
AC code:
# Include <iostream>
# Include <stdio. h>
# Include <cstring>
# Include <string>
Using namespace STD;
Int main ()
{
Int test;
Scanf ("% d", & test );
While (test --)
{
Char STR [10010];
Cin> STR;
Int Len = strlen (STR );
Int I, j, DP [Len + 1];
For (I = 0; I <Len; ++ I)
DP [I] = 0;
For (I = 0; I <Len; ++ I) // The longest incremental Sequence Value of the last element with I
{
For (j = 0; j <= I; ++ J)
If (STR [J] <STR [I] & DP [J] + 1> DP [I]) // you must add the DP [J] + 1> DP [I] judgment here. You will see the following examples.
DP [I] = DP [J] + 1;
}
Int max = 0;
For (int K = 0; k <Len; ++ K)
If (DP [k]> MAX) max = DP [k];
Cout <MAX + 1 <Endl;
}
}
The following is an example of bcdefxay. if no judgment is added, the answer will be 6 (bcdefx), and the correct answer will be 7 (bcdefxy )., because DP [7] = d [a] + 1 = 2, the value is reduced ~~