题目链接

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. NDK下载地址

    官方下载地址 http://developer.android.com/ndk/downloads/index.html 没有提供旧版下载链接,只能修改链接方式下载 http://dl.google. ...

  2. 如何用Redlock实现分布式锁

    转载请标明出处: http://blog.csdn.net/forezp/article/details/70305336 本文出自方志朋的博客 之前写过一篇文章<如何在springcloud分 ...

  3. 使用Windows服务定时去执行一个方法的三种方式

    方式一:使用System.Timers.Timer定时器 public partial class Service1 : ServiceBase { private UnitOfWork unitOf ...

  4. PTA Java tips(转载)

    在PTA提交Java程序需要注意如下几个要点 1. Main类与Scanner 1.1 Main类 你提交的所有程序都应该以如下形式出现 public class Main{ public stati ...

  5. ABAP术语-Authorization Check

    Authorization Check 原文:http://www.cnblogs.com/qiangsheng/archive/2007/12/19/1005490.html Check perfo ...

  6. HTTP头部信息解释分析

    HTTP 头部解释 1. Accept:告诉WEB服务器自己接受什么介质类型,*/* 表示任何类型,type/* 表示该类型下的所有子类型,type/sub-type. 2. Accept-Chars ...

  7. chrome浏览器中 F12 功能的简单介绍

    chrome浏览器中 F12 功能的简单介绍 由于F12是前端开发人员的利器,所以我自己也在不断摸索中,查看一些博客和资料后,自己总结了一下来帮助自己理解和记忆,也希望能帮到有需要的小伙伴,嘿嘿! 首 ...

  8. 爬虫——Selenium与PhantomJS

    Selenium Selenium是一个Web的自动化测试工具,最初是为网站自动化测试而开发的,类型像我们玩游戏用的按键精灵,可以按指定的命令自动操作,不同的是Selenium可以直接运行在浏览器上, ...

  9. 关于linux命令的说明

    开始前我们必须先认识绝对路径与相对路径 绝对路径是从盘符开始的路径 :例如:/etc/sysconfig/network (从根直接指到network) 相对路径是从当前自己所在位置开始的路径:例如我 ...

  10. js中面向对象(创建对象的几种方式)

    1.面向对象编程(OOP)的特点: 抽象:抓住核心问题 封装:只能通过对象来访问方法 继承:从已有的对象下继承出新的对象 多态:多对象的不同形态 一.创建对象的几种方式 javascript 创建对象 ...