CF - 1107 E Vasya and Binary String DP
题解:
dp[ l ][ r ][ k ] 代表的是[l, r]这段区间内, 前面有k-1个连续的和s[l]相同且连续的字符传进来的最大值。
solve( l, r, k) 代表的是处理 区间[L, R], 正在处理 [L, R]这个区间, 前面有k-1个连续的和s[l]相同且连续的字符。
转移状态:
dp[l][r][k] = a[k] + solve(l+1,r,1)。 在 l 这个位置切断连续字符。
dp[l][r][k] = solve( l+1, i-1, 1) + solve(i, r, k+1) 其中 s[ l ] == s[ i ] 加入新的连续字符。
代码:
/*
code by: zstu wxk
time: 2019/01/31
*/
#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL _INF = 0xc0c0c0c0c0c0c0c0;
const LL mod = (int)1e9+;
const int N = ;
int Wa(){return rand()%;}
void Hack(int n){srand(time());int hack = ;for(int j = ; j <= n; ++j)hack += Wa();if(hack == n)puts("OH No!");}
int n;
char s[N];
int a[N];
int pre[N];
LL dp[N][N][N];
LL solve(int l, int r, int k){
if(l > r) return ;
if(l == r) return a[k];
LL & ret = dp[l][r][k];
if(ret) return ret;
ret = a[k] + solve(l+,r,);
for(int i = l+; i <= r; ++i){
if(s[l] == s[i]){
ret = max(ret, solve(i,r,k+) + solve(l+,i-,));
}
}
return ret;
}
void Ac(){
scanf("%s", s+);
for(int i = ; i <= n; ++i)
scanf("%d", &a[i]);
printf("%I64d\n", solve(,n,));
}
int main(){
while(~scanf("%d", &n)){
Ac();
}
return ;
}
CF - 1107 E Vasya and Binary String DP的更多相关文章
- CF 1107 E. Vasya and Binary String
E. Vasya and Binary String 链接 分析: 对于长度为x的一段序列,我们可以dp出消除的过程的最优方案,背包即可. 然后区间dp,可以先合并完所有的点,即没相同的一段区间合并为 ...
- Codeforces 1107 E - Vasya and Binary String
E - Vasya and Binary String 思路:区间dp + 记忆化搜索 转移方程看上一篇博客. 代码: #pragma GCC optimize(2) #pragma GCC opti ...
- Codeforces1107E Vasya and Binary String 记忆化dp
Codeforces1107E 记忆化dp E. Vasya and Binary String Description: Vasya has a string \(s\) of length \(n ...
- [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 ...
- Codeforces 1107E (Vasya and Binary String) (记忆化,DP + DP)
题意:给你一个长度为n的01串,和一个数组a,你可以每次选择消除一段数字相同的01串,假设消除的长度为len,那么收益为a[len],问最大的收益是多少? 思路:前两天刚做了POJ 1390,和此题很 ...
- CF1107E Vasya and Binary String
比赛的时候又被垃圾题艹翻了啊. 这个题显然是区间dp 考虑怎么转移. 类似消除方块和ZYB玩字符串那样的一个DP. 可以从左到右依次考虑消除. dp[l][r][k][flag]表示区间l,r左边粘着 ...
- Vasya and Binary String(来自codeforces
题目大意: 给定一个0/1字符串,每次你可以将此字符串中一段连续的任意长度的0/1子串消除掉,注意每次消除的子串中只能有0或者1一种字符,消除掉一串长度为i的0/1字符串会得到a[i]的收益,问将这个 ...
- Codeforces1107E. Vasya and Binary String
题目链接 本题也是区间dp,但是需要保存的信息很多,是1还是0,有多少个连续的,那我们可以预处理,将所有的连续缩合成1个字符,那么字符串就变成了一个01交替的串,我们任意的消除1个部分,一定能引起连锁 ...
- CF 1003B Binary String Constructing 【构造/找规律/分类讨论】
You are given three integers a, b and x. Your task is to construct a binary string s of length n=a+b ...
随机推荐
- python 实现两个文本文件内容去重
实现两个文本内容去重,输出两个文本不重复的结果 两个测试文本内容如下 1.txt中内容为 1 2 3 4 5 6 7 8 2.txt中内容为 1 2 3 4 5 6 7 8 9 10 11 12 13 ...
- C# Winfrom 自定义控件——带图片的TextBox
效果: 描述: 本来是想用GDI在左边画图片上去的,文本是居中对齐,如果文本是左对齐,文本会把图片遮住控件长这样: 但这样做,输入框在获取焦点时候,会把图片挡住就像这样: 输入完成之后图片就会显示完整 ...
- Spring浅入浅出——不吹牛逼不装逼
Spring浅入浅出——不吹牛逼不装逼 前言: 今天决定要开始总结框架了,虽然以前总结过两篇,但是思维是变化的,而且也没有什么规定说总结过的东西就不能再总结了,是吧.这次总结我命名为浅入浅出,主要在于 ...
- c#将字符串转化为合理的文件名
string name = System.Text.RegularExpressions.Regex.Replace(url, "[<>/\\|:\"?*]" ...
- Draw.io
如何给类图增加一个字段? 选中一个字段,然后按 Ctrl +Enter 即可. 参考:Add row to class diagram - stackoverflow
- [转载]关于ActiveMQ集群
转载于 http://blog.csdn.net/nimmy/article/details/6247289 近日因工作关系,在研究JMS,使用ActiveMQ作为提供者,考虑到消息的重要,拟采用Ac ...
- Markdown转载
@TOC 欢迎使用Markdown编辑器 你好! 这是你第一次使用 Markdown编辑器 所展示的欢迎页.如果你想学习如何使用Markdown编辑器, 可以仔细阅读这篇文章,了解一下Markdown ...
- Duilib的圆角矩形 抗锯齿优化 弥补RoundRect不足(网易云信borderround版本)
VListBox class="list" name="list" padding="5,3,5,3" bordersize="1 ...
- Javasrcipt中从一个url或者从一个字符串中获取参数值得方法
从url中获取参数值是che程序开发过程中的常用需求,偶然得闲,便抽空研究了一下javasrcipt下,获取参数的办法(JAVA中也类似). 首先看url的规范: URL组成:protocol :// ...
- npm install 安装很慢
npm install 安装很慢 设置国内镜像 npm config set registry https://registry.npm.taobao.org npm install