题目描述

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. ad2017安装以及破解

    1.破Ad破解https://wenku.baidu.com/view/5e23a78e2e3f5727a5e962dd.html 2. Ad 汉化https://jingyan.baidu.com/ ...

  2. 同事写得Python对页面压测脚本

    #!/usr/bin/env python # *-* coding:utf-8 *-* import threading import requests import time # headers ...

  3. windows下linux子系统安装

    1.打开Windows功能中的使用于linux的Windows子系统 2.应用商店中下载需要的linux 3.下载完成后运行等待安装并输入用户名密码  4.查看系统信息 先后 sudo apt-get ...

  4. scroll滚动条样式修改

    一般我们有两种情况会出现滚动条,一种是overflow,一种是使用scroll. 当我们需要改变这个滚动条样式的时候,我们需要做以下的修改: html: <div id="style- ...

  5. 容错处理try

    var num = 90; try{ console.log( num + 100 ); consolel.log(aaa); }catch(e){ console.log("如果程序中有异 ...

  6. 如何让pl/sql developer记住密码,实现快速登录

    前两天,有同事使用plsql的时候,切换数据库的时候需要不断的重复输入密码,这样太麻烦了. 下面,我这里说下如何的实现plsql不需要输入密码就能快速登录的方法: 1.一开始登录,首先像往常那样输入密 ...

  7. SSH框架整合实现Java三层架构实例(一)

    HTML前台发送请求代码: <tr> <td>选择收派时间</td> <td> <input type="text" name ...

  8. 如何使用 Yum Repository 安装指定版本的 MySQL

    自从从使用 debian 系的 apt-get 转到使用 yum 工具之后一直不是很习惯,也没有去看过很多工具包安装的时候到底影响到了哪些文件等.这次借这次社区版 MySQL 安装来一并梳理一下. 首 ...

  9. xadmin快速搭建后台管理系统

    一.xadmin的特点: 1.基于Bootstrap3:Xadmin使用Bootstrap3.0框架精心打造.基于Bootstrap3,Xadmin天生就支持在多种屏幕上无缝浏览,并完全支持Boots ...

  10. peewee 事物 回滚

    peewee 事物 回滚 #!/usr/bin/env python # coding=utf-8 from peewee import * db = MySQLDatabase(host='123. ...