题目链接

本题也是区间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. 使用SqlDataReader的查询操作

    原创weixin_42430576 发布于2019-01-31 18:49:41 阅读数 762  收藏 展开 using System; using System.Collections.Gener ...

  2. python 实现 md文档自动编号

    目录 1. 原理 2. 运行方法 3. 效果 4. 代码 1. 原理 正则匹配对相应字符串进行替换 2. 运行方法 python md_convert.py [a.md, b.md,...] # 转换 ...

  3. Django流程-以登录功能为例

    Django流程-以登录功能为例 一.注意点 1.新创建的app一定要先去settings.py注册 简写:'app01' 完整:'app01.apps.App01Config' 2.启动Django ...

  4. python中字符串内置方法

    字符串类型 作用:定义姓名.性别等 定义方式: s='lzs' #\n换行 \t缩进4个空格 \r回退上一个打印结果,覆盖上一个打印结果 加上一个\让后面的\变得无意义 内置方法: (优先掌握) 1. ...

  5. 6485. 【GDOI2020模拟02.25】沙塔斯月光

    题目描述 题解 镇♂男则反 如果没有操作3很好办,反着做维护操作1的次数即可 实际上一次操作3的贡献是zi*(∑i到操作1位置的距离) 维护一下即可O(n^4) code #include <b ...

  6. Go_defer

    package main import "fmt" func main() { //外围函数 /* defer的词义:"延迟","推迟" 在 ...

  7. 基于selenium+Python3.7+yaml+Robot Framework的UI自动化测试框架

    前端自动化测试框架 项目说明 本框架是一套基于selenium+Python3.7+yaml+Robot Framework而设计的数据驱动UI自动化测试框架,Robot Framework 作为执行 ...

  8. 那些年做过的ctf之加密篇(加强版)

    MarkdownPad Document *:first-child { margin-top: 0 !important; } body>*:last-child { margin-botto ...

  9. LR编写webservice协议接口

    转自:http://lovesoo.org/use-loadrunner-call-webservice-interface-testing-optimization-summary.html 本文主 ...

  10. js加密(七)steam登录

    1. url: https://store.steampowered.com/login/?redir=&redir_ssl=1 2. target: 登录 3. 分析 3.1 老样子,抓包, ...