BZOJ 1031: [JSOI2007]字符加密Cipher (后缀数组)

求循环排列的排名,直接把原串复制一遍接在后面做后缀数组就行了.

CODE

#include<bits/stdc++.h>
using namespace std;
char cb[1<<15],*cs=cb,*ct=cb;
#define getc() (cs==ct&&(ct=(cs=cb)+fread(cb,1,1<<15,stdin),cs==ct)?0:*cs++)
template<class T>inline void read(T &res) {
	char ch; int flg = 1; while(!isdigit(ch=getchar()))if(ch=='-')flg=-flg;
	for(res=ch-'0';isdigit(ch=getchar());res=res*10+ch-'0'); res*=flg;
}
const int MAXN = 200005;
char s[MAXN];
int x[MAXN], y[MAXN], c[MAXN], rk[MAXN], sa[MAXN];
inline void Get_Sa(int n, int m) {
    for(int i = 1; i <= m; ++i) c[i] = 0;
    for(int i = 1; i <= n; ++i) ++c[x[i]=s[i]];
    for(int i = 2; i <= m; ++i) c[i] += c[i-1];
    for(int i = n; i >= 1; --i) sa[c[x[i]]--] = i;
    for(int k = 1; k <= n; k<<=1) {
        int p = 0;
        for(int i = n-k+1; i <= n; ++i) y[++p] = i;
        for(int i = 1; i <= n; ++i) if(sa[i]>k) y[++p] = sa[i]-k;
        for(int i = 1; i <= m; ++i) c[i] = 0;
        for(int i = 1; i <= n; ++i) ++c[x[i]];
        for(int i = 2; i <= m; ++i) c[i] += c[i-1];
        for(int i = n; i >= 1; --i) sa[c[x[y[i]]]--] = y[i];
        swap(x, y);
        x[sa[1]] = p = 1;
        for(int i = 2; i <= n; ++i)
            x[sa[i]] = (y[sa[i]] == y[sa[i-1]] && y[sa[i]+k] == y[sa[i-1]+k]) ? p : ++p;
        if((m=p) == n) break;
    }
    for(int i = 1; i <= n; ++i) rk[sa[i]] = i;
}
int n;
int main() {
	scanf("%s", s+1); n = strlen(s+1);
	for(int i = 1; i <= n; ++i) s[n+i] = s[i];
	Get_Sa(n<<1, 126);
	for(int i = 1; i <= n<<1; ++i) if(sa[i] <= n)
        putchar(s[sa[i]>1?sa[i]-1:n]);
}


posted @ 2019-12-14 14:51  _Ark  阅读(95)  评论(0编辑  收藏  举报