CF 1107 E. Vasya and Binary String

E. Vasya and Binary String

链接

分析:

  对于长度为x的一段序列,我们可以dp出消除的过程的最优方案,背包即可。

  然后区间dp,可以先合并完所有的点,即没相同的一段区间合并为一个点。设f[i][j][k]表示消完区间[i,j]和这段区间后面k个元素最大值,其中k个元素的颜色与点j的颜色相同。

  转移:可以首先将j和后面k个元素消除,然后消除[i,j-1]。也可以枚举一个和j颜色相同的点m,然后分别先消除[m+1,r-1],剩下的区间就和后面k个连在一起了,再递归求出。

代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef long long LL;

inline int read() {
    int x=0,f=1;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch==‘-‘)f=-1;
    for(;isdigit(ch);ch=getchar())x=x*10+ch-‘0‘;return x*f;
}

const int N = 105;
int n, cnt;
LL a[N], f[N], g[N][N][N];
char s[N];
struct Node { int size, col; } b[N];

void init() {
    for (int i = 1; i <= n; ++i)
        for (int j = i; j <= n; ++j) f[j] = max(f[j], f[j - i] + a[i]);
    int now = 1;
    for (int i = 2; i <= n; ++i) {
        if (s[i] == s[i - 1]) now ++;
        else cnt ++, b[cnt].size = now, b[cnt].col = s[i - 1] - ‘0‘, now = 1;
    }
    ++cnt; b[cnt].size = now, b[cnt].col = s[n] - ‘0‘;
}
LL DP(int l,int r,int k) {
    if (l == r) return f[b[l].size + k];
    if (g[l][r][k] != -1) return g[l][r][k]; //~
    LL res = DP(l, r - 1, 0) + f[b[r].size + k];
    for (int i = l; i < r - 1; ++i)
        if (b[i].col == b[r].col) res = max(res, DP(i + 1, r - 1, 0) + DP(l, i, b[r].size + k));
    return g[l][r][k] = res;
}
int main() {
    n = read();
    scanf("%s", s + 1);
    for (int i = 1; i <= n; ++i) a[i] = read();
    init();
    memset(g, -1, sizeof(g));
    cout << DP(1, cnt, 0);
    return 0;
}

原文地址:https://www.cnblogs.com/mjtcn/p/10507598.html

时间: 2024-11-10 11:12:15

CF 1107 E. Vasya and Binary String的相关文章

UVA - 10559 Blocks 和 Vasya and Binary String CodeForces - 1107E (dp OR 记忆化搜索)

UVA - 10559 Blocks 题意:消消乐,每次连续相同的可以消除,分数加上长度的平方,问最多可以获得几分全部消完 题解: 区间dp + 记忆化搜索 dp[i][j][k] : (区间 [i,  j] 后面带上一段和 j 颜色相同的且长度为 k )的消消乐最大积分 1.消最后一段颜色和 j 颜色相同的 dp[i][j][k] <-- dp[i][j-1][0] + (k+1)^2 2.对于i <= l < j, 如果 l 和 j 的颜色相同, 那么可以把 [l+1, j-1]消掉

CF1107E Vasya and Binary String

比赛的时候又被垃圾题艹翻了啊. 这个题显然是区间dp 考虑怎么转移. 类似消除方块和ZYB玩字符串那样的一个DP. 可以从左到右依次考虑消除. dp[l][r][k][flag]表示区间l,r左边粘着k个flag. 转移方式: 1.考虑强行去继续黏上下一个字符并使k+1. 2.考虑把一段跳过去,让被跳过的这一段自行不与外界产生关系被消除,然后再去消除后面的. 3.直接消除当前粘着的一段,并获得收益. #include<iostream> #include<cctype> #incl

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 such that there are exactly a zeroes, exactly b ones and exactly x indices i (where 1≤i<n) such that si≠si+1. It is guaranteed that the answer always

NYOJ 5 Binary String Matching【string find的运用】

Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alphabet consist only '0' and '1'. Your task is only to tell how many times does A appear as a substring of B? For example, the text string B is '100111011

NYOJ5 Binary String Matching

Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alphabet consist only ‘0’ and ‘1’. Your task is only to tell how many times does A appear as a substring of B? For example, the text string B is ‘100111011

NYOJ 5 Binary String Matching (kmp 字符串匹配)

Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alphabet consist only '0' and '1'. Your task is only to tell how many times does A appear as a substring of B? For example, the text string B is '100111011

NYOJ 5 Binary String Matching

Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alphabet consist only ‘0’ and ‘1’. Your task is only to tell how many times does A appear as a substring of B? For example, the text string B is ‘100111011

nyoj5 Binary String Matching(KMP)

Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alphabet consist only '0' and '1'. Your task is only to tell how many times does A appear as a substring of B? For example, the text string B is '100111011

How to convert a byte to its binary string representation

How to convert a byte to its binary string representation For example, the bits in a byte B are 10000010, how can I assign the bits to the string strliterally, that is, str = "10000010". byte b1 = (byte) 129; String s1 = String.format("%8s&