#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
const int N=1e6+10,S=62;
inline int get(char ch)
{
if(ch>='0'&&ch<='9') return ch-'0';
if(ch>='A'&&ch<='Z') return ch-'A'+10;
if(ch>='a'&&ch<='z') return ch-'a'+36;
}
struct SAM
{
int tot,tail;
int son[N][S],fa[N],maxlen[N],pos[N];
inline void init()
{
tot=tail=1;
memset(son,0,sizeof(son));
memset(fa,0,sizeof(fa));
}
inline void copy(int p,int q)
{
fa[p]=fa[q],pos[p]=pos[q];
for(int i=0;i<S;i++) son[p][i]=son[q][i];
}
inline void ins(int ch)
{
int p=tail,now=tail=++tot;
maxlen[now]=pos[now]=maxlen[p]+1;
for(;p&&!son[p][ch];p=fa[p])
son[p][ch]=now;
if(!p){ fa[now]=1; return; }
int cur1=son[p][ch],cur2;
if(maxlen[cur1]==maxlen[p]+1){ fa[now]=cur1; return; }
copy(cur2=++tot,cur1);
maxlen[cur2]=maxlen[p]+1;
for(;p&&son[p][ch]==cur1;p=fa[p])
son[p][ch]=cur2;
fa[cur1]=fa[now]=cur2;
}
}t;
struct graph
{
int tot;
int fir[N],to[N],nxt[N];
graph(){ tot=0; memset(fir,0,sizeof(fir)); }
inline void add(int x,int y)
{
to[++tot]=y; nxt[tot]=fir[x]; fir[x]=tot;
}
}e;
int n,rk;
char s[N];
int id[N],v[N],ans[N];
inline bool cmp(int a,int b){ return v[a]>v[b]; }
inline void dfs(int p)
{
if(t.maxlen[p]==t.pos[p]&&p!=1) ans[++rk]=n-t.pos[p]+1;
for(int i=e.fir[p];i;i=e.nxt[i])
dfs(e.to[i]);
}
int main()
{
t.init();
scanf("%s",s+1);
n=strlen(s+1);
for(int i=n;i>=1;i--)
t.ins(get(s[i]));
for(int i=1;i<=t.tot;i++)
v[i]=get(s[n-(t.pos[i]-t.maxlen[t.fa[i]])+1]),id[i]=i;
sort(id+1,id+t.tot+1,cmp);
for(int i=1;i<=t.tot;i++)
e.add(t.fa[id[i]],id[i]);
dfs(1);
for(int i=1;i<=n;i++)
printf("%d ",ans[i]);
return 0;
}剪贴板
剪贴板 gl9azt18
剪贴板信息
剪贴板信息
- 收录
- 更新