Interval DP:
DP (l, R) indicates that the range of [L, R] is the minimum number of coloring times of the target color.
Consider transfer:
L = = R is DP (L, r) = 1 (obviously)
s[l] = = s[L + 1] DP (l, r) = DP (L + 1, R) s[r] = = s[R-1] DP (l, r) = DP (l, R-1) because as long as the painting is more than one grid, it is obviously equal, so transfer it and do better
s[l] = = s[R] The DP (l, r) = Min (DP (l + 1, R), DP (L, R-1)) is equivalent to the interval [L, R] is colored so that it can be transferred to DP (L, R-1) or DP (L + 1, R)
Other conditions: DP (l, r) = Min (DP (l, K) + DP (k + 1, R)) (L <= K < R)
--------------------------------------------------------------------------
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#define REP (i, n) for (int i=0; i < n; ++i)#define CLR (x, C) memset (x, C, sizeof (x))using namespace std; const int MAXL =;int D[maxl][maxl], n;Char Goal[maxl];int dp (int l, int r) {int &t = d[l][r];if (t! =-1) return t;t = r-l + 1;if (goal[l] = = goal[l + 1]) return T = DP (l + 1, R);if (goal[r] = = Goal[r-1]) return t = DP (l, R-1);if (goal[l] = = Goal[r]) return t = Min (DP (l + 1, R), DP (L, R-1));for (int k = l; k < R; k++)t = min (t, DP (L, K) + DP (k + 1, R));return t;}int main () {freopen ("test.in", "R", stdin);CLR (d,-1);scanf ("%s", goal);n = strlen (goal);Rep (i, n) d[i][i] = 1;printf ("%d\n", DP (0, n-1));return 0;}
--------------------------------------------------------------------------
1260: [CQOI2007] Painted paint time limit: Sec Memory Limit: + MB
Submit: 818 Solved: 496
[Submit] [Status] [Discuss] Description assume that you have a 5-length block that has not been painted in any color at the beginning. You want to apply its 5 unit lengths to red, green, blue, green, and red, using a string of length 5 to indicate this target: RGBGR. Each time you can paint a continuous piece of wood into a given color, the color of the coat is covered with the first color. For example, for the first time, the wood planks were painted rrrrr, the second painted Rgggr, the third painted rgbgr, to achieve the goal. Achieve your goals with as few coloring times as possible. Input enters only one row, containing a string of length n, which is the coloring target. Each character in a string is an uppercase letter, different letters represent different colors, and the same letters represent the same color. Output has only one row, which contains a single number, which is the minimum number of paint times. Sample Inputsample Output"Sample Input 1"
AAAAA
"Sample Input 1"
Rgbgr
"Sample Output 1"
1
"Sample Output 1"
3
HINT
40% of the data meet: 1<=n<=10
100% of the data meet: 1<=n<=50
Source
BZOJ 1260: [CQOI2007] Painted paint (interval DP)