Vasya and Binary String(来自codeforces
题目大意:
给定一个0/1字符串,每次你可以将此字符串中一段连续的任意长度的0/1子串消除掉,注意每次消除的子串中只能有0或者1一种字符,消除掉一串长度为i的0/1字符串会得到a[i]的收益,问将这个字符串完全消除的最大收益
Examples
Input
7
1101001
3 4 9 100 1 2 3
Output
109
Input
5
10101
3 10 15 15 15
Output
23
第一个样例是1101001 →→ 111001 →→ 11101 →→ 1111 →→ ∅ .
第二个样例是10101 →→ 1001 →→ 11 →→ ∅ .
区间dp+记忆化搜索
#include<bits/stdc++.h>
using namespace std;
long long n,i;
long long s[105];
long long a[105];
long long f[105][105][55];
long long dfs(long long l,long long r,long long x)
{
long long ans,j;
if(l>r)return 0;
if(f[l][r][x]!=-1)return f[l][r][x];
if(l==r)return f[l][r][x]=a[x]; ans=a[x]+dfs(l+1,r,1);
for(j=l;++j<=r;)
if(s[j]==s[l])ans=max(ans,dfs(l+1,j-1,1)+dfs(j,r,x+1));
return f[l][r][x]=ans;
}
int main()
{
for(scanf("%lld",&n),memset(f,-1,sizeof(f)),i=0;++i<=n;)scanf("%1lld",&s[i]);
for(i=0;++i<=n;)scanf("%d",&a[i]); printf("%lld",dfs(1,n,1));
return 0;
}
代码很简单(第一次在博客园写博客,有什么不足,请说明~~)
谢谢观赏~~
Vasya and Binary String(来自codeforces的更多相关文章
- 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,可以先合并完所有的点,即没相同的一段区间合并为 ...
- Codeforces1107E Vasya and Binary String 记忆化dp
Codeforces1107E 记忆化dp E. Vasya and Binary String Description: Vasya has a string \(s\) of length \(n ...
- [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 l ...
- Codeforces 1107E (Vasya and Binary String) (记忆化,DP + DP)
题意:给你一个长度为n的01串,和一个数组a,你可以每次选择消除一段数字相同的01串,假设消除的长度为len,那么收益为a[len],问最大的收益是多少? 思路:前两天刚做了POJ 1390,和此题很 ...
- CF1107E Vasya and Binary String
比赛的时候又被垃圾题艹翻了啊. 这个题显然是区间dp 考虑怎么转移. 类似消除方块和ZYB玩字符串那样的一个DP. 可以从左到右依次考虑消除. dp[l][r][k][flag]表示区间l,r左边粘着 ...
- CF - 1107 E Vasya and Binary String DP
题目传送门 题解: dp[ l ][ r ][ k ] 代表的是[l, r]这段区间内, 前面有k-1个连续的和s[l]相同且连续的字符传进来的最大值. solve( l, r, k) 代表的是处理 ...
- Codeforces1107E. Vasya and Binary String
题目链接 本题也是区间dp,但是需要保存的信息很多,是1还是0,有多少个连续的,那我们可以预处理,将所有的连续缩合成1个字符,那么字符串就变成了一个01交替的串,我们任意的消除1个部分,一定能引起连锁 ...
- Codeforces 862D. Mahmoud and Ehab and the binary string (二分)
题目链接:Mahmoud and Ehab and the binary string 题意: 一道交互题,首先给出一个字符串的长度l.现在让你进行提问(最多15次),每次提问提出一个字符串,会返回这 ...
随机推荐
- 关于PHP的mkdir函数
问题:dedecms5.7 php5.6 我想项目根目录下的uploads文件夹下动态创建一个文件夹/uploads/imgs $path = '/uploads/imgs'; mkdir($path ...
- 关于最新create-react-app使用react-app-rewired2.x添加webpack配置
使用过create-react-app(以下简称cra)的朋友都知道,这是react官方的一款脚手架工具,内部集成了babel,webpack,webpack-dev-server等等. 以前我们要在 ...
- SqlServer 查看最近执行过的语句
查看系统中最近执行的语句 ST.text AS '执行的SQL语句', QS.execution_count AS '执行次数', QS.total_elapsed_time AS '耗时', QS. ...
- python----数据驱动@ddt.file_data结合yaml文件的使用
一.创建yaml文件1. 安装yaml模块 pip install pyyaml2. 新建yaml文件 右键任意文件夹-->New-->File,输入文件名并以.yaml或.yml结尾 二 ...
- WinForm界面设计优化过程
以在做的项目为例,记录一下界面美化过程中遇到的问题,由于项目是先做出来之后,又请美工进行稍微调整设计界面,所以会又些限制 1. TabControl的问题----在添加了背景图片后,TabContro ...
- MySQL union all排序问题
mysql中多个查询结果排序后union之后是混乱的,可以在各个查询结果最后加limit 999999999再union,参考https://blog.csdn.net/chenyao1994/art ...
- 解决Eclipse中.properties文件中文乱码问题
在.properties文件写注释时,发现中文乱码了,由于之前在idea中有见设置.properties文件的编码类型,便找了找乱码原因 在中文操作系统中,Eclipse中的Java类型文件的编码的默 ...
- WEEK1
#变量 var1: name = input('name:') age = input('age:) job = input('job:) salary = input('salary:) info ...
- java数组冒泡排序
public class PaiXu1 { public static void main(String[] args) { int[] s = {345,879,456,870,221,902,14 ...
- Unity中Terrain中刷出的树木模糊解决办法
Billboard Start 该项用于设定摄像机将树渲染为广告牌的距离.