码迷,mamicode.com
首页 > 其他好文 > 详细

BZOJ 1260 涂色

时间:2016-11-02 01:28:11      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:string   bzoj   cstring   print   stream   bsp   str   can   include   

区间dp。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 55
#define inf 2000000000
using namespace std;
int n,dp[maxn][maxn];
char s[maxn];
int main()
{
    scanf("%s",s);n=strlen(s);
    for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++)
        {
            if (i==j) dp[i][j]=1;
            else if (i<j) dp[i][j]=inf;
            else dp[i][j]=0;
        }
    for (int l=2;l<=n;l++)
        for (int i=1;i<=n-l+1;i++)
        {
            int left=i,right=i+l-1;
            for (int k=left;k<=right-1;k++)
                dp[left][right]=min(dp[left][right],dp[left][k]+dp[k+1][right]-(s[left-1]==s[right-1]));
        }
    printf("%d\n",dp[1][n]);
    return 0;
}

 

BZOJ 1260 涂色

标签:string   bzoj   cstring   print   stream   bsp   str   can   include   

原文地址:http://www.cnblogs.com/ziliuziliu/p/6021367.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!