后缀自动机沙茶题

将字符串复制一次,建立后缀自动机.

在后缀自动机上贪心走 $n$ 次即可.

Code:

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
#define setIO(s) freopen(s".in","r",stdin)
#define maxn 1000000
using namespace std;
int n,arr[maxn],m,last=1,tot=1,len[maxn],f[maxn];
map<int,int>ch[maxn];
map<int,int>::iterator it;
void ins(int c){
int p=last,np=last=++tot,q;
len[np]=len[p]+1;
while(p&&!ch[p][c]) ch[p][c]=np,p=f[p];
if(!p) f[np]=1;
else{
q=ch[p][c];
if(len[q]==len[p]+1)f[np]=q;
else
{
int nq=++tot;
len[nq]=len[p]+1;
ch[nq]=ch[q];
f[nq]=f[q];
f[np]=f[q]=nq;
while(p&&ch[p][c]==q)
ch[p][c]=nq,p=f[p];
}
}
}
int main(){
//setIO("input");
scanf("%d",&n),m=2*n;
for(int i=1;i<=n;++i) scanf("%d",&arr[i]);
for(int i=n+1;i<=m;++i) arr[i]=arr[i-n];
for(int i=1;i<=m;++i) ins(arr[i]);
int p=1;
while(n -- )
{
it=ch[p].begin();
printf("%d" , it->first);
if(n) printf(" ");
p = it->second;
}
return 0;
}

  

最新文章

  1. angular
  2. vue实例属性(vm.$els)
  3. jquery.query.js 插件(示例及简单应用)
  4. Oracle登录操作系统验证和密码文件验证
  5. JAVA 处理程序异常,(try、catch、finally),(thorws)
  6. CF A and B and Compilation Errors (排序)
  7. android95 缩放加载大图片
  8. DOM - nodeType 的取值
  9. poj1611 并查集
  10. 剑指offer-面试题2.实例Singleton模式
  11. 关于Struts2的碎碎念
  12. Android 学习开发笔记《Service 与 Thread 的区别 》
  13. CSS中position详解与常见应用实现
  14. (中等) HDU 4370 0 or 1,建模+Dijkstra。
  15. CSS之绝对定位
  16. k8s应用机密信息与配置管理(九)--技术流ken
  17. UVA1194 Machine Schedule
  18. 对Faster R-CNN的理解(1)
  19. Python基础点
  20. 常用的php数组函数

热门文章

  1. SSH Key的生成和使用(for git)
  2. AngularJS1 学习笔记1
  3. 百度编辑器UEditor修改成支持物理路径
  4. Uva 1605 Building for UN【构造法】
  5. SpringCloud学习笔记(1)----认识微服务与SpringCloud
  6. Mathab和Python的numpy中的数组维度
  7. [CQOI2015]任务查询系统 主席树_差分
  8. .NET 拼音汉字转化(全面)
  9. PHP JWT初识
  10. Fiddler 接口测试(Composer)的使用方法