CF1120 C. Compress String(SAM+DP)
有方程dp[i]=min(dp[i-1]+A,dp[j]+B);如果s[j+1,i]在s[i,j]中出现,所以我们就是要知道每个子串在s出现的第一个位置,这个可以hash实现或者sam,或者kmp实现。
pos[i][j]表示s[i,j]对应的sam的位置,occ[],表示第一次出现的位置。
#include<bits/stdc++.h>
#define ll long long
#define rep2(i,a,b) for(int i=a;i>=b;i--)
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
char c[maxn],s[maxn];
struct SAM{
int ch[maxn][],fa[maxn],maxlen[maxn],cnt,last;
int a[maxn],b[maxn],occ[maxn];
void init()
{
cnt=last=;
memset(ch[],,sizeof(ch[]));
memset(occ,0x3f,sizeof(occ));
}
int add(int x,int pos)
{
int np=++cnt,p=last; last=np; occ[np]=pos;
maxlen[np]=maxlen[p]+;memset(ch[np],,sizeof(ch[np]));
while(p&&!ch[p][x]) ch[p][x]=np,p=fa[p];
if(!p) fa[np]=;
else {
int q=ch[p][x];
if(maxlen[q]==maxlen[p]+) fa[np]=q;
else {
int nq=++cnt; maxlen[nq]=maxlen[p]+;
fa[nq]=fa[q]; fa[q]=fa[np]=nq;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
while(p&&ch[p][x]==q) ch[p][x]=nq,p=fa[p];
}
}
}
void Sort()
{
rep(i,,cnt) a[i]=;
rep(i,,cnt) a[maxlen[i]]++;
rep(i,,cnt) a[i]+=a[i-];
rep(i,,cnt) b[a[maxlen[i]]--]=i;
for(int i=cnt;i>=;i--)
occ[fa[b[i]]]=min(occ[b[i]],occ[fa[b[i]]]);
}
}T;
int a[maxn],A,B,dp[maxn],pos[][];
int main()
{
int N;
scanf("%d%d%d%s",&N,&A,&B,c+);
T.init();
rep(i,,N) T.add(c[i]-'a',i);
rep(i,,N) {
int now=;
rep(j,i,N){
now=T.ch[now][c[j]-'a'];
pos[i][j]=now;
}
}
T.Sort();
rep(i,,N){
dp[i]=dp[i-]+A;
rep(j,,i-){
if(T.occ[pos[j+][i]]<=j){
dp[i]=min(dp[i],dp[j]+B);
break;
}
}
}
printf("%d\n",dp[N]);
return ;
}
CF1120 C. Compress String(SAM+DP)的更多相关文章
- NYOJ 1067 Compress String(区间dp)
Compress String 时间限制:2000 ms | 内存限制:65535 KB 难度:3 描写叙述 One day,a beautiful girl ask LYH to help he ...
- spoj 1812 LCS2(SAM+DP)
[题目链接] http://www.spoj.com/problems/LCS2/en/ [题意] 求若干个串的最长公共子串. [思路] SAM+DP 先拿个串建个SAM,然后用后面的串匹配,每次将所 ...
- hdu_3336: Count the string(KMP dp)
题目链接 题意:求给定字符串中,可以与某一前缀相同的所有子串的数量 做这道题需要明白KMP算法里next[]数组的意义 首先用一数组nex[](这里与之前博客中提到的next明显不同)存储前缀后缀最长 ...
- hdu 3336 count the string(KMP+dp)
题意: 求给定字符串,包含的其前缀的数量. 分析: 就是求所有前缀在字符串出现的次数的和,可以用KMP的性质,以j结尾的串包含的串的数量,就是next[j]结尾串包含前缀的数量再加上自身是前缀,dp[ ...
- 【HDU 3336】Count the string(KMP+DP)
Problem Description It is well known that AekdyCoin is good at string problems as well as number the ...
- [HAOI2016]找相同字符(SAM+DP)
感觉很水. 因为SAM上一个点的子树大小代表这个点所表示子串的出现次数. 建出广义后缀自动机之后.在\(parent\)树上跑\(DP\),维护\(size[i][1]\),和\(size[i][0] ...
- F. Clear the String(区间 DP )//每次都删除一个相同字符的子串 , 最小多少次
https://codeforces.com/contest/1132/problem/F 借鉴:https://www.cnblogs.com/chhokmah/p/10508762.html 题意 ...
- 2018.12.12 codeforces 931E. Game with String(概率dp)
传送门 感觉这题难点在读懂题. 题目简述:给你一个字符串s,设将其向左平移k个单位之后的字符串为t,现在告诉你t的第一个字符,然后你可以另外得知t的任意一个字符,求用最优策略猜对k的概率. 解析: 预 ...
- 【Hihocoder1413】Rikka with String(后缀自动机)
[Hihocoder1413]Rikka with String(后缀自动机) 题面 Hihocoder 给定一个小写字母串,回答分别把每个位置上的字符替换为'#'后的本质不同的子串数. 题解 首先横 ...
随机推荐
- ActiveMQ queue和topic,持久订阅和非持久订阅
消息的 destination 分为 queue 和 topic,而消费者称为 subscriber(订阅者).queue 中的消息只会发送给一个订阅者,而 topic 的消息,会发送给每一个订阅者. ...
- linux的典型分支:
1.redhat 2.debian 3.centOS 4.ubuntu 5.fedora 6.kali linux
- python---多线程与多进程
一. 单进程多线程 1. 使用的模块是Threading.使用join()函数进行阻塞. from pdf2txt import pdfTotxt1, pdfTotxt2 import xlrd im ...
- asp.net mvc 笔记一
webapi controller 中 action 名称 不能与 View controller 中的 action 名称相同,否则 Url.Action("actionName&quo ...
- spring bean 的生命周期
感谢博友,内容源于博友的文章 http://www.cnblogs.com/zrtqsk/p/3735273.html 通过了解spring的bean 的生命周期 ,再结合jdk的注解,继承sprin ...
- rap使用手册
https://github.com/thx/RAP/wiki/user_manual_cn
- java构造函数使用方法总结 (继承与构造函数)
使用构造器时需要记住: 1.构造器必须与类同名(如果一个源文件中有多个类,那么构造器必须与公共类同名) 2.每个类可以有一个以上的构造器 3.构造器可以有0个.1个或1个以上的参数 4.构造器没有返回 ...
- matlab运行中出现“Caught "std::exception" Exception message is: Message Catalog MATLAB:builtins was not loaded from the file."
在我运行过程中,经常爆出这一不确定是什么的问题,经排查后发现,原来是fopen 文件后,没有及时fclose导致的.
- 20165326 java第四周学习笔记
第四周学习笔记 ch5 子类和父类 子类只能有一个父类 使用关键字extendsyclass 子类 extends 父类 系统默认的祖先类Object(java.lang包中) 继承:子类继承父类的方 ...
- Java 内存监控命令简介(零)
一.Java性能监控与调优命令.工具简介 1.jps :查看当前运行的Java程序端口号,包括运行jps的程序端口号. 2.jinfo :查看Java进程的运行时信息. 3.jmap + MAT :通 ...