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的更多相关文章

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

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

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

  3. Codeforces1107E. Vasya and Binary String

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

  4. Codeforces 1107 E - Vasya and Binary String

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

  5. CF 1107 E. Vasya and Binary String

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

  6. UVA - 11324 The Largest Clique 强连通缩点+记忆化dp

    题目要求一个最大的弱联通图. 首先对于原图进行强连通缩点,得到新图,这个新图呈链状,类似树结构. 对新图进行记忆化dp,求一条权值最长的链,每一个点的权值就是当前强连通分量点的个数. /* Tarja ...

  7. cf835(预处理 + 记忆化dp)

    题目链接: http://codeforces.com/contest/835/problem/D 题意: 定义 k 度回文串为左半部分和右半部分为 k - 1 度的回文串 . 给出一个字符串 s, ...

  8. cf779D(记忆化dp)

    题目链接: http://codeforces.com/problemset/problem/799/D 题意: 给出两个矩阵边长 a, b, 和 w, h, 以及一个 c 数组, 可选择 c 数组中 ...

  9. 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. ...

随机推荐

  1. python(11)- 文件处理

    文件操作 1.1 对文件操作流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 现有文件如下: 昨夜寒蛩不住鸣. 惊回千里梦,已三更. 起来独自绕阶行. 人悄悄,帘外月胧明 ...

  2. Ffmpeg 获取USB Camera 视频流

    本文讲述的案例是如何通过Ffmpeg实现从USB Camera中获取视频流并将视频流保存到MP4文件. 本文亦适用于从USB Camera 获取视频流并将视频流转发到rtmp服务的案例,二者基本的原理 ...

  3. 笔记04 WPF对象引用

    转自:http://www.fx114.net/qa-261-90254.aspx 我们应该都知道,XAML是一种声明式语言,XAML的标签声明的就是对象.一个XAML标签会对应着一个对象,这个对象一 ...

  4. Mysql 基本操作指令+增删查改

    nqinx是web前端服务端 负载均衡(软件)可以将用户请求调度到几台机器的nqinx上去做 ,一般都有两个负载均衡,一个做备用硬件的要比软件的好,但是一般公司都用软件实现数据库软件其实也是一个服务端 ...

  5. 用callgraph生成的两张函数调用关系图

    参考这里,感觉很Cool吧. Linux-0.11函数调用关系图: QEMU函数调用关系图:

  6. caffe搭建--缺少 skimage-缺少 google.protobuf.internal.-caffe搭建--ipython--ubuntu16.04+ caffe+ ipython

    mkdir build && cd build cmake .. make pycaffe -j4 sudo vim /etc/profile---- export PYTHONPAT ...

  7. 解读OC中的load和initialize

    在 Objective-C 中,NSObject 是绝大多数类的基类.而在 NSObject 中有两个类方法 load 和 initialize,那这两个方法是在什么时机被调用呢?父类.Categor ...

  8. 搭建React Native开发环境

    搭建React Native开发环境 本文档是Mac下搭建的环境,针对的目标平台不同,以及开发 iOS 和 Android 的不同,环境搭建也有差异. Github地址:https://github. ...

  9. Spring整合Struts2的方法

    一.基本支持 通常我们整合Spring和struts2的目的是让Spring来管理struts2的控制器.也就是说把Action交由Spring来管理,利用IOC的特性把Action注入到业务逻辑中. ...

  10. codeforces776D

    传送门 这题的意思就是原本有一个长度为n的01串,再给出m的长度为n的01串,要求你判定是否可以通过原串与m个串中的某些串xor使得原串到达一个状态.n,m小于1e5. 这题最初我发现不可做,因为这貌 ...