一个字符串 $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. Django_Form表单补充

    Form表单 问题1:  注册页面输入为空,报错:keyError:找不到password def clean(self): print("---",self.cleaned_da ...

  2. cookie、Session工作原理

    一.cookie机制和session机制的区别 具体来说cookie机制采用的是在客户端保持状态的方案,而session机制采用的是在服务器端保持状态的方案. 同时我们也看到,由于在服务器端保持状态的 ...

  3. android开发之网络棋牌类在线游戏开发心得(服务器端、Java) 好文章值得收藏

    标签: android服务器 2013-10-09 17:28 3618人阅读 评论(0) 收藏 举报 分类: android(11) 转自:http://blog.csdn.net/bromon/a ...

  4. loadrunner脚本篇——Run-time Settings之ContentCheck

    运用场景(很少用到): ContentCheck的设置可用来让VuGen检测存在错误的站点页面.如果被测的Web应用没有使用自定义的错误页面,那么这里不用添加规则,因为LR在回放时候,可以默认的捕捉到 ...

  5. hadoop03---nginx+keepalived

    1.1.反向代理 反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求 ...

  6. Hearbeat + Nginx 安装配置

    Hearbeat + Nginx 安装配置 实验环境 两台主机:Linux Centos 6.5 32位 主 服务端:Hearbeat + Nginx eth0:192.168.1.160(公网) e ...

  7. HTseq-count

    HTSeq:一个用于处理高通量数据(High-throughout sequencing)的python包.HTSeq包有很多功能类,熟悉python脚本的可以自行编写数据处理脚本.另外,HTSeq也 ...

  8. Kubernetes lxcfs

    容器实现的基础是NameSpace和Cgroups. NameSpace实现了对容器(进程)的隔离,NameSpace技术实际上修改了应用进程看待整个计算机“视图”,也就是作用域,即它的“视线”被操作 ...

  9. ScreenOS地址转换

    目录 1. NAT-src 1.1 来自DIP池(启用PAT)的NAT-src 1.2 来自DIP池(禁用PAT)的NAT-src 1.3 来自DIP池(带有地址变换)的NAT-src 1.4 来自出 ...

  10. 搭建配置cacti,采集信息监控

    安装cactilamp环境[iyunv@Cacti ~]#service iptables stop //关闭防火墙服务[iyunv@Cacti ~]#chkconfig iptables off / ...