3
20
2016
0

[BZOJ1031] [JSOI2007]字符加密Cipher

老年颓废选手表示不能再颓了,毕竟2天没做题了虽然还是学习了一下后缀数组

第一道后缀数组!

裸题,因为后缀本来就排好序了,然后直接输出就行了

需要把串复制一遍

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;

char s[200005];
int log2[200005],rmq[200005][20],n,sa[200005],rank[200005],rank2[200005],ws[200005],wv[200005],tot;

int cmp(int *r,int a,int b,int len){return (r[a]==r[b])&(r[a+len]==r[b+len]);}

void GetSA(char *r,int *sa,int n,int m){
int i,j,p,*x=rank,*y=rank2,*t;
for(i=0;i<m;i++)ws[i]=0;
for(i=0;i<n;i++)ws[x[i]=r[i]]++;
for(i=1;i<m;i++)ws[i]+=ws[i-1];
for(i=n-1;i>=0;i--)sa[--ws[x[i]]]=i;
for(j=p=1;p<n;j*=2,m=p){
	for(p=0,i=n-j;i<n;i++)y[p++]=i;
	for(i=0;i<n;i++)if(sa[i]>=j)y[p++]=sa[i]-j;
	for(i=0;i<m;i++)ws[i]=0;
	for(i=0;i<n;i++)ws[wv[i]=x[y[i]]]++;
	for(i=1;i<m;i++)ws[i]+=ws[i-1];
	for(i=n-1;i>=0;i--)sa[--ws[wv[i]]]=y[i];
	for(t=x,x=y,y=t,x[sa[0]]=0,i=p=1;i<n;i++)x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;
}
}

int main(){
freopen("1031.in","r",stdin);
freopen("1031.out","w",stdout);
scanf("%s",s);
n=strlen(s);
for(int i=n;i<2*n;i++)s[i]=s[i-n];
n*=2;
s[n]=0;
GetSA(s,sa,n+1,128);
for(int i=1;i<=n && tot<n/2;i++)if(sa[i]<n/2)printf("%c",s[sa[i]+n/2-1]),tot++;
return 0;
}
Category: BZOJ | Tags: OI bzoj | Read Count: 313

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com