【BZOJ2882】工艺(后缀自动机)
2024-10-14 19:05:26
【BZOJ2882】工艺(后缀自动机)
题面
题解
还是一样的,先把串在后面接一遍
然后构建\(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;
}
最新文章
- WEB开发中的字符集和编码
- 基于HTML5和JS实现的切水果游戏
- 微信公众号红包接口开发PHP开发 CA证书出错,请登陆微信支付商户平台下载证书
- Android学习之路——简易版微信为例(一)
- tmux快捷键`
- bzoj1237
- centos升级python到2.7
- [TYVJ] P1023 奶牛的锻炼
- iOS storyBoard中tableViewCell传值方法
- Discuz的安装与使用
- bzoj3142 luogu3228 HNOI2013 数列
- 阻塞队列 - java基于链表的简单实现
- Python中的高级变量类型
- JsonUtils序列化与反序列化工具
- java performance tools / NetBeans Profiler / Sun BTrace / Eclipse MAT / IBM ISA
- Spring Boot 集成 Redis 实现缓存机制
- H5开发APP考题和答案
- 一个.net程序客户端更新方案
- STM32 Timer Clock sources -- External Clock Both Edge
- Python基础之多线程事件Event
热门文章
- Visual Studio Code 调整字体大小
- 织梦使用if判断某个字段是否为空
- 二分图最大匹配模板【匈牙利;Dinic最大流】
- 解决java.lang.IllegalStateException: BeanFactory not initialized or already closed - call &#39;refresh&#39; before accessing beans via the ApplicationContext这个问题
- 拥抱.NET Core系列:MemoryCache 缓存过期
- cassandra 鉴权
- SpringBoot中过滤器、监听器以及拦截器
- Ubuntu忘记root密码怎么办?
- C语言_指针变量的赋值与运算,很详细
- Java文件及文件夹的创建与删除