Technocup 2019 C. Compress String
一个字符串 $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的更多相关文章
- [CareerCup] 1.5 Compress String 压缩字符串
1.5 Implement a method to perform basic string compression using the counts of repeated characters. ...
- Codeforces Round #543 (Div. 2, based on Technocup 2019 Final Round)
A. Technogoblet of Fire 题意:n个人分别属于m个不同的学校 每个学校的最强者能够选中 黑客要使 k个他选中的可以稳被选 所以就为这k个人伪造学校 问最小需要伪造多少个 思路:记 ...
- 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++ ...
- NYOJ 1067 Compress String(区间dp)
Compress String 时间限制:2000 ms | 内存限制:65535 KB 难度:3 描写叙述 One day,a beautiful girl ask LYH to help he ...
- 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],那么需要要 ...
- [Algo] 611. Compress String II
Given a string, replace adjacent, repeated characters with the character followed by the number of r ...
- Technocup 2019 - Elimination Round 1
http://codeforces.com/contest/1030 B. Vasya and Cornfield 判断点是否在矩形内(包括边界) 把每条边转化为一个不等式 public static ...
- Technocup 2019 - Elimination Round 2
http://codeforces.com/contest/1031 (如果感觉一道题对于自己是有难度的,不要后退,懂0%的时候敲一遍,边敲边想,懂30%的时候敲一遍,边敲边想,懂60%的时候敲一遍, ...
- (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 ...
随机推荐
- 【转】SVN使用教程总结
看到一篇超赞的文章,原链接:http://www.cnblogs.com/armyfai/p/3985660.html SVN简介: 为什么要使用SVN? 程序员在编写程序的过程中,每个程序员都会生成 ...
- Linux安装Mycat
1.官网下载mycat到/home/install目录下 2.解压到/usr/local/mycat目录下 tar -zxvf Mycat-server-1.6-RELEASE-20161028204 ...
- 设计模式—迭代器Iterator模式
什么是迭代器模式? 让用户通过特定的接口访问容器的数据,不需要了解容器内部的数据结构. 首先我们先模仿集合中ArrayList和LinkedList的实现.一个是基于数组的实现.一个是基于链表的实现, ...
- Linux 基础一---操作系统&常用命令
UNIX是一个计算机操作系统,一个用来协调.管理和控制计算机硬件和软件资源的控制程序. 1.UNIX操作系统的特点:多用户和多任务: a) 多用户表示在同一时刻可以有多个用户同时使用UNIX操作系统而 ...
- Too many open files 问题
1.解决办法 (1)查看 查看当前系统打开的文件数量 lsof | wc -l watch "lsof | wc -l" 查看某一进程的打开文件数量 lsof -p pid | w ...
- MySQL几个重要的目录
MySQL几个重要的目录 1 数据库目录 /var/lib/mysql/ 2 配置文件 /usr/share/mysql(mysql.server命令及配置文件) 3 相关命令 /usr/bin(my ...
- HDU 2603 二分匹配
#include <queue>#include <vector>#include <cstdio>#include <cstring>#include ...
- jenkins tomcat
tomcat增加用户配置: <role rolename="tomcat"/> <role rolename="role1"/> < ...
- nginx常见面试题1
Nginx是网页服务器运维人员不可能绕开的一个弯,剩下几个比较高危的面试范围是:linux基础.网络知识基础.python,或许还会有zabbix等监控工具.这里先说nginx,后面几个肯定也会写. ...
- linux 安装mysql服务
1.检查是否已安装,grep的-i选项表示匹配时忽略大小写 rpm -qa|grep -i mysql *可见已经安装了库文件,应该先卸载,不然会出现覆盖错误.注意卸:载时使用了--nodeps选项, ...