[CF1107E]Vasya and Binary String【区间DP】
题目描述
Vasya has a string s of length n consisting only of digits 0 and 1. Also he has an array a of length n.
Vasya performs the following operation until the string becomes empty: choose some consecutive substring of equal characters, erase it from the string and glue together the remaining parts (any of them can be empty). For example, if he erases substring 111 from string 111110 he will get the string 110. Vasya gets ax points for erasing substring of length x.
Vasya wants to maximize his total points, so help him with this!
{题目从CF盗取)
题目大意
给你一个长度为n(1<=n<=100)的01串s,从s中删除一段连续的长度为i的所有字符都相同的子串将会得到ai的分数,如果执行上述操作直到s被删除完,求最大分数和。
(题意从lg盗取)
题解
一道简单的区间DP,也不清楚为什么在洛谷上是一道黑题,可能是我太菜了。(废话不多说)
\(f[i][j][k]\)表示消除了\([j,k]\)后,在\(j\)之前有\(i\)个字符和\(j\)相同的消除的最大值。
考虑初始值,一开始\(f[i][L][R]=a[i+1]+f[0][L][R]\)。\(L,R\)是当前的区间,(从0开始标号)
转移:\(f[k][L][R]=max(f[k][L][R],f[0][L+1][l-1]+f[k+1][l][R])\)(L和R是当前的区间)
ac代码
# include <bits/stdc++.h>
# define LL long long
using namespace std;
inline int gi(){
int w=0,x=0;char ch=0;
while(!isdigit(ch)) w|=ch=='-',ch=getchar();
while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
return w?-x:x;
}
# define N 105
LL f[N][N][N];
int a[N],n,m;
char s[N];
int main(){
n=gi();
scanf("%s",s);
for (register int i=1;i<=n;i++) a[i]=gi();
for (register int i=0;i<n;i++)
for (register int j=0;j+i<n;j++)
for (register int k=0;k<n;k++){
f[k][j][j+i]=a[k+1]+f[0][j+1][j+i];
for (register int l=j+1;l<=j+i;l++)
if (s[j]==s[l])
f[k][j][j+i]=max(f[k][j][j+i],f[0][j+1][l-1]+f[k+1][l][j+i]);
}
printf("%lld\n",f[0][0][n-1]);
return 0;
}
[CF1107E]Vasya and Binary String【区间DP】的更多相关文章
- CF1107E Vasya and Binary String
比赛的时候又被垃圾题艹翻了啊. 这个题显然是区间dp 考虑怎么转移. 类似消除方块和ZYB玩字符串那样的一个DP. 可以从左到右依次考虑消除. dp[l][r][k][flag]表示区间l,r左边粘着 ...
- Codeforces1107E Vasya and Binary String 记忆化dp
Codeforces1107E 记忆化dp E. Vasya and Binary String Description: Vasya has a string \(s\) of length \(n ...
- Codeforces 1107 E - Vasya and Binary String
E - Vasya and Binary String 思路:区间dp + 记忆化搜索 转移方程看上一篇博客. 代码: #pragma GCC optimize(2) #pragma GCC opti ...
- CF 1107 E. Vasya and Binary String
E. Vasya and Binary String 链接 分析: 对于长度为x的一段序列,我们可以dp出消除的过程的最优方案,背包即可. 然后区间dp,可以先合并完所有的点,即没相同的一段区间合并为 ...
- Codeforces Round #354 (Div. 2)-C. Vasya and String,区间dp问题,好几次cf都有这种题,看来的好好学学;
C. Vasya and String time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Vasya and Binary String(来自codeforces
题目大意: 给定一个0/1字符串,每次你可以将此字符串中一段连续的任意长度的0/1子串消除掉,注意每次消除的子串中只能有0或者1一种字符,消除掉一串长度为i的0/1字符串会得到a[i]的收益,问将这个 ...
- Codeforces1107E. Vasya and Binary String
题目链接 本题也是区间dp,但是需要保存的信息很多,是1还是0,有多少个连续的,那我们可以预处理,将所有的连续缩合成1个字符,那么字符串就变成了一个01交替的串,我们任意的消除1个部分,一定能引起连锁 ...
- CF - 1107 E Vasya and Binary String DP
题目传送门 题解: dp[ l ][ r ][ k ] 代表的是[l, r]这段区间内, 前面有k-1个连续的和s[l]相同且连续的字符传进来的最大值. solve( l, r, k) 代表的是处理 ...
- Codeforces 1107E (Vasya and Binary String) (记忆化,DP + DP)
题意:给你一个长度为n的01串,和一个数组a,你可以每次选择消除一段数字相同的01串,假设消除的长度为len,那么收益为a[len],问最大的收益是多少? 思路:前两天刚做了POJ 1390,和此题很 ...
随机推荐
- (Git 学习)Git SSH Key 创建步骤
首先感谢segmentfalut上的朋友对我帮助. 首先:查看你是否有../ssh 这个文件:怎么查看:找到你的git安装目录,在安装目录下查看是否./ssh,以我的为例: 在C盘/Users/11/ ...
- Django之Django终端打印SQL语句
Django之Django终端打印SQL语句 在Django项目中,settings.py文件中,在最后添加如下代码即可实现在Django终端打印SQL语句. LOGGING = { 'version ...
- Python之字符串操作
一.字符串特点 内容不可修改 password=' #内容不可修改 二.字符串常用方法 1..strip()方法 去字符串两边的空格和换行符 print(password.strip()) #去掉字符 ...
- C语言操作WINDOWS系统存储区数字证书相关函数详解及实例
C语言操作WINDOWS系统存储区数字证书相关函数详解及实例 以下代码使用C++实现遍历存储区证书及使用UI选择一个证书 --使用CertOpenSystemStore打开证书存储区. --在循环中 ...
- Python PEP8 编码规范中文版
原文链接:https://legacy.python.org/dev/peps/pep-0008/ 参考:https://blog.csdn.net/ratsniper/article/details ...
- C# web发布设置
1.配置文件设置: 选择"自定义",配置文件框自己输入. 2.连接设置: 3.发布版本设置 4.预览 预览没问题点发布即可.
- Sqlserver tablediff的简单使用
1. 先列举一下自己简单的比较语句 tablediff -sourceserver 10.24.160.73 -sourcedatabase cwbasemi70 -sourceschema lcmi ...
- CSS3 background-size属性兼容
background-size是CSS3新增的属性,但是IE8以下还是不支持 background-size:contain; // 缩小图片来适应元素的尺寸(保持像素的长宽比):background ...
- WPF设置软件界面背景为MediaElement并播放视频
在我们的常见的软件界面设计中我们经常会设置软件的背景为SolidColorBrush或者LinerColorBrush.RadialGradientBrush 等一系列的颜色画刷为背景,有时我们也会使 ...
- TP5上传图片
模板: <form action="{:url('Temp/addTempDo')}" enctype="multipart/form-data" met ...