后缀数组
当年感觉好神的题现在好像变水了……
题意其实有点蛋疼……一开始没看懂<_<
将原串复制一遍接在后面,用后缀数组求一下SA,那么SA<n的就是所找到的那n个字符串,然后把它们的第n个字符抠出来就可以了……
1 /************************************************************** 2 Problem: 1031 3 User: Tunix 4 Language: C++ 5 Result: Accepted 6 Time:752 ms 7 Memory:5568 kb 8 ****************************************************************/ 9 10 //BZOJ 1031 11 #include<vector> 12 #include<cstdio> 13 #include<cstring> 14 #include<cstdlib> 15 #include<iostream> 16 #include<algorithm> 17 #define rep(i,n) for(int i=0;i<n;++i) 18 #define F(i,j,n) for(int i=j;i<=n;++i) 19 #define D(i,j,n) for(int i=j;i>=n;--i) 20 #define pb push_back 21 using namespace std; 22 inline int getint(){ 23 int v=0,sign=1; char ch=getchar(); 24 while(ch<'0'||ch>'9'){ if (ch=='-') sign=-1; ch=getchar();} 25 while(ch>='0'&&ch<='9'){ v=v*10+ch-'0'; ch=getchar();} 26 return v*sign; 27 } 28 const int N=2e5+10,INF=~0u>>2; 29 typedef long long LL; 30 /******************tamplate*********************/ 31 int n; 32 inline bool cmp(int *r,int a,int b,int l){ 33 return r[a]==r[b] && r[a+l]==r[b+l]; 34 } 35 int wa[N],wb[N],c[N],sa[N],rank[N]; 36 void DA(char *s,int *sa,int n,int m){ 37 int i,j,p,*x=wa,*y=wb; 38 rep(i,m) c[i]=0; 39 rep(i,n) c[x[i]=s[i]]++; 40 F(i,1,m-1) c[i]+=c[i-1]; 41 D(i,n-1,0) sa[--c[x[i]]]=i; 42 for(j=1,p=1;p<n;j<<=1,m=p){ 43 for(p=0,i=n-j;i<n;i++) y[p++]=i; 44 rep(i,n) if (sa[i]>=j) y[p++]=sa[i]-j; 45 46 rep(i,m) c[i]=0; 47 rep(i,n) c[x[y[i]]]++; 48 F(i,1,m-1) c[i]+=c[i-1]; 49 D(i,n-1,0) sa[--c[x[y[i]]]]=y[i]; 50 swap(x,y); x[sa[0]]=0; p=1; 51 F(i,1,n-1) x[sa[i]]=cmp(y,sa[i-1],sa[i],j) ? p-1 : p++; 52 } 53 } 54 char s[N],ans[N]; 55 int main(){ 56 #ifndef ONLINE_JUDGE 57 freopen("1031.in","r",stdin); 58 freopen("1031.out","w",stdout); 59 #endif 60 scanf("%s",s); n=strlen(s); 61 rep(i,n) s[n+i]=s[i]; s[n+n]='