题意:给出一个字符串,求出所有全排列后,按字典序从小到大排序,每个串的最后一位组成的字符串
题解:后缀数组搞一搞,将他复制一遍,跑一个sa,那么对于长度n以后的字符串在sa中可以不管,算是个模板题吧
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 1e5 + 5;
typedef long long LL;
char s[maxn*2];
int sa[maxn*2];
int t[maxn*2];
int t2[maxn*2];
int c[maxn*2];
void build_sa(int n, int m){
int *x = t, *y = t2;
for(int i=0; i<m; i++) c[i] = 0;
for(int i=0; i<n; i++) c[x[i] = s[i]]++;
for(int i=1; i<m; i++) c[i] += c[i-1];
for(int i=n-1; i>=0; i--) sa[--c[x[i]]] = i;
for(int k=1; k<=n; k<<=1){
int p = 0;
for(int i=n-k; i<n; i++) y[p++] = i;
for(int i=0; i<n; i++) if(sa[i] >= k) y[p++] = sa[i] - k;
for(int i=0; i<m; i++) c[i] = 0;
for(int i=0; i<n; i++) c[x[y[i]]]++;
for(int i=0; i<m; i++) c[i] += c[i-1];
for(int i=n-1; i>=0; i--) sa[--c[x[y[i]]]] = y[i];
swap(x,y);
p = 1; x[sa[0]] = 0;
for(int i=1; i<n; i++)
x[sa[i]] = y[sa[i-1]] == y[sa[i]] && y[sa[i-1]+k] == y[sa[i]+k] ? p-1 : p++;
if(p >= n) break;
m = p;
}
}
int main(){
scanf("%s",s);
int len = (int)strlen(s);
for(int i=0; i<len; i++)
s[i+len] = s[i];
build_sa(len*2,130);
for(int i=0; i<len*2; i++){
if(sa[i] < len)
printf("%c",s[sa[i]-1+len]);
}
printf("\n");
}