后缀数组suffix模板

25min默完,漏掉了一些p=n时的小优化…还有m=p的常数优化
好在主体没什么问题,看来还是不够熟悉

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 100005;
int sa[maxn], Rank[maxn], height[maxn];
int wa[maxn], wb[maxn], wv[maxn], cnt[maxn]; 
void DA(int *r, int n, int m)
{
    int *x = wa, *y = wb, p;
    for(int i = 0; i < m; i++) cnt[i] = 0;
    for(int i = 0; i < n; i++) cnt[ x[i] = r[i] ]++;
    for(int i = 1; i < m; i++) cnt[i] += cnt[i-1];
    for(int i = n-1; i >= 0; i--) sa[--cnt[x[i]]] = i;
    for(int j = 1; j < n; j<<=1)
    {
        p = 0;
        for(int i = n-j; i < n; i++) y[p++] = i;
        for(int i = 0; i < n; i++) if( sa[i] >= j ) y[p++] = sa[i] - j;
        for(int i = 0; i < n; i++) wv[i] = x[y[i]];
        for(int i = 0; i < m; i++) cnt[i] = 0;
        for(int i = 0; i < n; i++) cnt[wv[i]]++;
        for(int i = 1; i < m; i++) cnt[i] += cnt[i-1];
        for(int i = n-1; i >= 0; i--) sa[--cnt[wv[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]] == y[sa[i-1]]) && (y[sa[i]+j] == y[sa[i-1]+j]) ? p-1 : p++;
        if(p >= n) break;
        m = p;
    }
}
void calheight(int *r, int n)
{
    for(int i = 1; i <= n; i++) Rank[sa[i]] = i;
    int j = 0, k = 0;
    for(int i = 0; i < n; height[Rank[i]] = k, i++)
        for(k ? k-- : k=0, j = sa[Rank[i]-1]; r[i+k] == r[j+k]; k++);
}
int main(void)
{
    return 0;
}

你可能感兴趣的:(后缀数组suffix模板)