一个字符串 $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. Ubuntu 下 java 版本的切换

    切换的方法很简单,使用下面的两个命令即可: update-alternatives --config java update-alternatives --config javac eg: root@ ...

  2. [JavaScript]常用的页面倒计时

    倒计时是web开发中比较常用的,以下列出常用的几个倒计时方法,仅供参考: 一 :页面倒计时 原理一般都是通过 setTimeout 或 setInterval 函数实现,下面是一个最简单的倒计时 &l ...

  3. JSON.parse和JSON.stringify的作用

    //JSON.parse将字符串格式json转化为json对象 var str='{"name":"lingling","age":&quo ...

  4. constexpr与指针

    一. 常量表达式:是指值不会改变并且在编译过程就能得到的计算结果的表达式. 定义常量表达式变量: constexpr 变量类型 变量名: 例如: constexpr int mf=20://///20 ...

  5. 实现HTML格式的数据报表邮件

  6. MapReduce-二进制输入

    Hadoop的MapReduce不只是可以处理文本信息,它还可以处理二进制格式的数据1. 关于SequenceFileInputFormat类Hadoop的顺序文件格式存储二进制的键/值对的序列.由于 ...

  7. 域名注册中EAP期间是什么意思

    所谓域名申请期间的EAP指的是,域名优先注册期,行业上也称为“早期接入期”,这个期间的时间是由该域名所在的管理注册局定,而这个EPA期的时间长度也不一样,有的是一个星期,也有的长达两个星期. 域名EA ...

  8. myEclipse 导入maven项目时报错

    1.先右击项目,build Path 查看是否是某个JAR出了错.去掉无用的JAR 以及修改对应的JDK版本 2.检查src源文件,查看是否是源文件出了错.发现,还真的是 导入的包上面,报错:The ...

  9. 安装,配置webpack.

    1.下载node.js 2.在需要用到webpack的项目下打开命令窗口运行npm init生成package.js 3.安装webpack,使用npm install webpack --save- ...

  10. Spring Boot入门——web相关配置

    1.Servlet 引用HttpServlet接口,采用原生的Servlet进行请求响应 2.Listener 引用ServletContextListener,常用于Web缓存 3.Filter 引 ...