题意

题目链接

Sol

居然出个SAM板子也是没谁了233

  1. #include<bits/stdc++.h>
  2. #define Pair pair<int, int>
  3. #define MP(x, y) make_pair(x, y)
  4. #define fi first
  5. #define se second
  6. //#define int long long
  7. #define LL long long
  8. #define Fin(x) {freopen(#x".in","r",stdin);}
  9. #define Fout(x) {freopen(#x".out","w",stdout);}
  10. using namespace std;
  11. const int MAXN = 2e5 + 1, mod = 1e9 + 7, INF = 1e9 + 10;
  12. const double eps = 1e-9;
  13. template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
  14. template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
  15. 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;}
  16. 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);}
  17. template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
  18. template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
  19. template <typename A> inline void debug(A a){cout << a << '\n';}
  20. template <typename A> inline LL sqr(A x){return 1ll * x * x;}
  21. inline int read() {
  22. char c = getchar(); int x = 0, f = 1;
  23. while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
  24. while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  25. return x * f;
  26. }
  27. int N, a, b, f[MAXN];
  28. char s[MAXN];
  29. int len[MAXN], fa[MAXN], ch[MAXN][26], las = 1, tot = 1, root = 1, siz[MAXN];
  30. bool g[5001][5001];
  31. void insert(int x) {
  32. int now = ++tot, pre = las; las = now; len[now] = len[pre] + 1; siz[now] = 1;
  33. for(; pre && !ch[pre][x]; pre = fa[pre])
  34. ch[pre][x] = tot;
  35. if(!pre) {fa[now] = root; return ;}
  36. int q = ch[pre][x];
  37. if(len[q] == len[pre] + 1) fa[tot] = q;
  38. else {
  39. int nq = ++tot; fa[nq] = fa[q]; len[nq] = len[pre] + 1; fa[q] = fa[now] = nq;
  40. memcpy(ch[nq], ch[q], sizeof(ch[q]));
  41. for(; pre && ch[pre][x] == q; pre = fa[pre]) ch[pre][x] = nq;
  42. }
  43. }
  44. void GGGG() {
  45. for(int i = 1; i <= N; i++) {
  46. insert(s[i] - 'a');
  47. int now = root;
  48. for(int j = i + 1; j <= N; j++) {
  49. int x = s[j] - 'a';
  50. if(ch[now][x]) g[j][i] = 1, now = ch[now][x];
  51. else break;
  52. }
  53. }
  54. }
  55. signed main() {
  56. N = read(); a = read(); b = read();
  57. scanf("%s", s + 1);
  58. GGGG();
  59. for(int i = 1; i <= N; i++) {
  60. f[i] = f[i - 1] + a;
  61. for(int j = i - 1; j >= 1; j--)
  62. if(g[i][j]) chmin(f[i], f[j] + b);
  63. }
  64. cout << f[N];
  65. return 0;
  66. }
  67. /*
  68. 6
  69. 4 4 4 4 1 7
  70. */

cf1121F. Compress String(后缀自动机)的更多相关文章

  1. 【hihocoder#1413】Rikka with String 后缀自动机 + 差分

    搞了一上午+接近一下午这个题,然后被屠了个稀烂,默默仰慕一晚上学会SAM的以及半天4道SAM的hxy大爷. 题目链接:http://hihocoder.com/problemset/problem/1 ...

  2. 牛客多校第四场 I string 后缀自动机/回文自动机

    这个回文自动机的板有问题,它虽然能过这道题,但是在计算size的时候会出锅! 题意: 求一个字符串中本质不同的连续子串有几个,但是某串和它反转后的字符串算一个. 题解: 要注意的是,一般字符串题中的“ ...

  3. Codeforces 917F Substrings in a String - 后缀自动机 - 分块 - bitset - KMP

    题目传送门 传送点I 传送点II 传送点III 题目大意 给定一个字母串,要求支持以下操作: 修改一个位置的字母 查询一段区间中,字符串$s$作为子串出现的次数 Solution 1 Bitset 每 ...

  4. 识别子串 (string)——后缀自动机+线段树

    题目 [题目描述] 一般地,对于一个字符串 S,和 S 中第 $ i $ 个字符 x,定义子串 $ T=S(i.j) $ 为一个关于 x 的识别子申,当且仅当: 1.$ i \leq x \leq j ...

  5. #1413 : Rikka with String 后缀自动机 + 二级差分

    http://hihocoder.com/problemset/problem/1413?sid=1199641 这题断断续续做了2个多星期吧,一直不会 设总答案为sum,替换后新加的子串数量为x,失 ...

  6. bzoj 5408: string 后缀自动机 + LCT

    联赛前练练码力. code: #include <vector> #include <cstdio> #include <cstring> #include < ...

  7. 【Hihocoder1413】Rikka with String(后缀自动机)

    [Hihocoder1413]Rikka with String(后缀自动机) 题面 Hihocoder 给定一个小写字母串,回答分别把每个位置上的字符替换为'#'后的本质不同的子串数. 题解 首先横 ...

  8. 【计蒜客】是男人就过 8 题--Pony.AI 题 A. A String Game 后缀自动机+SG函数

    [题目]A. A String Game [题意]给定目标串S和n个子串Ti,Alice和Bob轮流选择一个子串操作,必须且只能在子串末尾添加一个字符使得新串也是S的子串,不能操作即输,求胜利者.|S ...

  9. 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 ...

随机推荐

  1. sql的转义字符单引号

    在SQL中,我们都知道单引号 ' 表示字符串的开始和结束符号,如: select * from students where name = '小明'; 但如果字符串里面有单引号时,应该怎么查询呢? 这 ...

  2. 高清语音技术(WBS)及其在手机和蓝牙耳机中的实现

    高清语音也被称为宽带语音,是一种能为蜂窝网络.移动电话和无线耳机传输高清.自然语音质量的音频技术.与传统的窄带电话相比,高清语音很大程度上提高了语音质量,减少了听觉负担. 通信产业链上的所有网络和设备 ...

  3. Docker安装配置MongoDB并使用Robo 3T在局域网连接

    主要参考了这位老兄的文章(传送门),写的还是比较详细的. 不废话直接来正经的. 1.docker pull mongo 从docker hub拉取最新的mongo镜像文件,300多M大小. 2.doc ...

  4. 【PHP篇】字符串基础

    1.声明时既可以用双引号也可以用单引号 str1 =”字符串值”;    //可解析引号里的变量等内容 str2=’字符串值’;     //不可解析内容 2.字符串没有长度限制,但要注意内存的消耗 ...

  5. SVN+Axure版本管理&协同设计(一)

    1. SVN介绍 2.安装部署 环境介绍:Ubuntu16

  6. tensorflow 1.0 学习:参数和特征的提取

    在tf中,参与训练的参数可用 tf.trainable_variables()提取出来,如: #取出所有参与训练的参数 params=tf.trainable_variables() print(&q ...

  7. mysql 开发进阶篇系列 15 锁问题 (总结)

    1. innodb 行锁是基于索引实现的,如果不通过索引访问数据,innodb会使用表锁. http://www.cnblogs.com/MrHSR/p/9376086.html 2. Innodb ...

  8. java 容器 集合 用法

    Set,List,Map,Vector,ArrayList的区别 JAVA的容器---List,Map,Set Collection ├List │├LinkedList │├ArrayList │└ ...

  9. 【web开发】docker中的数据库

    注:自从开始使用docker,部署方面的事情就简单多了.使用docker构建的数据库容器不用直接安装,开启后就可以使用,也比以前方便很多.下面将一些要点记录下来. 下面的例子使用以下环境: - 系统( ...

  10. 字符串不相同出现相同HashCode(算法)

    转自:https://blog.csdn.net/fly_grass_fish/article/details/81742794 在Java中有HashCode的说法,以前以为HashCode是唯一的 ...