Suffix Array

· · 算法·理论

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 s of length n (0-indexed); sort all of the n suffixes of s, in lexicographically increasing order.

Output the answer as a permutation p, where p_x=i indicates that the suffix s[i\cdots (n-1)] is the (x+1)-th smallest among all suffixes.

Brute Force

Extract all suffixes from s and simply sort them. This takes \mathcal{O}(n^2\log n) time and \mathcal{O}(n^2) space.

Optimization 1

We do not need the full information from the entire strings of all n original suffixes.

For convenience, we are going to sort all cyclic shifts instead of the suffixes of s, 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 i-th iteration, our goal is to sort all suffixes, but ignoring all characters except the first 2^i ones. For i=0, we can sort them directly. For i>0, we use the idea of binary lifting, utilizing the information from the (i-1)-th iteration. Iterate from 1 to \lceil \log n \rceil; In this process, we have to maintain two arrays:

For example, when s=\text{ababa.}, at the beginning of iteration i=1 (that is, sorted by the first 2^0=1 character of the suffixes):

To sort by the order of the characters in [0,2^i) is essentially the same as: sorting by the ordered pair ([0,2^{i-1}),[2^{i-1},2^i)); that is, sorting p by (c_x,c_{(x+2^{i-1})\mod n}), where x is an element of p. c_{(x+2^{i-1})\mod n} corresponds to the rank of the characters in [2^{i-1},2^i) of s after x cyclic shifts (which is why we want to sort the cyclic shifts rather than suffixes).

Since we have \mathcal{O}(\log n) iterations and need to sort the whole thing in \mathcal{O}(n\log n) time in each iteration, the total time complexity is \mathcal{O}(n\log^2n), and it only costs \mathcal{O}(n) space.

Optimization 1.1

The range of c is \mathcal{O}(n). Use counting sort twice: first sort by c_{(x+2^{i-1})\mod n}, then sort by c_x. It works since counting sort is stable. This reduces the time complexity to \mathcal{O}(n\log n).

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 p_j\leftarrow (p_j-2^{i-1})\mod n to the entire array p.

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: Misaka16172

GenAI usage: only for grammar check.