BZOJ4516: [Sdoi2016]生成魔咒
2016-05-11 14:36
316 查看
Description
魔咒串由许多魔咒字符组成,魔咒字符可以用数字表示。例如可以将魔咒字符 1、2 拼凑起来形成一个魔咒串 [1,2]。一个魔咒串 S 的非空字串被称为魔咒串 S 的生成魔咒。
例如 S=[1,2,1] 时,它的生成魔咒有 [1]、[2]、[1,2]、[2,1]、[1,2,1] 五种。S=[1,1,1] 时,它的生成魔咒有 [1]、
[1,1]、[1,1,1] 三种。最初 S 为空串。共进行 n 次操作,每次操作是在 S 的结尾加入一个魔咒字符。每次操作后都
需要求出,当前的魔咒串 S 共有多少种生成魔咒。
Input
第一行一个整数 n。第二行 n 个数,第 i 个数表示第 i 次操作加入的魔咒字符。
1≤n≤100000。,用来表示魔咒字符的数字 x 满足 1≤x≤10^9
Output
输出 n 行,每行一个数。第 i 行的数表示第 i 次操作后 S 的生成魔咒数量Sample Input
71 2 3 3 3 1 2
Sample Output
13
6
9
12
17
22
强上后缀自动机,用个map存边就行了。
时间复杂度O(NlogN)。
#include<cstdio> #include<cctype> #include<map> #include<cstring> #include<algorithm> #define rep(i,s,t) for(int i=s;i<=t;i++) #define dwn(i,s,t) for(int i=s;i>=t;i--) #define ren for(int i=first[x];i;i=next[i]) using namespace std; const int BufferSize=1<<16; char buffer[BufferSize],*head,*tail; inline char Getchar() { if(head==tail) { int l=fread(buffer,1,BufferSize,stdin); tail=(head=buffer)+l; } return *head++; } inline int read() { int x=0,f=1;char c=Getchar(); for(;!isdigit(c);c=Getchar()) if(c=='-') f=-1; for(;isdigit(c);c=Getchar()) x=x*10+c-'0'; return x*f; } const int maxn=200010; long long ans; map<int,int>to[maxn]; int l[maxn],fa[maxn],cnt=1,last=1; void extend(int c) { int p,q,np,nq;p=last;last=np=++cnt;l[np]=l[p]+1; for(;!to[p][c];p=fa[p]) to[p][c]=np; if(!p) fa[np]=1; else { q=to[p][c]; if(l[p]+1==l[q]) fa[np]=q; else { l[nq=++cnt]=l[p]+1; ans-=l[q]-l[fa[q]]; fa[nq]=fa[q];fa[q]=fa[np]=nq; ans+=l[q]-l[fa[q]];ans+=l[nq]-l[fa[nq]]; to[nq]=to[q]; for(;to[p][c]==q;p=fa[p]) to[p][c]=nq; } } ans+=l[np]-l[fa[np]]; } int main() { dwn(i,read(),1) { extend(read()); printf("%lld\n",ans); } return 0; }
相关文章推荐
- 【Django】依赖auth.user的数据库迁移,以及admin用户非交互式创建
- 【FLUENT案例】06:与EDEM耦合计算
- 画饼状图一
- SQL查询语句通配符与ACCESS模糊查询like的解决方法
- 沟通的艺术,心理学与生活,学会提问
- python学习笔记10(Python的内存管理)
- 操作系统开发系列—13.d.多进程 ●
- JavaScript复习第二天js小特效练习
- 用AndroidStudio导入github项目
- JAVA 笔记(一)
- 时间戳转成字符串时间或者日期
- 【代码】key-value模式下的哈希二次探测与简单的哈希类的实现
- Bit masking for introspection of Objective-C object pointer is strongly discounraged
- 屏幕是横屏时,让页面也随着屏幕以横屏形式显示
- 记录一个非常实用的gif制作工具licecap
- linux如何关闭防火墙
- JDK1.5,Switch中使用Enum
- 【Spring】Spring oxm 入门初尝试
- jquery的on()方法控制还未创建的节点
- WPF 中动态创建和删除控件