题目大意:

给定一个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. mysql 重点性能测试指标

    #qps 每秒钟查询数量 计算方式queries/seconds 查询总数/秒数show GLOBAL STATUS LIKE 'question%' #tps 每秒事务数 计算方式 (com_com ...

  2. dedecmsV5.7和discuz!X3.4整合之后免激活登陆

    问题:dedecmsv5.7和discuz!X3.4整合之后,从dede过去的用户,第一次登陆discuz!X3.4,需要激活.后来我就上百度了一番,找到了一个方法 我找到的方法: 1.在dedecm ...

  3. mysql Access denied for user root @localhost (using password:YES)错误

    C:\AppServ\MySQL> mysql -u root -p Enter password:  ERROR 1045 (28000): Access denied for user 'r ...

  4. python3 LDA主题模型以及TFIDF实现

    import codecs #主题模型 from gensim import corpora from gensim.models import LdaModel from gensim import ...

  5. linux-基础命令篇-02

    基本命令:--LS 关于显示的颜色含义: 白色:表示普通文件 蓝色:表示目录 绿色:表示可执行文件 红色:表示压缩文件 浅蓝色:链接文件 红色闪烁:表示链接的文件有问题 黄色:表示设备文件 灰色:表示 ...

  6. .net core 发送邮件

    var message = new MimeMessage();            //发送方            message.From.Add(new MailboxAddress(&qu ...

  7. 笨办法学python 文本复制

    本来面目 from sys import argv from os.path import exists script, from_file, to_file = argv print(f" ...

  8. spring(IOC)动态代理

    姓名:黄于霞      班级:软件151 1.引入Spring IOC的核心jar包,创建IOC的配置文件beans.xml,内容如下: 1 <?xml version="1.0&qu ...

  9. css--父元素塌陷

    当父元素内都是漂浮元素时,会造成父高度塌陷的问题.(因为等同于父元素内容为空,所以长,宽都等于空) 我们想要的页面结构是: <!DOCTYPE html> <html lang=&q ...

  10. 1.2 JAVA多线程实现

    线程和进程 进程:是执行中一段程序, 进程是系统进行资源分配和调度的一个独立单位. 线程:比进程更小的能独立运行的基本单位,单个进程中执行中每个任务就是一个线程.线程是进程中执行运算的最小单位. Th ...