Interval DP.
Use f[l][r] to indicate a minimum number of colors to be dyed from L to R.
State transition equation:
1.f[l][r]=min (F[l][i],f[i+1][r]) (L<=I<R) This dyeing is equal to two sections respectively stained, very good-looking out.
2.if (S[l]==s[r]) f[l][r]=min (min (f[l+1][r],f[l][r+1]), f[l+1][r-1]).
Two kinds of one can be applied directly to the two endpoints, 2 is to ignore the left and right end of the endpoint.
The 2nd role is critical, and it solves problems like ABACDA. After ignoring an endpoint, you can continue to find two endpoints for staining.
Why is it right? Because eventually a node needs to be painted at all, and all the same color ends are painted.
#include <cstdio>#include<algorithm>#include<cstring>using namespacestd;Const intMAXN = $+Ten;intN;intF[MAXN][MAXN];CharS[MAXN];intdpintLintr) {if(L>r)return 0; if(L==R)return 1; if(F[l][r])returnF[l][r]; int&res=F[l][r]; Res=r-l+1; for(inti=l;i<r;i++) Res=min (RES,DP (l,i) +DP (i+1, R)); if(S[l]==s[r]) res=min (res,min (Min (DP (l,r-1), DP (L +1, R)), DP (L +1, R-1)+1)); returnRes;}intMain () {scanf ("%s", s+1); printf ("%d\n", DP (1, strlen (s+1))); return 0;}
bzoj1260: [CQOI2007] Coloring Paint