题目描述

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 读取配置文件数据

    Properties类 Properties类,是一个工具类,包含在java.util包中. 功能:可以保存持久的属性,通常用来读取配置文件或者属性文件,将文件中的数据读入properties对象中, ...

  2. 实验楼----PHP代码审计(sha1、md5)

    地址:http://www.shiyanbar.com/ctf/1787 题目:

  3. toTree

    // js实现树级递归, // 通过js生成tree树形菜单(递归算法) const data = [ { id: 1, name: "办公管理", pid: 0 }, { id: ...

  4. MySQL dump文件导入

    1 打开cmd 输入要导入的数据库,用户名,密码,dump文件路径 mysql -u employees <E:\employees_db\load_departments.dump

  5. vue-cli 上传图片上传到OSS(阿里云)

    https://help.aliyun.com/document_detail/32068.html?spm=5176.doc32069.6.304.Qc4SUs(看) https://help.al ...

  6. hashCode和equals的关系分析

    hashCode:说白了,简单的就看做一个函数,但是该函数有可能出现:对于某个x值,存在不止一个y值与之对应.这种情况就叫哈希碰撞. 那么: 1.如果hashCode相等,两个对象不一定是同一个对象( ...

  7. StringTokenizer

    StringTokenizer是一个用来分隔String的应用类,相当于VB的split函数. 1.构造函数 public StringTokenizer(String str) public Str ...

  8. delphi 中出现dataset not in edit or insert mode的问题

    self.ADOQuery2.Edit;self.ADOQuery2.First;while not self.ADOQuery2.Eof dobeginself.ADOQuery2.FieldByN ...

  9. Python——Flask框架——数据库

    一.数据库框架 Flask-SQLAlchemy (1)安装: pip install flask-sqlalchemy (2)Flask-SQLAlchemy数据库URL 数据库引擎 URL MyS ...

  10. SQL 添加索引

    使用CREATE 语句创建索引 CREATE INDEX index_name ON table_name(column_name,column_name) include(score) 普通索引 C ...