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次),每次提问提出一个字符串,会返回这 ...
随机推荐
- Confluence 6 升级以后
7. 拷贝你的数据库驱动 如果你现在使用的是 Oracle 或者 MySQL 数据库的话,你讲要重新拷贝 jdbc 驱动的 jar 文件到你已经存在的 Confluence 安装目录中 conflue ...
- bilinear pooling
一.双线性汇合的计算过程: 第一步,计算Gram 矩阵: 对于一组H×W×D的feature maps,$\boldsymbol{x}_{i} \in \mathbb{R}^{D}$是图像的深度描述, ...
- 8.4 GOF设计模式三: 外观模式 Facade
GOF设计模式三: 外观模式 Facade “现有系统”功能强大.复杂,开发“新系统”需要用到其中一部分,但又要增加一部 分新功能,该怎么办?4.1 Facade Pattern: Key Fea ...
- Codeforces Round #422 (Div. 2)E. Liar sa+st表+dp
题意:给你两个串s,p,问你把s分开顺序不变,能不能用最多k段合成p. 题解:dp[i][j]表示s到了前i项,用了j段的最多能合成p的前缀是哪里,那么转移就是两种,\(dp[i+1][j]=dp[i ...
- PHP获取POST的几种方法
一.PHP获取POST数据的几种方法 方法1.最常见的方法是:$_POST['fieldname'];说明:只能接收Content-Type: application/x-www-form-urlen ...
- java Data、String、Long三种日期类型之间的相互转换
java Data.String.Long三种日期类型之间的相互转换 // date类型转换为String类型 // formatType格式为yyyy-MM-dd HH:mm:ss// ...
- Public_1.bat
:: targetset targetFileName=Public_2set targetPath=./:: sourceset sourceFileName=Public_1@echo ui ru ...
- CSS3-2
倒圆角 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...
- Oracle 11g 单实例到单实例OGG同步实施文档-EXPDP初始化
Oracle 11g 单实例到单实例OGG同步实施文档-EXPDP初始化 2018-06-07 00:446470原创GoldenGate 作者: leo 本文链接:https://www.cndba ...
- easyui combobox模糊搜索
combobox实现模糊搜索功能 <input class="easyui-combobox" id="hybq_PADD" name="hyb ...