Nyist OJ 17 monotonic increasing sequence of the eldest son (classical question of Dynamic Planning)

Source: Internet
Author: User
Monotonically incrementing maximum eldest son 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
3aaaababcabklmncdefg
Sample output
137
Source

Typical questions

Classic questions of dynamic planning; there seem to be several solutions. I am studying the most basic solutions;


Here I directly referred to the http://blog.csdn.net/sjf0115/article/details/8715672 blog, copy the picture here, I feel that it is not bad, deepen the understanding of this kind of questions;

Http://www.cnblogs.com/mycapple/archive/2012/08/22/2651453.html, this blog is also good

# Include <cstdio> # include <cstring> const int maxn = 10001; char s [maxn]; int DP [maxn], Max; void LICs () {int Len; memset (DP, 0, sizeof (DP); Len = strlen (s); For (INT I = 0; I <Len; I ++) {DP [I] = 1; // when an array is given, the initial value is 1. The maximum sequence of an array must have one character; For (Int J = 0; j <I; j ++) {If (s [I]> S [J] & DP [I] <1 + dp [J]) // recursive formula, if this position is greater than the previous character, add it to the incremental sequence DP [I] = 1 + dp [J] ;}} max = 0; for (INT I = 0; I <Len; I ++) // obtain the maximum value if (max <DP [I]) max = DP [I];} int main () {int t; scanf ("% d", & T); While (t --) {scanf ("% s", S); LICs (); printf ("% d \ n", max);} return 0 ;}

I saw the optimal code for this question. I wrote more than 300 ms for submission, and the optimal code can be up to 4 ms and 0 ms can be achieved;

But it's a bit difficult to understand. Save and learn;

  #include<iostream>#include <string>//#include <time.h>using namespace std;int main(){//freopen("1.txt","r",stdin);int n ;cin>>n;while(n--){string str;int count=1;cin>>str;int a[200];a[0]=-999;for (int i=0;i<str.length();i++){for (int j=count-1;j>=0;j--){if((int)str[i]>a[j]){a[j+1]=str[i];if(j+1==count) count++;break;}}}cout<<count-1<<endl;}//cout<<(double)clock()/CLOCKS_PER_SEC<<endl;return 0;}                


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.