题目链接

https://vjudge.net/problem/CodeForces-1132F

题面

Description

You are given a string \(s\) of length \(n\) consisting of lowercase Latin letters. You may apply some operations to this string: in one operation you can delete some contiguous substring of this string, if all letters in the substring you delete are equal. For example, after deleting substring bbbb from string abbbbaccdd we get the string aaccdd.

Calculate the minimum number of operations to delete the whole string \(s\).

Input

The first line contains one integer \(n\) (\(1 \le n \le 500\)) — the length of string \(s\).

The second line contains the string \(s\) (\(|s| = n\)) consisting of lowercase Latin letters.

Output

Output a single integer — the minimal number of operation to delete string \(s\).

Examples

Input

5
abaca

Output

3

Input

8
abcddcba

Output

4

题解

区间dp,设\(f[l][r]\)是消除\(l...r\)区间所花的最小次数,对于每次更新:

  1. 如果\(s[l] == s[r]\), \(f[l][r] = f[l+1][r-1]+1\)
  2. 如果\(s[l] != s[r], f[l][r] = min(f[l + 1][r], f[l][r-1]) + 1\)
  3. 枚举中间点k,\(f[l][r] = min(f[l][r], f[l][k] + f[k][r] -1)\),这样的话k这个点删了两次,所以要减一,因为\(l, k, r\)可能被同时删去,所以要这样转移

AC代码

#include <bits/stdc++.h>
#define N 505
using namespace std;
int max(int a, int b) {
return a > b ? a : b;
}
int min(int a, int b) {
return a < b ? a : b;
}
int f[N][N];
char s[N];
int main() {
int n;
scanf("%d", &n);
scanf("%s", s + 1);
for (int i = 1; i <= n; i++) {
f[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]) f[l][r] = f[l + 1][r - 1] + 1;
else f[l][r] = min(f[l + 1][r], f[l][r - 1]) + 1;
for (int k = l; k <= r; k++) {
f[l][r] = min(f[l][r], f[l][k] + f[k][r] - 1);
}
}
}
printf("%d\n", f[1][n]);
return 0;
}

DP还是很玄学的啊...

CodeForces-1132F Clear the String的更多相关文章

  1. Codeforces #541 (Div2) - E. String Multiplication(动态规划)

    Problem   Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...

  2. 【CF1132F】Clear the String(动态规划)

    [CF1132F]Clear the String(动态规划) 题面 CF 题解 考虑区间\(dp\). 增量考虑,每次考虑最后一个字符和谁一起删去,然后直接转移就行了. #include<io ...

  3. 【Codeforces 1132F】Clear the String

    Codeforces 1132 F 题意:给一个串\(S\),问每次删除连续的一段相同字母,最少删几次将原串删空. 思路:考虑区间\(dp\),我们看要删多少次能把\([l,r]\)删空,那么最终答案 ...

  4. codeforces#1132 F. Clear the String(神奇的区间dp)

    题意:给出一个字符串S,|S|<=500.每次操作可以删除一段连续的相同字母的子串.问,最少操作多少次可以把这个字符串变成空串. 分析:刚开始的思路是,把连续的串给删除掉,然后再....贪心.完 ...

  5. 【动态规划】【最短路】Codeforces 710E Generate a String

    题目链接: http://codeforces.com/problemset/problem/710/E 题目大意: 问写N个字符的最小花费,写一个字符或者删除一个字符花费A,将当前的字符数量翻倍花费 ...

  6. codeforces 632C The Smallest String Concatenation

    The Smallest String Concatenation 题目链接:http://codeforces.com/problemset/problem/632/C ——每天在线,欢迎留言谈论. ...

  7. CodeForces 632C The Smallest String Concatenation//用string和sort就好了&&string的基础用法

    Description You're given a list of n strings a1, a2, ..., an. You'd like to concatenate them togethe ...

  8. 【Codeforces 1120C】Compress String

    Codeforces 1120 C 题意:给一个串\(S\),将这个串分成\(t_1..t_m\),如果\(t_i\)在\(t_1..t_{i-1}\)中作为子串出现过,那么这个的代价是\(b\),否 ...

  9. 【codeforces 797C】Minimal string

    [题目链接]:http://codeforces.com/contest/797/problem/C [题意] 一开始,给你一个字符串s:两个空字符串t和u; 你有两种合法操作; 1.将s的开头字符加 ...

  10. Codeforces 1144 E. Median String

    原题链接:https://codeforces.com/problemset/problem/1144/E tag:字符串模拟,大整数. 题意:给定两个字符串,求字典序中间串. 思路:可以把这个题当做 ...

随机推荐

  1. 网页静态化技术Freemarker

    1.为什么要使用网页静态化技术 网页静态化解决方案在实际开发中运用比较多,例如新闻网站,门户网站中的新闻频道或者是文章类的频道. 对于电商网站的商品详细页来说,至少几百万个商品,每个商品又有大量的信息 ...

  2. code First 三 Fluent API

    Entity Framework Fluent API用于配置域类以覆盖约定. 在实体框架6中,DbModelBuilder类充当Fluent API,我们可以使用它来配置许多不同的东西.它提供了比数 ...

  3. generative models

    A generative model G can be seen as taking a random seed h (say, a sample from a multivariate Normal ...

  4. Openresty最佳案例 | 汇总

    转载请标明出处: http://blog.csdn.net/forezp/article/details/78616856 本文出自方志朋的博客 目录 Openresty最佳案例 | 第1篇:Ngin ...

  5. SpringBoot非官方教程 | 第三篇:SpringBoot用JdbcTemplates访问Mysql

    转载请标明出处: 原文首发于https://www.fangzhipeng.com/springboot/2017/07/11/springboot3-JdbcTemplates-Mysql/ 本文出 ...

  6. jenkins添加环境变量 ,win 10的 环境变量如下,win7 就是不加也可以运行,不报 “python 不是内部命令 ” 的错误。

    jenkins 添加win 10的 环境变量如下,win7 就是不加也可以运行,不报 “python 不是内部命令 ” 的错误,暂时不知道怎么回事.    jenkins这样添加环境变量 .

  7. 初涉基环外向树dp&&bzoj1040: [ZJOI2008]骑士

    基环外向树dp竟然如此简单…… Description Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各界的赞扬.最近发生了一件可怕的事情,邪恶的Y国发 ...

  8. chromium之non_thread_safe

    先看看介绍 // A helper class used to help verify that methods of a class are // called from the same thre ...

  9. Percona-Tookit工具包之pt-kill

      Preface       Sometimes,we are determined to kill some MySQL connections which are occupying huge ...

  10. Redis学习推荐

    Redis快速入门 https://www.yiibai.com/redis/redis_quick_guide.html Redis用途和使用场景 https://blog.csdn.net/wei ...