题目链接

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

我们设dpi,j,k为区间[i,j],j后面有k个与j相同的元素,若j与前面的i-j-1一起消,状态转移为:dpi,j,k=dpi,m,k+siz[j]+dpm+1,j-1,0,(m与j的字符相同)意思是合并i到m,m+1到j-1合并后,将i到m与j再合并,如果j单独消,dpi,j,k=dpi,j-1,0+f[siz[j]+k]

#include<bits/stdc++.h>
using namespace std;
#define lowbit(x) ((x)&(-x))
typedef long long LL;
typedef pair<int,int> pii; const int maxn = 105; char s[maxn];
LL dp[maxn][maxn][maxn], f[maxn], cost[maxn];
int n, siz[maxn], bit[maxn], cnt; void prework() {
for(int i = 1; i <= n; ++i)
for(int j = i; j <= n; ++j)
f[j] = max(f[j], f[j-i] + cost[i]);
cnt = 0;
int now = 0;
for(int i = 1; i <= n; ++i) {
if(i == 1 || s[i] == s[i-1]) ++now;
else {
siz[++cnt] = now;
bit[cnt] = s[i-1] - '0';
now = 1;
}
}
siz[++cnt] = now, bit[cnt] = s[n] - '0';
} LL DP(int l, int r, int k) {
if(l == r) return dp[l][r][k] = f[siz[r]+k];
if(dp[l][r][k] != -1) return dp[l][r][k];
LL t = DP(l, r-1, 0) + f[siz[r] + k];
for(int i = l; i < r-1; ++i)
if(bit[i] == bit[r])
t = max(t, DP(l, i, k+siz[r])+DP(i+1, r-1, 0));
return dp[l][r][k] = t;
} void run_case() {
cin >> n;
cin >> (s+1);
for(int i = 1; i <= n; ++i) cin >> cost[i];
prework();
memset(dp, -1, sizeof(dp));
cout << DP(1, n, 0);
} int main() {
ios::sync_with_stdio(false), cin.tie(0);
cout.flags(ios::fixed);cout.precision(10);
//int t; cin >> t;
run_case();
cout.flush();
return 0;
}

转自

https://blog.csdn.net/litble/article/details/87290658

Codeforces1107E. Vasya and Binary String的更多相关文章

  1. Codeforces1107E Vasya and Binary String 记忆化dp

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

  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. [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. CF1107E Vasya and Binary String

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

  6. Vasya and Binary String(来自codeforces

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

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

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

  8. CF - 1107 E Vasya and Binary String DP

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

  9. Binary String Matching

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

随机推荐

  1. arcgis中的Join(合并连接)和Relate(关联连接)

    arcgis中的Join(合并连接)和Relate(关联连接) 一.区别 1.连接关系不一样. Relate(关联连接)方式连接的两个表之间的记录可以是“一对一”.“多对一”.“一对多”的关系 Joi ...

  2. 专题-集合-HashMap

    集合中的HashMap几乎是面试时必问的知识点,下面就从原理上剖析以下这个集合,看完了这一块的知识点应该就没问题了. 一.HashMap概述 HashMap基于哈希表的 Map 接口的实现.此实现提供 ...

  3. PHP正则表达式常用例子

    "^[0-9]*[1-9][0-9]*$" //正整数"^((-\d+)|(0+))$" //非正整数(负整数 + 0)"^-[0-9]*[1-9][ ...

  4. innerHTML,innerText,textContent

    参考理解 https://www.e-learn.cn/content/html/1765240 https://developer.mozilla.org/zh-CN/docs/Web/API/El ...

  5. ALSA driver --PCM 实例创建过程

    前面已经写过PCM 实例的创建框架,我们现在来看看PCM 实例是如何创建的. 在调用snd_pcm_new时就会创建一个snd_pcm类型的PCM 实例. struct snd_pcm { struc ...

  6. 如何在Linux中显示和设置主机名(适用ubantu、centos等版本)

    随着连接到网络的计算机数量越来越多,每一台计算机都需要有一个属性来区别于其它计算机.和现实世界中的人一样,计算机也有一个叫做hostname(主机名)的属性. 什么是hostname 从它的操作手册来 ...

  7. socket udp编程的一些积累的记录

    接了个小活,要求写udp的客户端,循环接收服务端的固定的指令并显示数据 我设计的逻辑是,用户在界面输入框输入服务器ip.端口,随后udp连接,开启线程循环接收,接收指令,解析成数据,存在结构体的lis ...

  8. c# /MVC设置类的自定义特性

    public class MarkStaticAttribute:Attribute { public MarkStaticAttribute(bool mark=true) { _IsMark = ...

  9. django+vue 基础框架 :vue

    <template> <div> <p>用户名:<input type="text" v-model="name"&g ...

  10. mysql int类型字段插入空字符串时自动转为0

    mysql int类型字段插入空字符串时自动转为0 如果不想转的话可以修改配置文件 修改 my.ini 文件. # Set the SQL mode to strictsql-mode=”STRICT ...