【BZOJ2882】工艺(后缀自动机)

题面

BZOJ权限题,良心洛谷

题解

还是一样的,先把串在后面接一遍

然后构建\(SAM\)

直接按照字典序输出\(n\)次就行了

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define ll long long
#define RG register
#define MAX 620000
inline int read()
{
RG int x=0,t=1;RG char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=-1,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return x*t;
}
struct Node
{
map<int,int> son;
int ff,len;
}t[MAX<<1];
int last=1,tot=1;
int n,a[MAX];
void extend(int c)
{
int p=last,np=++tot;last=np;
t[np].len=t[p].len+1;
while(p&&!t[p].son[c])t[p].son[c]=np,p=t[p].ff;
if(!p)t[np].ff=1;
else
{
int q=t[p].son[c];
if(t[q].len==t[p].len+1)t[np].ff=q;
else
{
int nq=++tot;
t[nq]=t[q];
t[nq].len=t[p].len+1;
t[q].ff=t[np].ff=nq;
while(p&&t[p].son[c]==q)t[p].son[c]=nq,p=t[p].ff;
}
}
}
int main()
{
n=read();
for(int i=1;i<=n;++i)a[i]=read();
for(int i=1;i<=n;++i)extend(a[i]);
for(int i=1;i<=n;++i)extend(a[i]);
for(int i=1,pos=1;i<=n;++i)
{
printf("%d ",t[pos].son.begin()->first);
pos=t[pos].son.begin()->second;
}
puts("");
return 0;
}

最新文章

  1. WEB开发中的字符集和编码
  2. 基于HTML5和JS实现的切水果游戏
  3. 微信公众号红包接口开发PHP开发 CA证书出错,请登陆微信支付商户平台下载证书
  4. Android学习之路——简易版微信为例(一)
  5. tmux快捷键`
  6. bzoj1237
  7. centos升级python到2.7
  8. [TYVJ] P1023 奶牛的锻炼
  9. iOS storyBoard中tableViewCell传值方法
  10. Discuz的安装与使用
  11. bzoj3142 luogu3228 HNOI2013 数列
  12. 阻塞队列 - java基于链表的简单实现
  13. Python中的高级变量类型
  14. JsonUtils序列化与反序列化工具
  15. java performance tools / NetBeans Profiler / Sun BTrace / Eclipse MAT / IBM ISA
  16. Spring Boot 集成 Redis 实现缓存机制
  17. H5开发APP考题和答案
  18. 一个.net程序客户端更新方案
  19. STM32 Timer Clock sources -- External Clock Both Edge
  20. Python基础之多线程事件Event

热门文章

  1. Visual Studio Code 调整字体大小
  2. 织梦使用if判断某个字段是否为空
  3. 二分图最大匹配模板【匈牙利;Dinic最大流】
  4. 解决java.lang.IllegalStateException: BeanFactory not initialized or already closed - call &#39;refresh&#39; before accessing beans via the ApplicationContext这个问题
  5. 拥抱.NET Core系列:MemoryCache 缓存过期
  6. cassandra 鉴权
  7. SpringBoot中过滤器、监听器以及拦截器
  8. Ubuntu忘记root密码怎么办?
  9. C语言_指针变量的赋值与运算,很详细
  10. Java文件及文件夹的创建与删除