复制一下原串处理环,然后直接出SA就可以了
1 #include<bits/stdc++.h> 2 #define inc(i,l,r) for(int i=l;i<=r;i++) 3 #define dec(i,l,r) for(int i=l;i>=r;i--) 4 #define link(x) for(edge *j=h[x];j;j=j->next) 5 #define mem(a) memset(a,0,sizeof(a)) 6 #define inf 1e9 7 #define ll long long 8 #define succ(x) (1<<x) 9 #define NM 200000+5 10 using namespace std; 11 int read(){ 12 int x=0,f=1;char ch=getchar(); 13 while(!isdigit(ch)){if(ch==‘-‘)f=-1;ch=getchar();} 14 while(isdigit(ch))x=x*10+ch-‘0‘,ch=getchar(); 15 return x*f; 16 } 17 char st[NM]; 18 int top[NM],rank[NM],sa[NM],tmp[NM],n,t,m; 19 void getsa(){ 20 int j;m=256; 21 inc(i,0,n)top[rank[i]=(int)st[i]]++; 22 inc(i,1,m)top[i]+=top[i-1]; 23 inc(i,0,n)sa[--top[rank[i]]]=i; 24 // inc(i,0,n)printf("%d ",sa[i]);printf("\n"); 25 for(int k=1;k<=n;k<<=1){ 26 inc(i,0,n){ 27 j=sa[i]-k; 28 if(j<0)j+=n+1; 29 tmp[top[rank[j]]++]=j; 30 } 31 sa[tmp[top[0]=0]]=j=0; 32 // inc(i,0,n)printf("%d ",tmp[i]);printf("\n"); 33 inc(i,0,n){ 34 if(rank[tmp[i]]!=rank[tmp[i-1]]||rank[tmp[i]+k]!=rank[tmp[i-1]+k]) 35 top[++j]=i; 36 sa[tmp[i]]=j; 37 } 38 memcpy(rank,sa,sizeof(sa)); 39 memcpy(sa,tmp,sizeof(tmp)); 40 if(j>=n)break; 41 } 42 } 43 int main(){ 44 freopen("data.in","r",stdin); 45 scanf("%s",st); 46 t=strlen(st); 47 inc(i,0,t-1)st[i+t]=st[i]; 48 n=strlen(st)-1; 49 getsa(); 50 inc(i,0,n) 51 if(sa[i]<t) 52 printf("%c",st[sa[i]+t-1]); 53 printf("\n"); 54 return 0; 55 }
时间: 2024-10-22 09:24:48