You’re half a world away
But in my mind I whisper every single word you say
The Suffix Array (SA) is used to address the following problem:
You are given a string of length (0-indexed); sort all of the suffixes of , in lexicographically increasing order.
Output the answer as a permutation , where indicates that the suffix is the -th smallest among all suffixes.
Brute Force
Extract all suffixes from and simply sort them. This takes time and space.
Optimization 1
We do not need the full information from the entire strings of all original suffixes.
For convenience, we are going to sort all cyclic shifts instead of the suffixes of , after appending a character with an ASCII value smaller than every character in the alphabet (e.g., .). It can be shown that the solution for our new problem is identical to the original one.
In the -th iteration, our goal is to sort all suffixes, but ignoring all characters except the first ones. For , we can sort them directly. For , we use the idea of binary lifting, utilizing the information from the -th iteration. Iterate from to ; In this process, we have to maintain two arrays:
- : The answer after iterations.
- : The ID of equivalence lass distinguished by the first characters of each suffix.
For example, when , at the beginning of iteration (that is, sorted by the first character of the suffixes):
- ;
- ;
- ;
- ;
- ;
- ;
To sort by the order of the characters in is essentially the same as: sorting by the ordered pair ; that is, sorting by , where is an element of . corresponds to the rank of the characters in of after cyclic shifts (which is why we want to sort the cyclic shifts rather than suffixes).
Since we have iterations and need to sort the whole thing in time in each iteration, the total time complexity is , and it only costs space.
Optimization 1.1
The range of is . Use counting sort twice: first sort by , then sort by . It works since counting sort is stable. This reduces the time complexity to .
Optimization
There is a solution in linear time.
Implementation
A minor constant optimization: Notice that we don’t need to actually do the first sort in every iteration; just apply to the entire array .
int n;
inline int f(int x,int i){
x+=(1<<(i-1));
if(x>=n) x-=n;
return x;
}
void solve(){
string s;
cin>>s,n = s.length(),s+='$',n++;
vector<int> p(n),c(n);
map<char,int> mp;
for(char c:s) mp[c]++;
for(auto it=++mp.begin();it!=mp.end();it++) it->snd+=prev(it)->snd;
for(int i=n-1;i>=0;i--) p[--mp[s[i]]] = i;
int id = 0;
for(int l=0,r=0;l<n;l=r,id++){
while(r<n && s[p[l]]==s[p[r]]) c[p[r++]] = id;
}
id++;
for(int i=1;(1<<(i-1))<=n;i++){
for(int &j:p){
j-=1<<(i-1);
if(j<0) j+=n;
}
vector<int> tp = p,cnt(id);
for(int j:c) cnt[j]++;
for(int j=1;j<id;j++) cnt[j]+=cnt[j-1];
for(int j=n-1;j>=0;j--) p[--cnt[c[tp[j]]]] = tp[j];
id = 0;
auto tc = c;
tc[p[0]] = 0;
for(int j=1;j<n;j++){
if(tc[p[j-1]]==tc[p[j]] && tc[f(p[j-1],i)]==tc[f(p[j],i)]) c[p[j]] = id;
else c[p[j]] = ++id;
}
id++;
}
p.erase(p.begin());
for(int i:p) cout<<i+1<<" ";
cout<<"\n";
}Author:
Misaka16172GenAI usage: only for grammar check.