BZOJ 1031: [JSOI2007]字元加密Cipher

來源:互聯網
上載者:User

標籤:cipher   putc   using   char s   ons   res   amp   ***   memory   

Description

一個環,求字典序排列,\(n\leqslant 10^5\)

Solution

尾碼數組。

把串複製一遍在後面,排序就行了...

Code
/**************************************************************    Problem: 1031    User: BeiYu    Language: C++    Result: Accepted    Time:712 ms    Memory:6968 kb****************************************************************/ #include <bits/stdc++.h>using namespace std; const int N = 200500; int l,n,m=256;char str[N];int a[N]; namespace SA {    int t1[N],t2[N],c[N],sa[N],rk[N],ht[N];         void get_sa(int a[],int n=::n,int m=::m) {        int *x=t1,*y=t2;        for(int i=1;i<=m;i++) c[i]=0;        for(int i=1;i<=n;i++) c[x[i]=a[i]]++;        for(int i=1;i<=m;i++) c[i]+=c[i-1];        for(int i=n;i;--i) sa[c[x[i]]--]=i;        for(int k=1,p=0;k<n;k<<=1,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=1;i<=m;i++) c[i]+=c[i-1];            for(int i=n;i;--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(p>=n) break;m=p;        }    }    void get_ht(int a[],int n=::n) {        for(int i=1;i<=n;i++) rk[sa[i]]=i;        for(int i=1,j,k=0;i<=n;ht[rk[i++]]=k)             for(j=sa[rk[i]-1],k=k?k-1:k;a[i+k]==a[j+k];k++);    }}; int main() {    scanf("%s",str+1);    l=n=strlen(str+1);    for(int i=1;i<=n;i++) a[i]=str[i]+1;    for(int i=1;i<=l;i++) a[++n]=a[i];    SA::get_sa(a,n,m);    SA::get_ht(a,n);//  for(int i=1;i<=n;i++) cout<<SA::sa[i]<<" ";cout<<endl;//  for(int i=1;i<=n;i++) cout<<SA::ht[i]<<" ";cout<<endl;    for(int i=1;i<=n;i++)         if(SA::sa[i]<=l) putchar(a[SA::sa[i]+l-1]-1);    return 0;}

  

BZOJ 1031: [JSOI2007]字元加密Cipher

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.