一个字符串 $s$,你要把它分成若干段,有两种合法的段

1.段长为 $1$,代价为 $a$

2.这个段是前面所有段拼起来组成的字符串的字串,代价为 $b$

问最小代价

$|s| \leq 5000$

sol:

赛后看到带 log 的过了十分不解...

考虑 dp

$f_i = min(f_{i-1} + a,\space min\{f_j + b\})$ ($s[i+1,j]$ 要在 $s[1,i]$ 中作为子串出现)

子串的话写个 SAM 就可以线性求了,具体就是 extend 一个前缀,然后看后面能匹配几位

#include <bits/stdc++.h>
#define LL long long
#define rep(i, s, t) for (register int i = (s), i##end = (t); i <= i##end; ++i)
#define dwn(i, s, t) for (register int i = (s), i##end = (t); i >= i##end; --i)
using namespace std;
inline int read() {
int x = , f = ; char ch;
for (ch = getchar(); !isdigit(ch); ch = getchar()) if (ch == '-') f = -f;
for (; isdigit(ch); ch = getchar()) x = * x + ch - '';
return x * f;
}
int n, a, b;
int root, last, dfn;
int tr[][], fa[], mxlen[];
char s[];
int trans[][], f[];
void extend(int x) {
int p = last, np = last = ++dfn;
mxlen[np] = mxlen[p] + ;
for(; !tr[p][x]; p = fa[p]) tr[p][x] = np;
if(!p) fa[np] = root;
else {
int q = tr[p][x];
if(mxlen[p] == mxlen[q] + ) fa[np] = q;
else {
int nq = ++dfn; fa[nq] = fa[q];
memcpy(tr[nq], tr[q], sizeof(tr[nq]));
fa[q] = fa[np] = nq;
for(; tr[p][x] == q; p = fa[p]) tr[p][x] = nq;
}
}
}
int main() {
root = last = ++dfn;
n = read(), a = read(), b = read();
scanf("%s", s + );
rep(i, ,n) {
extend(s[i] - 'a');
int now = root;
rep(j, i+, n) {
if(tr[now][s[j] - 'a']) trans[j][i] = , now = tr[now][s[j] - 'a'];
else break;
}
}
rep(i, , n) {
f[i] = f[i - ] + a;
dwn(j, i-, ) if(trans[i][j]) f[i] = min(f[i], f[j] + b);
}
cout << f[n] << endl;
}

Technocup 2019 C. Compress String的更多相关文章

  1. [CareerCup] 1.5 Compress String 压缩字符串

    1.5 Implement a method to perform basic string compression using the counts of repeated characters. ...

  2. Codeforces Round #543 (Div. 2, based on Technocup 2019 Final Round)

    A. Technogoblet of Fire 题意:n个人分别属于m个不同的学校 每个学校的最强者能够选中 黑客要使 k个他选中的可以稳被选 所以就为这k个人伪造学校 问最小需要伪造多少个 思路:记 ...

  3. Codeforces Round #517 (Div. 2, based on Technocup 2019 Elimination Round 2)

    Codeforces Round #517 (Div. 2, based on Technocup 2019 Elimination Round 2) #include <bits/stdc++ ...

  4. NYOJ 1067 Compress String(区间dp)

    Compress String 时间限制:2000 ms  |  内存限制:65535 KB 难度:3 描写叙述 One day,a beautiful girl ask LYH to help he ...

  5. Codeforces Round #543 (Div. 1, based on Technocup 2019 Final Round) 题解

    题面戳这里 A. Diana and Liana 首先如果s>ks>ks>k一定无解,特判一下.那么我们考虑找恰好满足满足题目中的要求的区间[l,r][l,r][l,r],那么需要要 ...

  6. [Algo] 611. Compress String II

    Given a string, replace adjacent, repeated characters with the character followed by the number of r ...

  7. Technocup 2019 - Elimination Round 1

    http://codeforces.com/contest/1030 B. Vasya and Cornfield 判断点是否在矩形内(包括边界) 把每条边转化为一个不等式 public static ...

  8. Technocup 2019 - Elimination Round 2

    http://codeforces.com/contest/1031 (如果感觉一道题对于自己是有难度的,不要后退,懂0%的时候敲一遍,边敲边想,懂30%的时候敲一遍,边敲边想,懂60%的时候敲一遍, ...

  9. (AB)Codeforces Round #528 (Div. 2, based on Technocup 2019 Elimination Round

    A. Right-Left Cipher time limit per test 1 second memory limit per test 256 megabytes input standard ...

随机推荐

  1. 面向对象编程——super进阶(十)

    一.入门使用 在python中,使用super最常见的让子类继承父类.在这种情况下,当前类和对象可以作为super函数的参数使用,调用函数返回的任何方法都是调用超类的方法,而不是当前类的方法. cla ...

  2. 分布式计算开源框架Hadoop入门实践(二)

    其实参看Hadoop官方文档已经能够很容易配置分布式框架运行环境了,不过这里既然写了就再多写一点,同时有一些细节需要注意的也说明一下,其实也就是这些细节会让人摸索半天.Hadoop可以单机跑,也可以配 ...

  3. 2015.6.30 反弹的教训(想做T)

    心路:在6.29号,市场连续大跌!我到6.29号才想到可以做T+0.6.30消息面已经利好(双降准),已经计划做T+0(X先买后卖).  开市大跌至跌停.午后所有股票开始反弹.但是上午跌停时不敢入市, ...

  4. c#中使用NetCDF存储二维数据的读写操作简单应用

                      [DllImport(                   [DllImport(                  [DllImport(             ...

  5. ReactNative学习一

    ReactNative   主要学习来源于RN官方文档https://reactnative.cn/docs/0.51/getting-started.html 不过除了这个RN官方文档,其他RN中文 ...

  6. 单周期CPU设计

    终于有点时间了,恰好多周期的设计也已经完成,其实只想写写多周期的,无奈单周期补上才好,哈哈哈~ —————+—————黄金分割线—————+————— 首先要理解什么叫单周期CPU(与后面多周期CPU ...

  7. INSPIRED启示录 读书笔记 - 第20章 基本产品

    消减功能还是延长工期 不要再试图定义最终产品,转而定义只满足基本要求的产品,简称基本产品 1.产品经理与设计师合作设计产品的高保真原型,这个原型只具备实现商业目标的最基本功能要求,以及良好的用户体验和 ...

  8. 初始化dataframe

    由字典生成dataframe: >>> d = {'col1': [1, 2], 'col2': [3, 4]} >>> df = pd.DataFrame(dat ...

  9. Python词云的中文问题

    image= Image.open('F:/__identity/course/建模/九寨沟地震/四川地图.jpg') fig = plt.figure(figsize=(20, 16)) graph ...

  10. OpenStack with Opendaylight Part 1: Intro to Pipeline

    Using Vagrant to create vm nodes; devstack to start openstack using Opendaylight as ML2. Openstack w ...