Codeforces1107E Vasya and Binary String 记忆化dp
Codeforces1107E 记忆化dp
E. Vasya and Binary String
Description:
Vasya has a string \(s\) of length \(n\) consisting only of digits 0 and 1. Also he has an array \(a\) of length \(n\).
Vasya performs the following operation until the string becomes empty: choose some consecutive substring of equal characters, erase it from the string and glue together the remaining parts (any of them can be empty). For example, if he erases substring 111 from string 111110 he will get the string 110. Vasya gets \(a_x\) points for erasing substring of length \(x\).
Vasya wants to maximize his total points, so help him with this!
Input:
The first line contains one integer \(n\) (\(1 \le n \le 100\)) — the length of string \(s\).
The second line contains string \(s\), consisting only of digits 0 and 1.
The third line contains \(n\) integers \(a_1, a_2, \dots a_n\) (\(1 \le a_i \le 10^9\)), where \(a_i\) is the number of points for erasing the substring of length \(i\).
Output
Print one integer — the maximum total points Vasya can get.
Sample Input:
7
1101001
3 4 9 100 1 2 3
Sample Output:
109
Sample Input:
5
10101
3 10 15 15 15
Sample Output:
23
题目链接
题解:
你有一个长为n的01串,每次可以消去长度为\(len\)的连续相同字符,收益为\(a_{len}\),求消去整个串的收益最大值
记忆化dp
设\(dp[0,1][l][r][cnt]\)代表把\(l\)到\(r\)删除到只剩下\(cnt\)个0或1的最大收益,\(ans[l][r]\)代表把\(l\)到\(r\)删完的最大收益
转移方程为\(ans[l][r] = \max_{cnt = 1}^{r-l+1}(a[cnt] + dp[0,1][l][r][cnt])\),\(dp[c][l][r][cnt] = \max_{s[i] = c, i = l}^{r - 1}(ans[l][i-1] + dp[c][i+1][r][cnt-1])\),cnt=1时特判一下
状态数为\(O(n^3)\),转移为\(O(n)\),总复杂度为\(O(n^4)\)
AC代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 102;
long long dp[2][N][N][N], ans[N][N];
int n, a[N];
char s[N];
long long calcdp(int c, int l, int r, int cnt);
long long calcans(int l, int r) {
if(l > r) return 0;
long long &res = ans[l][r];
if(res != -1) return res;
res = 0;
for(int cnt = 1; cnt <= r - l + 1; ++cnt) {
res = max(res, a[cnt] + calcdp(0, l, r, cnt));
res = max(res, a[cnt] + calcdp(1, l, r, cnt));
}
return res;
}
long long calcdp(int c, int l, int r, int cnt) {
if(cnt == 0) return dp[c][l][r][cnt] = calcans(l, r);
long long &res = dp[c][l][r][cnt];
if(res != -1) return res;
res = -1e10;
for(int i = l; i < r; ++i) {
if(s[i] - '0' == c)
res = max(res, calcans(l, i - 1) + calcdp(c, i + 1, r, cnt - 1));
}
if(cnt == 1 && s[r] - '0' == c)
res = max(res, calcans(l, r - 1));
return res;
}
int main() {
scanf("%d", &n);
scanf("%s", s + 1);
for(int i = 1; i <= n; ++i)
scanf("%d", &a[i]);
memset(dp, -1, sizeof(dp));
memset(ans, -1, sizeof(ans));
int t, a, b, c, d;
printf("%lld\n", calcans(1, n));
return 0;
}
Codeforces1107E Vasya and Binary String 记忆化dp的更多相关文章
- Codeforces 1107E (Vasya and Binary String) (记忆化,DP + DP)
题意:给你一个长度为n的01串,和一个数组a,你可以每次选择消除一段数字相同的01串,假设消除的长度为len,那么收益为a[len],问最大的收益是多少? 思路:前两天刚做了POJ 1390,和此题很 ...
- [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 ...
- Codeforces1107E. Vasya and Binary String
题目链接 本题也是区间dp,但是需要保存的信息很多,是1还是0,有多少个连续的,那我们可以预处理,将所有的连续缩合成1个字符,那么字符串就变成了一个01交替的串,我们任意的消除1个部分,一定能引起连锁 ...
- Codeforces 1107 E - Vasya and Binary String
E - Vasya and Binary String 思路:区间dp + 记忆化搜索 转移方程看上一篇博客. 代码: #pragma GCC optimize(2) #pragma GCC opti ...
- CF 1107 E. Vasya and Binary String
E. Vasya and Binary String 链接 分析: 对于长度为x的一段序列,我们可以dp出消除的过程的最优方案,背包即可. 然后区间dp,可以先合并完所有的点,即没相同的一段区间合并为 ...
- UVA - 11324 The Largest Clique 强连通缩点+记忆化dp
题目要求一个最大的弱联通图. 首先对于原图进行强连通缩点,得到新图,这个新图呈链状,类似树结构. 对新图进行记忆化dp,求一条权值最长的链,每一个点的权值就是当前强连通分量点的个数. /* Tarja ...
- cf835(预处理 + 记忆化dp)
题目链接: http://codeforces.com/contest/835/problem/D 题意: 定义 k 度回文串为左半部分和右半部分为 k - 1 度的回文串 . 给出一个字符串 s, ...
- cf779D(记忆化dp)
题目链接: http://codeforces.com/problemset/problem/799/D 题意: 给出两个矩阵边长 a, b, 和 w, h, 以及一个 c 数组, 可选择 c 数组中 ...
- Google Code Jam 2009, Round 1C C. Bribe the Prisoners (记忆化dp)
Problem In a kingdom there are prison cells (numbered 1 to P) built to form a straight line segment. ...
随机推荐
- Apache/Nigix + Tomcat + 负载均衡
Part I: Apache + Tomcat + 负载均衡 http://www.open-open.com/lib/view/open1350612892352.html http://micha ...
- FFmpeg编码详细流程
FFmpeg在编码一个视频的时候的函数调用流程.为了保证结构清晰,其中仅列出了最关键的函数,剔除了其它不是特别重要的函数. 函数背景色 函数在图中以方框的形式表现出来.不同的背景色标志了该函数不同的作 ...
- 多媒体开发之--- Live555 server 获取不到本地ip 全为0
今天把wis-streamer live555 移植到8148上面跑起来了,运行testOnDemandRTSPServer的时候发现,本地IP地址居然为0.0.0.0; 于是乎就跟踪调试了下,看看它 ...
- WPF 后台Render线程崩溃, Exception from HRESULT: 0x88980406
近期遇到一个问题.窗口在Loaded同一时候Resize会出现黑屏或者直接崩溃, 调查发现是WPF后端的Render线程渲染UI到DirectX时崩溃. 硬件环境:WES7 + .Net4.0 关于W ...
- 获取本地IP V4 出现::1
获取本地IP V4 竟然得到 ::1 和 192.168.x.xxx 多出来一个::1???? 终于在网络找到答案,原来是禁用了IP V6 导致,重新勾选IP V6,或者卸载IP V6 都可以解决问 ...
- STM32 ~ 查看系统时钟
调用库函数RCC_GetClocksFreq,该函数可以返回片上的各种时钟的频率 函数原形 void RCC_GetClocksFreq(RCC_ClocksTypeDef* RCC_Clocks) ...
- (转)windows一台电脑添加多个git账号
版权声明:技术总结来自互联网,书籍,以及工作积累.如果觉得我的总结有帮助,欢迎分享. https://blog.csdn.net/qq1332479771/article/details/701496 ...
- 解密阿里云Redis助力双十一背后的技术
摘要: Redis是一个使用范围很广的NOSQL数据库,阿里云Redis同时在公有云和阿里集团内部进行服务,本文介绍了阿里云Redis双11的一些业务场景:微淘社区之亿级关系链存储.天猫直播之评论商品 ...
- (C)struct结构体指针
结构体指针 指针结构与指针的关系亦有两重:其一是在定义结构时,将指针作为结构中的一个成员:其二是指向结构的指针(称为结构指针). 前者同一般的结构成员一样可直接进行访问,后者是本节讨论的重点. 结构指 ...
- Matlab小技巧之怎么复制汉字
在我们复制Matlab到Word的过程中,经常会出现乱码的情况.这时候可以这么做. 1.复制Matlab代码. 2.新建一个txt文件,将代码粘贴到txt文件中. 3.复制txt文件中的代码到Word ...