Visit Counter
AmazingCounters.com

0、感谢阳哥
1、题目大意:后缀数组模板题
2、分析:汝佳的书上的代码的有bug,还有那个n是字符串长度+1,‘’也要加入排序的
3、代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
char s[101000];
int sa[101000], t[101000], t2[101000], c[101000];
int Rank[101000], height[101000];
inline void build_sa(int m){
    int *x = t, *y = t2;
    int n = strlen(s) + 1;
    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 ++){
            if(y[sa[i - 1]] == y[sa[i]] && y[sa[i - 1] + k] == y[sa[i] + k]) x[sa[i]] = p - 1;
            else x[sa[i]] = p ++;
        }
        if(p >= n) break;
        m = p;
    }
    for(int i = 1; i < n; i ++) printf("%d ", sa[i] + 1);
    printf("\n");
    return;
}
inline void getHeight(){
    int k = 0;
    int n = strlen(s) + 1;
    for(int i = 0; i < n; i ++) Rank[sa[i]] = i;
    for(int i = 0; i < n; i ++){
        if(k) k --;
        if(!Rank[i]) continue;
        int j = sa[Rank[i] - 1];
        while(s[i + k] == s[j + k] && i + k < n && j + k < n) k ++;
        height[Rank[i]] = k;
    }
    for(int i = 2; i < n; i ++) printf("%d ", height[i]);
    printf("\n");
}
int main(){
    scanf("%s", s);
    build_sa(300);
    getHeight();
    return 0;
}

4、分类:后缀数组