题目链接

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. 【luogu T34117 打油门】 题解

    王强怎么这么强啊 王强太强了 二维树状数组 #include <cstdio> #include <cstring> #include <iostream> #in ...

  2. HTML5标签应用

    <!doctype html> <html> <head> <style> /* *{border:1px solid red;height:20px} ...

  3. 【Linux-CentOS】CentOS安装Win双系统后Win启动项丢失及默认启动项修改

    转载自:搁浅bky,有部分更正,建议看此文. 1.Windows启动项消失的原因:   在安装Win7.8/10系统+CentOS7双系统后,默认会将mbr(Main Boot Record)改写为g ...

  4. springboot缓存的使用

    spring针对各种缓存实现,抽象出了CacheManager接口,用户使用该接口处理缓存,而无需关心底层实现.并且也可以方便的更改缓存的具体实现,而不用修改业务代码.下面对于在springboot中 ...

  5. Oracle 数据导出注意事项

    1.数据导出exp.expbd和imp.impbd 区别: exp,imp:既可以在客户端执行也可以在服务端执行,效率慢于expbd.impbd expbd.impbd:只能够在服务端执行,impbd ...

  6. 第13届景驰-埃森哲杯广东工业大学ACM程序设计大赛--E-回旋星空

    链接:https://www.nowcoder.com/acm/contest/90/E 来源:牛客网 1.题目描述 曾经有两个来自吉尔尼斯的人(A和C)恋爱了,他们晚上经常在一起看头上的那片名为假的 ...

  7. jquery点击按钮复制内容

    做移动端的项目遇到一个需求要点击按钮复制dom里的内容,看了很多资料显示必须textarea或者input里的内容才能简单复制,还有就是用插件的了,最终都因为遇到各种问题放弃,最终选择了最简单的点击复 ...

  8. 双击 ajax修改单元格里的值

    最终效果 列表页面表格里双击排序修改其值 按钮样式要引入bootstrap才可以用 本文件用的是laravel框架环境 larave路由里 Route::get('category/changesta ...

  9. Leecode刷题之旅-C语言/python-14.最长公共前缀

    /* * @lc app=leetcode.cn id=14 lang=c * * [14] 最长公共前缀 * * https://leetcode-cn.com/problems/longest-c ...

  10. HyperLedger Fabric 1.4 架构(6.2)

    6.2.1 架构演进       Fabric架构经历了0.6版本到1.0版本的演进,架构上进行了重大改进,从0.6版本的结构简单演进到可扩展.多通道的设计,在架构上有了质的飞跃:从1.0版本以后,架 ...