Origin SuffixArray

不成熟的后缀数组模板:P,还缺ST表、前缀和什么的。
开始试着敲hzwer学长的模板,然而mul函数调用非常慢,滚动也略不方便。
最后还是用了标准的模板,不会拖后腿的模板感觉还好。

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define inf (1<<30)
#define INF (1ll<<62)
#define fi first
#define se second
#define rep(x,s,t) for(register int x=s,t_=t;x
#define per(x,s,t) for(register int x=t-1,s_=s;x>=s_;--x)
#define prt(x) cout<<#x<<":"<
#define prtn(x) cout<<#x<<":"<
#define pb(x) push_back(x)
#define hash asfmaljkg
#define rank asfjhgskjf
#define y1 asggnja
#define y2 slfvm
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
template<class T>void sc(T &x){
    int f=1;char c;x=0;
    while(c=getchar(),c<48)if(c=='-')f=-1;
    do x=x*10+(c^48);
    while(c=getchar(),c>47);
    x*=f;
}
template<class T>void nt(T x){
    if(!x)return;
    nt(x/10);
    putchar(x%10+'0');
}
template<class T>void pt(T x){
    if(x<0)putchar('-'),x=-x;
    if(!x)putchar('0');
    else nt(x);
}
template<class T>void ptn(T x){
    pt(x);putchar('\n');
}
template<class T>void pts(T x){
    pt(x);putchar(' ');
}
template<class T>inline void Max(T &x,T y){if(xtemplate<class T>inline void Min(T &x,T y){if(x>y)x=y;}

int n;
const int maxs=200006;
struct Suffix{
    static const int maxs=200006;
    static const int maxc=30;
    int n;
    int sa[maxs],rk[maxs];//adc.
    int h[maxs],s[maxs];//sup.
    int v[maxs];//sort.
    void sapoly(){
        int *a=rk,*b=h;
        rep(i,1,maxc)v[i]=0;
        rep(i,1,n+1)v[s[i]]++;
        rep(i,1,maxc)v[i]+=v[i-1];
        rep(i,1,n+1)sa[v[s[i]]--]=i;
        rep(i,1,n+1)a[sa[i]]=a[sa[i-1]]+(s[sa[i-1]]!=s[sa[i]]);
        a[n+1]=b[n+1]=0;
        for(int p,k=1;k1){
            p=0;
            rep(i,1,n+1)v[a[sa[i]]]=i;
            rep(i,n-k+1,n+1)b[++p]=i;
            rep(i,1,n+1)if(sa[i]>k)b[++p]=sa[i]-k;
            per(i,1,n+1)sa[v[a[b[i]]]--]=b[i];
            p=0;
            rep(i,1,n+1)b[sa[i]]=(a[sa[i-1]]!=a[sa[i]]||a[sa[i-1]+k]!=a[sa[i]+k])?++p:p;
            swap(a,b);
            if(p==n)break;
        }
        if(a!=rk)memcpy(rk,a,n+1<<2);
    }
    void hipoly(){
        int k=0;
        rep(i,1,n+1){
            for(int j=sa[rk[i]-1];s[j+k]==s[i+k];++k);
            h[rk[i]]=k;if(k)--k;
        }
    }

    void init(char *str,int nn){
        n=nn;
        rep(i,1,n+1)s[i]=str[i]-'a'+1;
        sapoly();
        hipoly();
//      rep(i,1,n+1)mn[0][i]=h[i];
//      rep(i,1,stp[n]+1)
//          rep(j,1,n-(1<
//              mn[i][j]=min(mn[i-1][j],mn[i-1][j+(1<
    }
    void print(){
        rep(i,1,n)pts(sa[i]);ptn(sa[n]);
        rep(i,2,n)pts(h[i]);if(n>1)ptn(h[n]);
    }
}SA;
char str[maxs];

int main(){
//  freopen("pro.in","r",stdin);
//  freopen("chk.out","w",stdout);
    scanf("%s",str+1);
    n=strlen(str+1);
    SA.init(str,n);
    SA.print();
    return 0;
}

你可能感兴趣的:(心情)