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

Posted ldudxy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

以上是关于Educational Codeforces Round 61 (Rated for Div. 2)F(区间DP,思维,枚举)的主要内容,如果未能解决你的问题,请参考以下文章

Educational Codeforces Round 7 A

Educational Codeforces Round 7

Educational Codeforces Round 90

Educational Codeforces Round 33

Codeforces Educational Codeforces Round 54 题解

Educational Codeforces Round 27