• 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]);
    }
    
    
    
  • 相关阅读:
    HDU 5119 Happy Matt Friends(递推)
    爬虫系列之requests
    基于SVM的python简单实现验证码识别
    python3.6配置libsvm2.2
    KNN——图像分类
    python写web服务器
    牛客练习赛9 珂朵莉的值域连续段
    【转】3次重传的机制
    【转】三个重复的ACK意味着发生拥塞?
    【转】TCP报文格式定义详解
  • 原文地址:https://www.cnblogs.com/Orz-IE/p/12039315.html
Copyright © 2020-2023  润新知