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. JS中Float类型加减乘除 修复 JQ 操作 radio、checkbox 、select LINQ to SQL:Where、Select/Distinct LINQ to SQL Count/Sum/Min/Max/Avg Join

    JS中Float类型加减乘除 修复   MXS&Vincene  ─╄OvЁ  &0000027─╄OvЁ  MXS&Vincene MXS&Vincene  ─╄Ov ...

  2. On ROWNUM and Limiting Results

    This issue's Ask Tom column is a little different from the typical column. I receive many questions ...

  3. 代写GIS系统

    代写GIS系统,熟悉arcgis,leaflet ,百度地图等api.可以提供系统代写,技术咨询等

  4. 脱了裤子放屁之std::string

    一个天天跟c#奋斗的苦逼c++程序猿 改自己曾经代码的时候发现有例如以下几行. char szPath[MAX_PATH] = {0}; GetModuleFileNameA(NULL,szPath, ...

  5. Mixtures of Gaussians and the EM algorithm

    http://cs229.stanford.edu/ http://cs229.stanford.edu/notes/cs229-notes7b.pdf

  6. Rethinking the Inception Architecture for Computer Vision

    https://arxiv.org/abs/1512.00567 Convolutional networks are at the core of most state-of-the-art com ...

  7. 基于地理位置信息的traceroute

    我们在机房选择.測试网络的质量的时候,往往仅仅依据跳数.延迟.抖动.网络吞吐量等指标来衡量,非常多时候跳数并不能全然显示网络拓扑优劣,于是写了个traceroute结合whois的小脚本来直观显示每一 ...

  8. myeclipse10集成Tomcat6时出现错误

    myeclipse配置Tomcat时出现错误:如图 tomcat6目录:如图 在搜集各种资料后,最终得出结论: 在Tomcat目录中新建temp文件夹,问题解决. 亲测好使.

  9. SpringBoot-(4)-Filter的使用

    一,Filter Filter功能,它使用户可以改变一个 request和修改一个response. Filter 不是一个servlet,它不能产生一个response,它能够在一个request到 ...

  10. php date之间的相互转换

    字符串转成date $str =date("Y-m-d H:i:s",strtotime("2011-12-12 14:23:01")); echo $str; ...