题意:给你一个长度为n的01串,和一个数组a,你可以每次选择消除一段数字相同的01串,假设消除的长度为len,那么收益为a[len],问最大的收益是多少?

思路:前两天刚做了POJ 1390,和此题很相似:POJ 1390 。我们甚至可以直接套用这个题的状态转移方程。仍然先把01串预处理一下,把相邻的并且数字相同的位合并成一个块。这样,01串就变成了若干个相邻的01块了。

设dp[i][j][k]为处理第i个块到第j个块,并且后面有k个位和第j个块颜色相同,设f[i]为消除长度为i的串的最大收益,那么状态转移方程可以这样写:

1:先把后面相同的合并了:dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][0] + f[第j个块的位的个数 + k]。

2:与前面的颜色相同的块合并(假设块j与块p颜色相同):dp[i][j][k] = max(dp[i][j][k], dp[i][p][k + 第j个块的位的个数] + dp[p + 1][j - 1][0]);

现在唯一的问题f数组怎么求?很明显此题并不是一次消除的长度越长越好,多次消除短的串可能获得更高的收益。我们可以思考一下,f[i]肯定是把长度i拆成1份,2份,......i份中收益最大的情况。

设re[i][j]为把长度j拆成i份获得的最大收益,我们可以暴力枚举第i份的长度是多少(假设是k),然后继续递归去求得把j - k拆成i - 1份的最优值来更新当前状态。那么f[i]就是re[1][i],re[2][i].....re[i][i]中的最优情况。

dp和re数组我们都可以记忆化,可以大幅度提高效率,31ms水过。。。

代码:

#include <bits/stdc++.h>
#define LL long long
using namespace std;
LL dp[110][110][110];
bool v[110][110][110], v1[110][110];
LL f[110], b[110];
LL re[110][110]; struct node{
int flag, cnt;
};
node a[110]; LL get_f(int tot, int n) {
if(v1[tot][n]) return re[tot][n];
if(tot == 1) {
v1[tot][n] = 1;
return re[tot][n] = f[n];
}
else {
LL ans = 0;
for (int i = 1; i <= n - tot + 1; i++) {
ans = max(ans, f[i] + get_f(tot - 1, n - i));
}
v1[tot][n] = 1;
return re[tot][n] = ans;
}
} int cal(int n) {
LL ans = 0;
f[n] = b[n];
for (int i = 1; i <= n; i++) {
ans = max(ans, get_f(i, n));
}
f[n] = ans;
} LL solve(int l, int r, int x) {
if(v[l][r][x]) return dp[l][r][x];
if(l >= r) return f[a[r].cnt + x];
LL ans = solve(l, r - 1, 0) + f[a[r].cnt + x];
for (int i = l; i < r; i++) {
if(a[i].flag == a[r].flag) {
ans = max(ans, solve(l, i, a[r].cnt + x) + solve(i + 1, r - 1, 0));
}
}
v[l][r][x] = 1;
return dp[l][r][x] = ans;
} int main() {
int n;
char s[210];
scanf("%d", &n);
scanf("%s", s + 1);
int now_flag = -1, now = 0, tot = 0;
for (int i = 1; i <= n; i++) {
if(s[i] - '0' != now_flag) {
a[++tot].flag = s[i] -'0';
a[tot].cnt = 1;
now_flag = s[i] - '0';
} else {
a[tot].cnt++;
}
}
for (int i = 1; i <= n; i++) {
scanf("%lld", &b[i]);
}
for (int i = 1; i <= n; i++)
cal(i);
printf("%lld\n", solve(1, tot, 0));
}

  

Codeforces 1107E (Vasya and Binary String) (记忆化,DP + DP)的更多相关文章

  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. HUD 1501 Zipper(记忆化 or DP)

    Problem Description Given three strings, you are to determine whether the third string can be formed ...

  5. HDU1978How Many Ways 记忆化dfs+dp

    /*记忆化dfs+dp dp[i][j]代表达到这个点的所有路的条数,那么所有到达终点的路的总数就是这dp[1][1]加上所有他所能到达的点的 所有路的总数 */ #include<stdio. ...

  6. 记忆化搜索(DP+DFS) URAL 1183 Brackets Sequence

    题目传送门 /* 记忆化搜索(DP+DFS):dp[i][j] 表示第i到第j个字符,最少要加多少个括号 dp[x][x] = 1 一定要加一个括号:dp[x][y] = 0, x > y; 当 ...

  7. Vasya and Binary String(来自codeforces

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

  8. codeforces 284 D. Cow Program(记忆化搜索)

    题目链接:http://codeforces.com/contest/284/problem/D 题意:给出n个数,奇数次操作x,y都加上a[x],偶数次操作y加上a[x],x减去a[x],走出了范围 ...

  9. Codeforces 294B Shaass and Bookshelf(记忆化搜索)

    题目 记忆化搜索(深搜+记录状态) 感谢JLGG //记忆话搜索 //一本书2中状态,竖着放或者横着放 //初始先都竖着放,然后从左边往右边扫 #include<stdio.h> #inc ...

随机推荐

  1. ural 2015 Zhenya moves from the dormitory(模拟)

    2015. Zhenya moves from the dormitory Time limit: 1.0 secondMemory limit: 64 MB After moving from hi ...

  2. Collection集合存储自定义对象练习

    public class Student { private String name; private int age; public Student() { super(); } public St ...

  3. L119

    A big collaboration is trying to understand diseases of the psycheDiseases of the psyche have always ...

  4. mysql 开发标准规范

    一.表设计 1. 库名.表名.字段名使用小写字母,“_”分割. 2. 库名.表名.字段名不超过12个字符. 3. 库名.表名.字段名见名知意,尽量使用名词而不是动词. 4. 优先使用InnoDB存储引 ...

  5. 树莓派(Arduino)仿真软件 —— Fritzing

    Fritzing 官网:Fritzing Fritzing 下载地址:Fritzing Download windows 下降 zip 文件解压后,免安装双击 exe 即可运行:

  6. hadoop-pig学习笔记

    A1 = LOAD '/luo/lzttxt01.txt' AS (col1:chararray,col2:int,col3:int,col4:int,col5:double,col6:double) ...

  7. 记一次内存溢出的分析经历——使用thrift

    背景: 有一个项目做一个系统,分客户端和服务端,客户端用c++写的,用来收集信息然后传给服务端(客户端的数量还是比较多的,正常的有几千个), 服务端用Java写的(带管理页面),属于RPC模式,中间的 ...

  8. 日志收集系统搭建-BELK

    前言 日志是我们分析系统运行情况.问题定位.优化分析等主要数据源头.目前,主流的业务系统都采用了分布式.微服务的形式.如果想要查看日志,就需要从不同的节点上去查看,而且对于整个业务链路也非常不清晰.因 ...

  9. Azure disk iops的测试

    在Public Cloud中,VM.Storage和Network是IaaS的三大基础.本文将介绍在Azure的VM上测试磁盘IOPS的工具和方法. 一.添加磁盘.初始化磁盘 1.添加磁盘 把相应的信 ...

  10. BMP格式转JPEG格式

    int Bmp2Jpg(const char *bmp_data, const char *jeg_file, const int width, const int height) { int ret ...