Monotonically incrementing and monotonic incrementing

Source: Internet
Author: User

Monotonically incrementing and monotonic incrementing
Monotonically incrementing longest 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

3 aaaababcabklmncdefg

Sample output

137

 

Program code:
# Include <stdio. h>

# Include <string. h>

# Include <stdlib. h>

 

Int leng1, leng2 = 26;

Int num [27] [10001];

Char errors [10001], ch2 [] = "abcdefghijklmnopqrstuvwxyz ";

 

Void LCSLength ()

{

Int I, j;

For (I = 1; I <= leng2; I ++)

For (j = 1; j <= leng1; j ++)

{

If (else [J-1] = ch2 [I-1])

Num [I] [j] = num [I-1] [J-1] + 1;

Else

{

If (num [I-1] [j]> num [I] [J-1])

Num [I] [j] = num [I-1] [j];

Else

Num [I] [j] = num [I] [J-1];

}

}

}

 

 

Int main ()

{

Int n;

Scanf ("% d", & n );

Getchar ();

While (n --)

{

Scanf ("% s", callback );

Leng1 = strlen (dependencies );

LCSLength ();

Printf ("% d \ n", num [leng2] [leng1]);

}

System ("pause ");

Return 0;

}

 

 


On which page is the introduction to algorithms?

Chapter 2: Dynamic Planning in the form of exercises

Design an O (n2) time algorithm to find the longest monotonic increasing subsequence of the series composed of n numbers

Just use the greedy algorithm.

Create a stack and scan the entire sequence sequentially.
The first element is first placed in the stack,
For each element scanned later, if it is larger than the top element of the stack, it will be pushed into the stack. If it is smaller than the top element of the stack, it will be played one by one until it is found that the top element of the stack is smaller than it.
Record the stack length at a time and its maximum value. After a sequence scan is completed, the maximum length of the stack is the longest sequence, and the element in the stack at the maximum length is the largest incremental sequence.
This algorithm is O (N ^ 2) level, but faster than dp.
 

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.