题目大意:

给定一个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的更多相关文章

  1. Codeforces 1107 E - Vasya and Binary String

    E - Vasya and Binary String 思路:区间dp + 记忆化搜索 转移方程看上一篇博客. 代码: #pragma GCC optimize(2) #pragma GCC opti ...

  2. CF 1107 E. Vasya and Binary String

    E. Vasya and Binary String 链接 分析: 对于长度为x的一段序列,我们可以dp出消除的过程的最优方案,背包即可. 然后区间dp,可以先合并完所有的点,即没相同的一段区间合并为 ...

  3. Codeforces1107E Vasya and Binary String 记忆化dp

    Codeforces1107E 记忆化dp E. Vasya and Binary String Description: Vasya has a string \(s\) of length \(n ...

  4. [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 ...

  5. Codeforces 1107E (Vasya and Binary String) (记忆化,DP + DP)

    题意:给你一个长度为n的01串,和一个数组a,你可以每次选择消除一段数字相同的01串,假设消除的长度为len,那么收益为a[len],问最大的收益是多少? 思路:前两天刚做了POJ 1390,和此题很 ...

  6. CF1107E Vasya and Binary String

    比赛的时候又被垃圾题艹翻了啊. 这个题显然是区间dp 考虑怎么转移. 类似消除方块和ZYB玩字符串那样的一个DP. 可以从左到右依次考虑消除. dp[l][r][k][flag]表示区间l,r左边粘着 ...

  7. CF - 1107 E Vasya and Binary String DP

    题目传送门 题解: dp[ l ][ r ][ k ] 代表的是[l, r]这段区间内, 前面有k-1个连续的和s[l]相同且连续的字符传进来的最大值. solve( l, r, k) 代表的是处理 ...

  8. Codeforces1107E. Vasya and Binary String

    题目链接 本题也是区间dp,但是需要保存的信息很多,是1还是0,有多少个连续的,那我们可以预处理,将所有的连续缩合成1个字符,那么字符串就变成了一个01交替的串,我们任意的消除1个部分,一定能引起连锁 ...

  9. Codeforces 862D. Mahmoud and Ehab and the binary string (二分)

    题目链接:Mahmoud and Ehab and the binary string 题意: 一道交互题,首先给出一个字符串的长度l.现在让你进行提问(最多15次),每次提问提出一个字符串,会返回这 ...

随机推荐

  1. java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context 错误

    spring boot 项目启动报错:原因一般是注入了相同名字的service -- :: com.gxcards.mes.MainWwwWeb: logStartupProfileInfo INFO ...

  2. SpringBoot之配置文件加载位置

    1.SpringBoot启动会扫描application.properties或者application.yml文件作为springboot的配置文件.默认创建项目生成application.prop ...

  3. document.body.scrollTop和document.documentElement.scrollTop 以及值为0的问题

    转自http://wo13145219.iteye.com/blog/2001598 一.先遇到document.body.scrollTop值为0的问题 做页面的时候可能会用到位置固定的层,读取do ...

  4. what API can do

    APIs for manipulating documents loaded into the browser. The most obvious example is the DOM (Docume ...

  5. IDEA(添加类注释以及方法注释)

    添加类注释: File---Setting----Editor----Code Style-----File and    Code  Templates--------Class #if (${PA ...

  6. Js 框架之Vue .JS学习记录 ① 与Vue 初识

    目录 与 Vue.js 认识 VUE.JS是个啥?有啥好处? Vue 的目标 战前准备  VUE.JS环境 VUE.JS 尝试一下,你就明白 第一步 实例化VUE 对象 第二步VueAPP 调用数据 ...

  7. 983. Minimum Cost For Tickets

    网址:https://leetcode.com/problems/minimum-cost-for-tickets/ 参考:https://leetcode.com/problems/minimum- ...

  8. MACE移植要求

    MACE支持Tensorflow的depth_to_space和space_to_depth,以及strided_slice算子. 其中depth_to_space可以用来无平滑地进行上采样. spa ...

  9. oracle 12g,断电,数据库启动不了,无法登录 并且 报ora-00119和ora-00132错误

    oracle 12g,断电,数据库启动不了,无法登录 前提:服务全部打开,监听也配置好了! 可以参考网站:https://jingyan.baidu.com/article/5552ef47c73ee ...

  10. JS设置、获取和取消Cookie

    // 设置cookie   function setCookie(name, value, seconds, domain) {       seconds = seconds || 0; // se ...