suffix_array(后缀数组) 学习笔记

没学会正常做法
写了一个链表实现的 双关键字 统计排序,常数差不多大

#include 
#define N 200100
using namespace std;

int n, SA[N], rnk[N], height[N];
char S[N];

inline void get_SA() {
    static int x[N], y[N], t[N], head[N], nxt[N], p = 26;
    for (int k=1;k<=n;k<<=1) {
        for (int i=1;i<=n;i++) x[i] = rnk[i], y[i] = (i+k > n) ? 0 : rnk[i+k];
        for (int i=0;i<=p;i++) t[i] = head[i] = 0;
        for (int i=1;i<=n;i++) nxt[i] = 0;
        for (int i=1;i<=n;i++) {++t[x[i]]; nxt[i] = head[y[i]];  head[y[i]] = i;}
        for (int i=1;i<=p;i++) t[i] += t[i-1];
        for (int i=p;i>=0;i--) for (int j=head[i];j;j=nxt[j]) SA[t[x[j]]--] = j;
        p = 0;
        for (int i=1;i<=n;i++) rnk[SA[i]] = (x[SA[i]] == x[SA[i-1]] && y[SA[i]] == y[SA[i-1]]) ? p : ++p;
        if (p >= n) break;
    }
    int k = 0;
    for (int i=1;i<=n;i++) {
        int j = SA[rnk[i]-1];
        for (k?--k:0;S[i+k] == S[j+k];k++);
        height[rnk[i]] = k;
    }
}

int main() { freopen("_in.txt","r",stdin);
    scanf("%s",S+1);
    n = strlen(S+1);
    for (int i=1;i<=n;i++) rnk[i] = S[i] - 'a' + 1;
    get_SA();
    for (int i=1;i<=n;i++) printf("%d | %s\n",height[i],S+SA[i]);
}

你可能感兴趣的:(解题报告)