Monotonic increasing sequence nyoj 17

Source: Internet
Author: User

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 ~~

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.