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 ...
随机推荐
- MySQL数据库(2)_MySQL数据库和数据库表操作语句
一.关于数据库操作的sql语句 -- .创建数据库(在磁盘上创建一个对应的文件夹) create database [if not exists] db_name [character set xxx ...
- Ubuntu 安装VMware Tools
安装步骤: 首先,点击VMware菜单的-VM-Install VMware Tools (虚拟机-装载VMwareTool 工具) 这时,在Ubuntu下会自动加载Linux版的VMware Too ...
- springboot-整合freemarker
freemarker是一个页面模板引擎.用springboot整合freemarker的方式如以下步骤: 1.在创建springboot的项目的时候,选择freemarker的组件,或者自己手动在ma ...
- 在freescale mx6q平台上添加spi资源
1:配置管脚为SPI功能 在board-mx6q_sabresd.h的最后添加,复制被重定义 (以添加SPI2为例) <span style="font-size:18px;" ...
- 【Head First Servlets and JSP】笔记7:如何创建一个全局的dog?
重定向与请求分派 “局部”参数——ServletConfig——servlet初始化参数 “全局”参数——ServletContext——上下文初始化参数 Web app的“构造器”——Servlet ...
- 跨平台移动开发_Windows Phone 8 使用 PhoneGap 方法
第一步,下载Windows Phone SDK 8.0 http://www.microsoft.com/zh-cn/download/details.aspx?id=35471 离线下载地址(推荐使 ...
- mysql下merge分表
1.merge简介分表就是把N条记录的表,分成若干个分表,各个分表记录的总和仍为N. 分表的方法有很多,用merge来分表,是最简单的一种方式.merge是mysql的一种存储引擎,它把一组MyISA ...
- Ubuntu: 无法使用su命令
Ubuntu 无法使用su命令解决方案 在Ubuntu上编译Qt环境时发现无法使用su命令切换到root用户,通过网上查找发现解决方案如下: xt@xt-ubuntu:~$ su密码: su:认证失败 ...
- Grafana连接Prometheus监控Docker平台
Grafana是一款开源的分析平台. Grafana allows you to query, visualize, alert on and understand your metrics no m ...
- CCNA 课程 二
传输层:两个重要的协议 TCP 和 UDP TCP: 面向连接的协议:在传输用户数据前,先要建立连接 (TCP的三次握手) 错误检查 数据包序列化 可靠性传输:发送的数据需要接受者提供确认,通过报头中 ...