bzoj1260: [CQOI2007] Coloring Paint

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.