https://www.lydsy.com/JudgeOnline/problem.php?id=1396
On the parent tree of the suffix automaton, if it is not a leaf node, then there are at least two child nodes
The number of occurrences of a substring represented by a state is the number of leaf nodes in the subtree
Therefore, only the substring represented by the state of the leaf node, namely |Right|=1, appears once
When we calculate each position as the right endpoint of the substring, its contribution to some positions
enumeration |Right|=1 state s
令end=Right(s)
Then with end as the right endpoint of the substring, the substring whose length is [1,Max(parent(s))] will at least appear in the state represented by the parent node of s
Therefore, when end is used as the right end point of the identified substring
The shortest length of position [end-Max(parent(s)),end] is Max(parent(s)+1
The shortest length of position [end-Max(s)+1, end-Max(parent(s))] is end-i+1
Maintenance with two segment trees
A direct maintenance minimum
The other tree maintains the minimum value of end+1, and the result is -i when querying
#include<cstdio> #include<cstring> #include<algorithm> #define N 100001 using namespace std; char s[N]; int ch[N<<1][26],tot=1; int fa[N<<1],len[N<<1]; int endpos[N<<1]; int siz[N<<1]; int last=1,p,q,np,nq; int leaf [N]; int v[N],sa[N<<1]; struct Segment { int mx[N<<2]; int tag[N<<2]; void down(int k) { mx[k<<1]=min(mx[k<<1],tag[k]); mx[k<<1|1]=min(mx[k<<1|1],tag[k]); tag[k<<1]=min(tag[k<<1],tag[k]); tag[k<<1|1]=min(tag[k<<1|1],tag[k]); tag[k] = 2e9; } void build(int k,int l,int r) { mx[k]=tag[k]=2e9; if(l==r) return; int mid=l+r>>1; build(k<<1,l,mid); build(k<<1|1,mid+1,r); } void change(int k,int l,int r,int opl,int opr,int w) { if(l>=opl && r<=opr) { mx [k] = min (mx [k], w); day[k] = min(day[k],w); return ; } int mid=l+r>>1; if(tag[k]!=2e9) down(k); if(opl<=mid) change(k<<1,l,mid,opl,opr,w); if(opr>mid) change(k<<1|1,mid+1,r,opl,opr,w); mx[k]=min(mx[k<<1],mx[k<<1|1]); } int query(int k,int l,int r,int x) { if(l==r) return mx[k]; int mid=l+r>>1; if(tag[k]!=2e9) down(k); if(x<=mid) return query(k<<1,l,mid,x); return query(k<<1|1,mid+1,r,x); } }; Segment tr1,tr2; void extend(int c) { len[np =++tot]=len[last]+ 1 ; endpos [np] = flax [np]; siz[tot]=1; for(p=last;p && !ch[p][c];p=fa[p]) ch[p][c]=np; if(!p) fa[np]=1; else { q=ch[p][c]; if(len[q]==len[p]+1) fa[np]=q; else { nq = ++ tot; fa [nq] = fa [q]; memcpy (ch [nq], ch [q], sizeof (ch [nq])); fa [q] = fa [np] = nq; len[nq]=len[p]+1; for(;ch[p][c]==q;p=fa[p]) ch[p][c]=nq; } } last=np; } intmain () { scanf("%s",s+1); int n=strlen(s+1); for(int i=1;i<=n;++i) { leaf [i] = tot + 1 ; extend(s[i]-'a'); } for(int i=1;i<=tot;++i) v[len[i]]++; for(int i=1;i<=n;++i) v[i]+=v[i-1]; for(int i=1;i<=tot;++i) sa[v[len[i]]--]=i; int x; for(int i=tot;i;--i) { x = sa [i]; endpos[fa[x]]=max(endpos[fa[x]],endpos[x]); you [fa [x]] + = you [x]; } tr1.build(1,1,n); tr2.build(1,1,n); int l,r,end; for(int x=2;x<=tot;++x) if(siz[x]==1) { l = len [fa [x]]; r = len [x]; end=endpos[x]; tr1.change(1,1,n,end-l,end,l+1); tr2.change(1,1,n,end-r+1,end-l,end+1); } int a,b; for(int i=1;i<=n;++i) { a=tr1.query(1,1,n,i); b=tr2.query(1,1,n,i)-i; printf("%d\n",min(a,b)); } return 0; }