1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn=1E6+5; 4 int n,f[maxn],cover[maxn],R[maxn]; 5 char str[maxn]; 6 int main() 7 { 8 ios::sync_with_stdio(false); 9 cin>>n; 10 for(int i=1;i<=n;++i) 11 cin>>str[i]; 12 int pos=0; 13 for(int i=2;i<=n;++i) 14 { 15 while(pos&&str[pos+1]!=str[i]) 16 pos=f[pos]; 17 if(str[pos+1]==str[i]) 18 ++pos; 19 f[i]=pos; 20 } 21 for(int i=1;i<=n;++i) 22 { 23 if(f[i]==0) 24 cover[i]=R[i]=i; 25 else 26 { 27 if(R[cover[f[i]]]+cover[f[i]]>=i) 28 { 29 cover[i]=cover[f[i]]; 30 R[cover[i]]=i; 31 } 32 else 33 cover[i]=R[i]=i; 34 } 35 } 36 for(int i=1;i<=n;++i) 37 cout<<cover[i]<<" "; 38 cout<<endl; 39 return 0; 40 }
k长cover?fail树上k级祖先,长剖即可。