CodeForces-1132F Clear the String
题目链接
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\)区间所花的最小次数,对于每次更新:
- 如果\(s[l] == s[r]\), \(f[l][r] = f[l+1][r-1]+1\)
- 如果\(s[l] != s[r], f[l][r] = min(f[l + 1][r], f[l][r-1]) + 1\)
- 枚举中间点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的更多相关文章
- Codeforces #541 (Div2) - E. String Multiplication(动态规划)
Problem Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...
- 【CF1132F】Clear the String(动态规划)
[CF1132F]Clear the String(动态规划) 题面 CF 题解 考虑区间\(dp\). 增量考虑,每次考虑最后一个字符和谁一起删去,然后直接转移就行了. #include<io ...
- 【Codeforces 1132F】Clear the String
Codeforces 1132 F 题意:给一个串\(S\),问每次删除连续的一段相同字母,最少删几次将原串删空. 思路:考虑区间\(dp\),我们看要删多少次能把\([l,r]\)删空,那么最终答案 ...
- codeforces#1132 F. Clear the String(神奇的区间dp)
题意:给出一个字符串S,|S|<=500.每次操作可以删除一段连续的相同字母的子串.问,最少操作多少次可以把这个字符串变成空串. 分析:刚开始的思路是,把连续的串给删除掉,然后再....贪心.完 ...
- 【动态规划】【最短路】Codeforces 710E Generate a String
题目链接: http://codeforces.com/problemset/problem/710/E 题目大意: 问写N个字符的最小花费,写一个字符或者删除一个字符花费A,将当前的字符数量翻倍花费 ...
- codeforces 632C The Smallest String Concatenation
The Smallest String Concatenation 题目链接:http://codeforces.com/problemset/problem/632/C ——每天在线,欢迎留言谈论. ...
- 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 ...
- 【Codeforces 1120C】Compress String
Codeforces 1120 C 题意:给一个串\(S\),将这个串分成\(t_1..t_m\),如果\(t_i\)在\(t_1..t_{i-1}\)中作为子串出现过,那么这个的代价是\(b\),否 ...
- 【codeforces 797C】Minimal string
[题目链接]:http://codeforces.com/contest/797/problem/C [题意] 一开始,给你一个字符串s:两个空字符串t和u; 你有两种合法操作; 1.将s的开头字符加 ...
- Codeforces 1144 E. Median String
原题链接:https://codeforces.com/problemset/problem/1144/E tag:字符串模拟,大整数. 题意:给定两个字符串,求字典序中间串. 思路:可以把这个题当做 ...
随机推荐
- 【luogu P1111 公路修建】 题解
题目链接:https://www.luogu.org/problemnew/show/P1111 考察并查集,运用kruskal的思想很好做.注意几个小问题即可. #include<iostre ...
- GYM 101550 G.Game Rank(模拟)
The gaming company Sandstorm is developing an online two player game. You have been asked to impleme ...
- tomcate8配置多个二级域名问题解决根目录空白2017年12月9日
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDepl ...
- OS_EVENT 信号量
1. OS_EVENT *T2sem=(OS_EVENT *)0; 这句代码的意思是 把OS_EVENT类型的一个指针T2sem赋值为0: 其中 OS_EVENT是数据类型,*代表是指针类型,(O ...
- 兼容性良好的 sticky-footer 布局
<div class="content"> <div class="content-wrapper"> <div class=&q ...
- TCP和UDP的现实应用
以下应用的区分是基于TCP可靠传输,UDP不可靠传输 TCP一般用于文件传输(FTP HTTP 对数据准确性要求高,速度可以相对慢),发送或接收邮件(POP IMAP SMTP 对数据准确性要求高,非 ...
- 关于 NSData 的数据类型(2进制,16进制之间)及深入剖析(转)
. NSData 与 NSString NSData-> NSString NSString *aString = [[NSString alloc initWithData:adataenco ...
- NSString+JSON - iOS
日常开发中常用的一个相互转换的方法; 直接创建对应的类,引用如下方法即可实现; 具体 code 如下: 声明: #import <Foundation/Foundation.h> @int ...
- Spring初始介绍
一.spring介绍 三层架构中spring位置: spring:对象的容器,相当于map容器,已经存好了相应的对象,而这三层对象(web层,service层,)进行创建时,不需要在进行new对象,s ...
- linux运维、架构之路-shell编程(一)
一.shell编程入门必备基础 1.vim编辑器的命令,vimrc设置 2.150个linux基础命令 3.linux中基础的系统服务crond,ssh网络服务,nfs,rsync,inotify,l ...