bzoj1031[JSOI2007]字元加密

來源:互聯網
上載者:User

標籤:

bzoj1031[JSOI2007]字元加密

題意:

一種加密辦法是把需要加密的資訊排成一圈,顯然,它們有很多種不同的讀法。把它們按照字串的大小排序,讀出最後一列字元,就是加密後的字串。給出原字串,求加密後的字串。

題解:

將原字串重複後接在後面,然後求尾碼數組,注意求完後要取那些長度大於原字串長度a2的尾碼的第a2個字元。

代碼:

 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define inc(i,j,k) for(int i=j;i<=k;i++) 5 #define dec(i,j,k) for(int i=j;i>=k;i--) 6 using namespace std; 7  8 char s[300000],s2[300000];int sa[300000],x[300000],y[300000],c[300000]; 9 void make(char *str){10     int len=strlen(str),m=0;memset(c,0,sizeof(c));inc(i,0,len-1)c[x[i]=str[i]]++,m=max(m,(int)str[i]);11     inc(i,1,m)c[i]+=c[i-1]; dec(i,len-1,0)sa[--c[x[i]]]=i; m++;12     for(int i=1;i<=len;i<<=1){13         int p=0; inc(j,len-i,len-1)y[p++]=j;14         inc(j,0,len-1)if(sa[j]>=i)y[p++]=sa[j]-i;15         memset(c,0,sizeof(c)); inc(j,0,len-1)c[x[y[j]]]++;16         inc(j,1,m-1)c[j]+=c[j-1]; 17         dec(j,len-1,0)sa[--c[x[y[j]]]]=y[j]; 18         swap(x,y);19         p=1; x[sa[0]]=0;20         inc(j,1,len-1)x[sa[j]]=y[sa[j-1]]==y[sa[j]]&&y[sa[j-1]+i]==y[sa[j]+i]?p-1:p++;21         if(p>=len)break; m=p;22     }23 }24 int main(){25     scanf("%s",s); int a1=strlen(s); inc(i,0,a1-1)s[a1+i]=s[i]; s[2*a1]=‘\0‘;26     make(s); int a2=0; inc(i,0,a1*2-1)if(sa[i]<a1)s2[a2++]=s[sa[i]+a1-1]; s2[a2]=‘\0‘;27     printf("%s",s2);28 }

 

20160322

bzoj1031[JSOI2007]字元加密

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.