比赛的时候又被垃圾题艹翻了啊。

这个题显然是区间dp

考虑怎么转移。

类似消除方块和ZYB玩字符串那样的一个DP。

可以从左到右依次考虑消除。

dp[l][r][k][flag]表示区间l,r左边粘着k个flag。

转移方式:

1.考虑强行去继续黏上下一个字符并使k+1。

2.考虑把一段跳过去,让被跳过的这一段自行不与外界产生关系被消除,然后再去消除后面的。

3.直接消除当前粘着的一段,并获得收益。

#include<iostream>
#include<cctype>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#define N 110
#define L 100
#define eps 1e-7
#define inf 1e9+7
#define db double
#define ll long long
#define ldb long double
using namespace std;
inline ll read()
{
char ch=0;
ll x=0,flag=1;
while(!isdigit(ch)){ch=getchar();if(ch=='-')flag=-1;}
while(isdigit(ch)){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return x*flag;
}
char s[N];
ll a[N],f[N],dp[N][N][N][2];
ll dfs(ll l,ll r,ll k,ll flag)
{
if(l>r)return a[k];
if(dp[l][r][k][flag]!=-1)return dp[l][r][k][flag];
if(!k||(k&&f[l]==flag))
dp[l][r][k][flag]=max(dp[l][r][k][flag],dfs(l+1,r,k+1,f[l]));
for(ll i=l;i<=r;i++)
dp[l][r][k][flag]=max(dp[l][r][k][flag],dfs(l,i,0,0)+dfs(i+1,r,k,flag));
for(ll i=1;i<=k;i++)
dp[l][r][k][flag]=max(dp[l][r][k][flag],dfs(l,r,k-i,flag)+a[i]);
return dp[l][r][k][flag];
}
int main()
{
ll n=read();
scanf("%s",s+1);
for(ll i=1;i<=n;i++)a[i]=read(),f[i]=s[i]-'0';
memset(dp,-1,sizeof(dp));
printf("%lld",dfs(1,n,0,0));
return 0;
}

CF1107E Vasya and Binary String的更多相关文章

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

  2. Codeforces 1107 E - Vasya and Binary String

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

  3. CF 1107 E. Vasya and Binary String

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

  4. Codeforces1107E Vasya and Binary String 记忆化dp

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

  5. Vasya and Binary String(来自codeforces

    题目大意: 给定一个0/1字符串,每次你可以将此字符串中一段连续的任意长度的0/1子串消除掉,注意每次消除的子串中只能有0或者1一种字符,消除掉一串长度为i的0/1字符串会得到a[i]的收益,问将这个 ...

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

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

  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. Binary String Matching

    问题 B: Binary String Matching 时间限制: 3 Sec  内存限制: 128 MB提交: 4  解决: 2[提交][状态][讨论版] 题目描述 Given two strin ...

随机推荐

  1. netty基础

    1,ServerBootstrap  [Bootstrap] 

  2. tcpdump使用方法

    TcpDump可以将网络中传送的数据包完全截获下来提供分析.它支持针对网络层.协议.主机.网络或端口的过滤,并提供and.or.not等逻辑语句来帮助你去掉无用的信息. 工作中使用tcpdump命令抓 ...

  3. 3、Ansible playbooks(Hosts、Users、tasks、handlers、变量、条件测试(when、迭代)、templates)

    Ansible playbooks playbook是由一个或多个“play”组成的列表.play的主要功能在于将事先归并为一组的主机装扮成事先通过ansible中的task定义好的角色.从根本上来讲 ...

  4. 51nod 1351 吃点心(贪心)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1351 题意: 思路: 要么先选low值大的,要么先选high值大的,分两 ...

  5. git pull 提示 There is no tracking information for the current branch

    在执行git pull的时候,提示当前branch没有跟踪信息: git pull There is no tracking information for the current branch. P ...

  6. git创建、删除分支

    //创建分支 git branch branchname //创建并切换到新分支 git checkout -b branchname //远程分支 git push origin branchnam ...

  7. Tp5.1使用导出Excel

    composer require phpoffice/phpexcel 不管它的警告,都能用的. use PHPExcel; use PHPExcel_IOFactory; public static ...

  8. react面试问题总结

    1. 在生命周期中的哪一步你应该发起 AJAX 请求? 我们应当将AJAX 请求放到 componentDidMount 函数中执行,主要原因有下: 放到componentWillMount不好.  ...

  9. Spark Streaming笔记

    Spark Streaming学习笔记 liunx系统的习惯创建hadoop用户在hadoop根目录(/home/hadoop)上创建如下目录app 存放所有软件的安装目录 app/tmp 存放临时文 ...

  10. DAY3 数据类型与运算符

    一.注释 代码注释分单行和多行注释, 单行注释用#,多行注释可以用三对双引号""" """ 注释用于解释某一行代码的作用,增加代码的可读性 ...