constint N =1;int SA[N], RK[N], n, m;int pre[N], cnt[N];char str[N];voidBuildSA_RK(){
m =122;for(int i =1; i <= n;++ i) RK[i]= str[i],++ cnt[RK[i]];for(int i =2; i <= m;++ i) cnt[i]+= cnt[i-1];for(int i = n; i >=1;-- i) SA[cnt[RK[i]]--]= i;for(int k =1; k <= n; k <<=1){int tot =0;for(int i = n - k +1; i <= n;++ i) pre[++tot]= i;for(int i =1; i <= n;++ i)if(SA[i]> k) pre[++tot]= SA[i]- k;memset(cnt,0,sizeof(cnt));for(int i =1; i <= n;++ i)++ cnt[RK[i]];for(int i =2; i <= m;++ i) cnt[i]+= cnt[i-1];for(int i = n; i >=1;-- i) SA[cnt[RK[pre[i]]]--]= pre[i], pre[i]=0;swap(RK, pre);
tot =1, RK[SA[1]]=1;for(int i =2; i <= n;++ i)if(pre[SA[i]]== pre[SA[i-1]]&& pre[SA[i]+k]== pre[SA[i-1]+k])
RK[SA[i]]= tot;else RK[SA[i]]=++ tot;if(tot == n)return;
m = tot;}}