Given a piece of wood board, each of the above positions has a color. Ask how many times the brush can reach this color sequence.
For dynamic planning, you can re-process it first (not necessary), so that f [I] [J] indicates the minimum number of times that J positions starting with I are flushed into the corresponding color sequence, the status transfer is as follows:
If s [I] = s [J] Then f [I] [J] = min (F [I-1] [J], F [I] [J-1]) combine I and the right half into a brush, or combine J and the left half into a brush.
If s [I]! = S [J], F [I] [J] = min {f [I] [k] + F [I + k] [J-K]}, where 1 <= k <j
Then I can solve the problem... Get a greedy error status transfer...
# Include <cstdio> # include <cstring> # include <iostream> # include <algorithm> # define M 60 using namespace STD; int N; char s [m]; int f [m] [m]; int main () {int I, j, k; scanf ("% s", S + 1); for (I = 1; s [I]; I ++) if (s [I]! = S [I-1]) s [+ + N] = s [I]; memset (F, 0x3f, sizeof F); for (I = 1; I <= N; I ++) f [I] [1] = 1; for (j = 2; j <= N; j ++) for (I = 1; I + J-1 <= N; I ++) {If (s [I] = s [I + J-1]) f [I] [J] = min (F [I + 1] [J], F [I] [J-1]); For (k = 1; k <J; k ++) f [I] [J] = min (F [I] [J], f [I] [k] + F [I + k] [J-K]);} cout <F [1] [N] <Endl ;}
Bzoj 1260 cqoi2007 color paint Dynamic Planning