题目描述

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 ax points for erasing substring of length x.
Vasya wants to maximize his total points, so help him with this!
{题目从CF盗取)

题目大意

给你一个长度为n(1<=n<=100)的01串s,从s中删除一段连续的长度为i的所有字符都相同的子串将会得到ai的分数,如果执行上述操作直到s被删除完,求最大分数和。
(题意从lg盗取)

题解

一道简单的区间DP,也不清楚为什么在洛谷上是一道黑题,可能是我太菜了。(废话不多说)
\(f[i][j][k]\)表示消除了\([j,k]\)后,在\(j\)之前有\(i\)个字符和\(j\)相同的消除的最大值。
考虑初始值,一开始\(f[i][L][R]=a[i+1]+f[0][L][R]\)。\(L,R\)是当前的区间,(从0开始标号)
转移:\(f[k][L][R]=max(f[k][L][R],f[0][L+1][l-1]+f[k+1][l][R])\)(L和R是当前的区间)

ac代码

# include <bits/stdc++.h>
# define LL long long
using namespace std;
inline int gi(){
    int w=0,x=0;char ch=0;
    while(!isdigit(ch)) w|=ch=='-',ch=getchar();
    while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    return w?-x:x;
}
# define N 105
LL f[N][N][N];
int a[N],n,m;
char s[N];
int main(){
    n=gi();
    scanf("%s",s);
    for (register int i=1;i<=n;i++) a[i]=gi();
    for (register int i=0;i<n;i++)
        for (register int j=0;j+i<n;j++)
            for (register int k=0;k<n;k++){
                f[k][j][j+i]=a[k+1]+f[0][j+1][j+i];
                for (register int l=j+1;l<=j+i;l++)
                    if (s[j]==s[l])
                        f[k][j][j+i]=max(f[k][j][j+i],f[0][j+1][l-1]+f[k+1][l][j+i]);
            }
    printf("%lld\n",f[0][0][n-1]);
    return 0;
}

[CF1107E]Vasya and Binary String【区间DP】的更多相关文章

  1. CF1107E Vasya and Binary String

    比赛的时候又被垃圾题艹翻了啊. 这个题显然是区间dp 考虑怎么转移. 类似消除方块和ZYB玩字符串那样的一个DP. 可以从左到右依次考虑消除. dp[l][r][k][flag]表示区间l,r左边粘着 ...

  2. Codeforces1107E Vasya and Binary String 记忆化dp

    Codeforces1107E 记忆化dp E. Vasya and Binary String Description: Vasya has a string \(s\) of length \(n ...

  3. Codeforces 1107 E - Vasya and Binary String

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

  4. CF 1107 E. Vasya and Binary String

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

  5. Codeforces Round #354 (Div. 2)-C. Vasya and String,区间dp问题,好几次cf都有这种题,看来的好好学学;

    C. Vasya and String time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. Vasya and Binary String(来自codeforces

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

  7. Codeforces1107E. Vasya and Binary String

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

  8. CF - 1107 E Vasya and Binary String DP

    题目传送门 题解: dp[ l ][ r ][ k ] 代表的是[l, r]这段区间内, 前面有k-1个连续的和s[l]相同且连续的字符传进来的最大值. solve( l, r, k) 代表的是处理 ...

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

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

随机推荐

  1. Java面试题详解二:java中的关键字

    一,final1.被final修饰的类不可以被继承2.被final修饰的方法不可以被重写3.被final修饰的变量不可以被改变  重点就是第三句.被final修饰的变量不可以被改变,什么不可以被改变呢 ...

  2. JEECG & JEESite Tomcat集群 Session共享

    多台tomcat服务的session共享 memcached与redis - JEECG开源社区 - CSDN博客https://blog.csdn.net/zhangdaiscott/article ...

  3. 分布式Tomcat session会话Sticky Sessions问题

    分布式session会话Sticky Sessions - tomcat_baby的专栏 - CSDN博客https://blog.csdn.net/tomcat_baby/article/detai ...

  4. Nginx负载均衡各种配置方式

    Nginx负载均衡 - 小刚qq - 博客园http://www.cnblogs.com/xiaogangqq123/archive/2011/03/04/1971002.html Module ng ...

  5. springboot注解@SpringBootApplication分析

    @SpringBootApplication注解用在Spring Boot的入口类上面,是Spring Boot提供的应用启动相关的注解. 直接上注解的源码: @Target(ElementType. ...

  6. mysql5.7 的 user表的密码字段从 password 变成了 authentication_string

    来源: http://www.zhimengzhe.com/shujuku/other/267631.html 感觉还是挺坑的 自己没了解清楚 就动手 转帖一下 mark 一下. 1.首先停止正在运行 ...

  7. SpringMVC+Spring+Mybatis+AngularJS 多规格保存示例代码

    insert时拿到最新增加的id值 绑定参数 js 实体类 Service实现类 Controller

  8. 剑指offer(17)层次遍历树

    题目: 从上往下打印出二叉树的每个节点,同层节点从左至右打印. public class Solution { ArrayList<Integer> list = new ArrayLis ...

  9. Flutter的输入框TextField

    TextFiled组件的API 先来看一下TextFiled的构造方法: const TextField({ Key key, this.controller, this.focusNode, thi ...

  10. linux查看端口是否开放

    在讨论这个问题前,我们先来了解一下物理端口.逻辑端口.端口号等计算机概念. 端口相关的概念: 在网络技术中,端口(Port)包括逻辑端口和物理端口两种类型.物理端口指的是物理存在的端口,如ADSL M ...