#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define maxn 5000
#define eps 1e-10
int r[maxn];int n;
int sa[maxn],rank[maxn],height[maxn];
int wa[maxn],wb[maxn],wv[maxn],wss[maxn];
int cmp(int *r,int a,int b,int l){
return r[a]==r[b]&&r[a+l]==r[b+l];
}
void da(int *r,int *sa,int n,int m)
{
int i,j,p,*x=wa,*y=wb,*t;
for(i=0;i<m;i++) wss[i]=0;
for(i=0;i<n;i++) wss[x[i]=r[i]]++;
for(i=1;i<m;i++) wss[i]+=wss[i-1];
for(i=n-1;i>=0;i--) sa[--wss[x[i]]]=i;
for(j=1,p=1;p<n;j*=2,m=p)
{
for(p=0,i=n-j;i<n;i++) y[p++]=i;
for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;
for(i=0;i<n;i++) wv[i]=x[y[i]];
for(i=0;i<m;i++) wss[i]=0;
for(i=0;i<n;i++) wss[wv[i]]++;
for(i=1;i<m;i++) wss[i]+=wss[i-1];
for(i=n-1;i>=0;i--) sa[--wss[wv[i]]]=y[i];
for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++)
x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
}
return;
}
void getHeight(){
int i,j,k=0;
for(i=1;i<=n;i++) rank[sa[i]]=i;
for(i=0;i<n;height[rank[i++]]=k)
for(k?k--:0,j=sa[rank[i]-1];r[i+k]==r[j+k];k++);
return;
}
int main()
{
cin>>n;getchar();
for(int i=0;i<n;i++)
scanf("%c",&r[i]);
r[n]='a'-1;
for(int i=0;i<=n;i++)
printf(" %d",r[i]);
puts("");
da(r,sa,n+1,'z'+1);
cout<<"sa[i] means the ranked i_th substring starts from position of sa[i] "<<endl;
for(int i=1;i<=n;i++)
printf("i=%d_%d%c",i,sa[i],i==n?'\n':' ');puts("");
getHeight();
cout<<"rank[i] means the substring starts from position i ranked rank[i]_th "<<endl;
for(int i=0;i<n;i++)
printf("i=%d_%d%c",i,rank[i],i==n-1?'\n':' '); puts("");
cout<<"height[i] means LCA of(sa[i] , sa[i-1])"<<endl;
for(int i=2;i<=n;i++){
printf("the %dth & %dth\n",i-1,i);
for(int j=sa[i-1]; j <n ;j++)
printf("%c",r[j]);puts("");
for(int j=sa[i]; j <n ;j++)
printf("%c",r[j]);puts("");
printf(" heihgt[%d] ---- %d\n",i,height[i]);
}
return 0;
}