1260: [CQOI2007] Paint paint time limit:30 Sec Memory limit:64 MB
submit:893 solved:540
[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 Input Sample 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
It's easy to see that the interval DPbut I don't know how DPHelpless to see the puzzle. Today a a lot of questions but it seems that only one problem is their own thinking. If A[I]==A[J], then the optimal scheme is min (select I-(j-1) interval, or (i+1)-j interval, select (i+1)-(j-1) interval). Otherwise enumerate K f[i][j]=min (F[i][k]+f[k+1][j]).
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <cmath>5 6 #defineMAXN 527 8 using namespacestd;9 Ten CharS[MAXN]; One A intf[maxn][maxn],inf=99999999; - - intMain () the { -scanf"%s", s); -Memset (F, -/3,sizeof(f)); - intn=strlen (s); + for(intI=1; i<=n;i++) -f[i][i]=1; + for(inti=n-1; i>=1; i--) A for(intj=i+1; j<=n;j++) at if(s[i-1]==s[j-1]) -F[i][j]=min (f[i+1][j-1]+1, Min (f[i][j-1],f[i+1][j])); - Else - for(intk=i;k<j;k++) -F[i][j]=min (f[i][j],f[i][k]+f[k+1][j]); -printf"%d", f[1][n]); in return 0; -}
View Code
"DP" Bzoj 1260: [CQOI2007] Coloring Paint