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 ;}