Educational Codeforces Round 61 (Rated for Div. 2)F(区间DP,思维,枚举)

#include<bits/stdc++.h>
typedef long long ll;
const int inf=0x3f3f3f3f;
using namespace std;
char b[507];
int dp[507][507];
int main(){
    memset(dp,0x3f,sizeof(dp));
    int n;
    scanf("%d",&n);
    scanf("%s",b+1);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            if(i==j)
                dp[i][j]=1;
            else
                dp[i][j]=inf;
    for(int len=1;len<=n;len++)//中间段长度
        for(int l=1,r;(r=l+len)<=n;l++)//枚举起点,枚举终点
            if(b[l]==b[r])
                if(len==1)
                    dp[l][r]=1;//初始为1
                else
                    dp[l][r]=min(min(dp[l+1][r],dp[l][r-1]),dp[l+1][r-1]+1);//更新最小值
            else
                for(int k=l;k<r;k++)
                    dp[l][r]=min(dp[l][r],dp[l][k]+dp[k+1][r]);//区间更新
    printf("%d",dp[1][n]);
}

//类似cf#538D

posted @ 2019-03-06 23:12  sewage  阅读(119)  评论(0编辑  收藏  举报