Hdu4105 Electric wave

Source: Internet
Author: User

Question;

Add spaces to a string of consecutive numbers to divide them into the same number .. Sequential Number

The valley is the adjacent two numbers, which are smaller than the adjacent ones.

 


Dp [I] [j] [0, 1] indicates the maximum value of the number between I and j.

Dp equation:

Int tmp = is (I, j, k, p); // determines whether the number between I and j can be used as the number between k and p (after I) returns 0 if the two numbers are equal.
If (tmp = 1) // I j can be
Dp [I] [j] [0] = max (dp [I] [j] [0], dp [k] [p] [1] + 1 );
Else if (tmp <0) // I j can be the peak
Dp [I] [j] [1] = max (dp [I] [j] [1], dp [k] [p] [0] + 1 );

 

 

Thoughts:

It is another question that does not know how to represent the dp equation ..

The search solution report contains only four articles. dp is one-dimensional, dp is two-dimensional, dp is three-dimensional, and greedy is another ..

And I cannot understand it .. I tried their program for 0 ms, although it was a little bit better than the male, it was still 109 ms ..

Male said that it is difficult to set the State where it is handled.

The minimum one is how to use the sub-structure of the state you set to get the value of the current state.

Experience .. Try more later

 

# Include <iostream> # include <cmath> # include <cstdio> # include <vector> # include <cstring> # include <algorithm> using namespace std; int dp [105] [105] [2], n, num [105]; char s [105]; int is (int I, int j, int k, int p) {while (num [I] = 0) I ++; // remove the front 0 while (num [k] = 0) k ++; if (k> p) return-1; // impossible .. If (I> j) return 1; if (j-I) <(p-k) return 1; // determine the size of if (j-I)> (p-k) return-1; for (int l = 0; l <= (j-I); l ++) // compare {if (num [l + I] <num [k + l]) return 1; else if (num [l + I]> num [k + l]) return-1;} return 0; // indicates that each bit is equal, neither the peak nor the valley} int main () {int I, j, k, p, ans; while (~ Scanf ("% d", & n) {getchar (); gets (s); if (n = 1) // optimize it .. {Printf ("0 \ n"); continue;} if (n = 2) {if (s [0]> s [1]) printf ("0 \ n"); else printf ("1 \ n"); continue;} memset (dp, 0, sizeof dp); for (I = 0; I <n; I ++) num [I] = s [I]-'0'; for (I = 0; I <n; I ++) // there must be only one {dp [I] [n-1] [0] = 1 when I is retrieved from the initialization; dp [I] [n-1] [1] = 1;} for (I = n-2; I> = 0; I --) {for (j = I; j <n-1; j ++) {k = j + 1; for (p = k; p <n; p ++) {int tmp = is (I, j, k, p ); if (tmp = 1) // I j can be used when the trough dp [I] [j] [0] = max (dp [I] [j] [0], dp [k] [p] [1] + 1); else if (tmp <0) // I j can be the peak dp [I] [j] [1] = max (dp [I] [j] [1], dp [k] [p] [0] + 1);} // printf ("I: % d j: % d \ n", I, j, dp [I] [j] [0], dp [I] [j] [1]) ;}}ans = 0; for (I = 0; I <n; I ++) ans = max (ans, dp [0] [I] [0]); printf ("% d \ n", ans-1);} return 0 ;}

 

Related Article

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.