Hdu4105 too many electric Electric Wave

Source: Internet
Author: User
Tags first string
Electric Wave

Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 596 accepted submission (s): 173

Problem descriptionali was doing a physic experiment which requires him to observe an electric wave. he needs the height of each peak value and valley value for further study (a peak value means the value is strictly larger than its neighbors and
Valley value means the value is strictly smaller than its neighbors ). he did write these numbers down but he was too careless that he wrote them in a line without separations, such as "712495" may represent "7 12 4 9 5 ". the only information he can remember
Was:
1. The data begins with a valley value
2. Each value is either a peak value or a valley value
Now he wants to insert blanks to make the data valid. If multiple solutions exist, he will choose the one with more blanks.

Inputthe input consists several testcases.
The first line contains one integer N (1 <= n <= 100), the length of the data.
The second line contains one string S, the data he recorded.
S contains only digits.

Outputprint one integer, the maximum number of blanks he can insert.

Sample Input

 
6712495

Sample output

 
4HintThe separated data may have leading zeros. 

Source2011 Alibaba-cup campus contest is to divide a string into small strings to ensure that the string is small and small. We use DP [flag] [I] [J] to indicate whether the first string is Valley or peak, the first step is from I to J. DP [flag] [I] [J] = fmax (DP [flag ^ 1] [J + 1] [k]), the complexity is n ^ 3. However, there is no good optimization method. There is no problem with the question!

# Include <stdio. h> # include <iostream> # include <string. h> using namespace STD; char STR [105]; int DP [2] [105] [105]; int compare (int I, Int J, int A, int B) // The first small returns 0 large returns 1 {int C; while (STR [I] = '0' & I <j) {I ++ ;} while (STR [a] = '0' & A <B) {A ++;} int len1 = J-I, len2 = B-; if (len1 <len2) {return 0;} else if (len1> len2) {return 1;} else {for (C = 0; C <= len1; C ++) {If (STR [I + C]! = STR [A + C]) {If (STR [I + C] <STR [A + C]) {return 0 ;}else {return 1 ;}}} return-1 ;}} int fmax (int A, int B) {If (A> B) return a; return B ;}int main () {int N, flag, i, J, K; while (scanf ("% d", & N )! = EOF) {scanf ("% s", STR); memset (DP, 0, sizeof (DP); {for (I = n-1; I> = 0; I --) {for (j = I; j <n; j ++) {for (flag = 0; flag <2; flag ++) for (k = J + 1; k <n; k ++) {If (flag ^ 1) = compare (I, j, J + 1, k )) DP [flag] [I] [J] = fmax (DP [flag] [I] [J], 1 + dp [flag ^ 1] [J + 1] [k]) ;}}} int Maxx = DP [1] [0] [0]; for (I = 0; I <n; I ++) {If (DP [1] [0] [I]> Maxx) {Maxx = DP [1] [0] [I] ;}} printf ("% d \ n", Maxx) ;}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.