Educational Codeforces Round 61 (Rated for Div. 2) F. Clear the String(区间DP)(*)

题目链接
在这里插入图片描述
思路:区间dp,设f[l][r]是消除l…r区间所花的最小次数

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=505;
int n,dp[maxn][maxn];
char s[maxn];
int main()
{
	scanf("%d",&n);
	scanf("%s",s+1);
	for(int i=1;i<=n;++i) dp[i][i]=1;
	for(int len=2;len<=n;++len)
		for(int l=1;l+len-1<=n;++l)
		{
			int r=l+len-1;
			if(s[l]==s[r]) dp[l][r]=dp[l+1][r-1]+1;
			else dp[l][r]=min(dp[l+1][r],dp[l][r-1])+1;
			for(int k=l;k<=r;++k)
			dp[l][r]=min(dp[l][r],dp[l][k]+dp[k][r]-1);
		}
	printf("%d\n",dp[1][n]);
}
发布了391 篇原创文章 · 获赞 1 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_42479630/article/details/105543048