cf1121F. Compress String(后缀自动机)
题意
Sol
居然出个SAM板子也是没谁了233
#include<bits/stdc++.h>
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
//#define int long long
#define LL long long
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int MAXN = 2e5 + 1, mod = 1e9 + 7, INF = 1e9 + 10;
const double eps = 1e-9;
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
template <typename A> inline void debug(A a){cout << a << '\n';}
template <typename A> inline LL sqr(A x){return 1ll * x * x;}
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, a, b, f[MAXN];
char s[MAXN];
int len[MAXN], fa[MAXN], ch[MAXN][26], las = 1, tot = 1, root = 1, siz[MAXN];
bool g[5001][5001];
void insert(int x) {
int now = ++tot, pre = las; las = now; len[now] = len[pre] + 1; siz[now] = 1;
for(; pre && !ch[pre][x]; pre = fa[pre])
ch[pre][x] = tot;
if(!pre) {fa[now] = root; return ;}
int q = ch[pre][x];
if(len[q] == len[pre] + 1) fa[tot] = q;
else {
int nq = ++tot; fa[nq] = fa[q]; len[nq] = len[pre] + 1; fa[q] = fa[now] = nq;
memcpy(ch[nq], ch[q], sizeof(ch[q]));
for(; pre && ch[pre][x] == q; pre = fa[pre]) ch[pre][x] = nq;
}
}
void GGGG() {
for(int i = 1; i <= N; i++) {
insert(s[i] - 'a');
int now = root;
for(int j = i + 1; j <= N; j++) {
int x = s[j] - 'a';
if(ch[now][x]) g[j][i] = 1, now = ch[now][x];
else break;
}
}
}
signed main() {
N = read(); a = read(); b = read();
scanf("%s", s + 1);
GGGG();
for(int i = 1; i <= N; i++) {
f[i] = f[i - 1] + a;
for(int j = i - 1; j >= 1; j--)
if(g[i][j]) chmin(f[i], f[j] + b);
}
cout << f[N];
return 0;
}
/*
6
4 4 4 4 1 7
*/
cf1121F. Compress String(后缀自动机)的更多相关文章
- 【hihocoder#1413】Rikka with String 后缀自动机 + 差分
搞了一上午+接近一下午这个题,然后被屠了个稀烂,默默仰慕一晚上学会SAM的以及半天4道SAM的hxy大爷. 题目链接:http://hihocoder.com/problemset/problem/1 ...
- 牛客多校第四场 I string 后缀自动机/回文自动机
这个回文自动机的板有问题,它虽然能过这道题,但是在计算size的时候会出锅! 题意: 求一个字符串中本质不同的连续子串有几个,但是某串和它反转后的字符串算一个. 题解: 要注意的是,一般字符串题中的“ ...
- Codeforces 917F Substrings in a String - 后缀自动机 - 分块 - bitset - KMP
题目传送门 传送点I 传送点II 传送点III 题目大意 给定一个字母串,要求支持以下操作: 修改一个位置的字母 查询一段区间中,字符串$s$作为子串出现的次数 Solution 1 Bitset 每 ...
- 识别子串 (string)——后缀自动机+线段树
题目 [题目描述] 一般地,对于一个字符串 S,和 S 中第 $ i $ 个字符 x,定义子串 $ T=S(i.j) $ 为一个关于 x 的识别子申,当且仅当: 1.$ i \leq x \leq j ...
- #1413 : Rikka with String 后缀自动机 + 二级差分
http://hihocoder.com/problemset/problem/1413?sid=1199641 这题断断续续做了2个多星期吧,一直不会 设总答案为sum,替换后新加的子串数量为x,失 ...
- bzoj 5408: string 后缀自动机 + LCT
联赛前练练码力. code: #include <vector> #include <cstdio> #include <cstring> #include < ...
- 【Hihocoder1413】Rikka with String(后缀自动机)
[Hihocoder1413]Rikka with String(后缀自动机) 题面 Hihocoder 给定一个小写字母串,回答分别把每个位置上的字符替换为'#'后的本质不同的子串数. 题解 首先横 ...
- 【计蒜客】是男人就过 8 题--Pony.AI 题 A. A String Game 后缀自动机+SG函数
[题目]A. A String Game [题意]给定目标串S和n个子串Ti,Alice和Bob轮流选择一个子串操作,必须且只能在子串末尾添加一个字符使得新串也是S的子串,不能操作即输,求胜利者.|S ...
- 2020牛客暑期多校训练营(第四场) C - Count New String (字符串,广义后缀自动机,序列自动机)
Count New String 题意: 定义字符串函数 \(f(S,x,y)(1\le x\le y\le n)\),返回一个长度为y-x+1的字符串,第 i 位是 \(max_{i=x...x+k ...
随机推荐
- Python - 使用pycallgraph生成函数关系图
1- pycallgraph简介 可用于创建python函数关系图,依赖于dot命令,需要先安装 graphviz: HomePage:http://pycallgraph.slowchop.com/ ...
- Python - 一些值得阅读的PEP
1- PEP简介 PEP是Python增强提案(Python Enhancement Proposal)的缩写.社区通过PEP来给Python语言建言献策,每个版本的新特性和变化都是通过PEP提案经过 ...
- compose函数
compose函数 在学习redux源码的时候看到了其中的工具函数compose,compose函数的作用就是组合函数,依次组合传入的函数: 后一个函数作为前一个函数的参数 最后一个函数可以接受多个参 ...
- Kaazing Gateway简单使用
Kaazing GateWay是一种提供跨平台跨浏览器WebSocket支持的网关,由Java编写,介绍一下Kaazing GateWay的安装配置和简单使用,哪里说得不对,还请指出. 1. 安装 a ...
- mysql 开发进阶篇系列 54 权限与安全(账号管理的各种权限操作 下)
1. 查看权限 -- 如果host值不是%, 就要加上host值,下面查看bkpuser用户权限(6个权限, 限本地连接) SHOW GRANTS FOR bkpuser@localhost; -- ...
- mysql 开发进阶篇系列 19 MySQL Server(innodb_flush_log_at_trx_commit与sync_binlog)
一. innodb_flush_log_at_trx_commit 这个参数名称有个log,一看就是与日志有关.是指:用来控制缓冲区(log buffer)中的数据写入到日志文件(log file), ...
- 可以用py库: pyautogui (自动测试模块,模拟鼠标、键盘动作)来代替pyuserinput
PyAutoGUI 是一个人性化的跨平台 GUI 自动测试模块 pyUserInput模块安装前需要安装pywin32和pyHook模块.(想要装的看https://www.cnblogs.com/m ...
- C++常见笔试题
1.实现字符串转整数的函数:int atoi(const char *nptr) 2.实现数组折半查找:int BinarySearch(int a[],int len, int key) 3.实现字 ...
- less用法小结
1,采用koala进行编译,可以实时地在vscode这样的工具中看到less到css的转换: 2,均支持/**/以及//两种形式的注释,由于后期维护是维护less,因此推荐使用后者,因为后者不会被编译 ...
- 【MySQL经典案例分析】 Waiting for table metadata lock
本文由云+社区发表 一. 问题是这样来的 2018年某个周末,接到连续数据库的告警,告警信息如下: 二. 苦逼的探索过程 1.总体的思路 看到too many connection的报错信息,基本 ...