后缀数组模板 SuffixArray
1 #include<bits/stdc++.h> 2 #define rep(i,a,b) for(i=a;i<b;++i) 3 #define drep(i,a,b) for(i=b;i>=a;--i) 4 using namespace std; 5 int p[N],wc[N],wv[N],wa[N],wb[N],rank[N],sa[N]; 6 char s[N]; 7 inline bool cmp(int *r,int a,int b,int l) { 8 return r[a]==r[b]&&r[a+l]==r[b+l]; 9 } 10 inline void tate(int *x,int *y,char *r,int n,int m,bool opt) { 11 int i; 12 rep(i,0,m) wc[i]=0; 13 rep(i,0,n) wc[opt?(x[i]=r[i]):wv[i]]++; 14 rep(i,1,m) wc[i]+=wc[i-1]; 15 drep(i,n-1,0) sa[--wc[opt?x[i]:wv[i]]]=opt?i:y[i]; 16 } 17 void f(char *r,int *sa,int n,int m) { 18 int i,j,p,*x=wa,*y=wb,*t; 19 tate(x,y,r,n,m,1); 20 for (j=1,p=1;p<n;j<<=1,m=p) { 21 p=0; 22 rep(i,n-j,n) y[p++]=i; 23 rep(i,0,n) if(sa[i]>=j) y[p++]=sa[i]-j; 24 rep(i,0,n) wv[i]=x[y[i]]; 25 tate(x,y,r,n,m,0); 26 for (t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;++i) 27 x[sa[i]]=cmp(r,sa[i-1],sa[i],j)?p-1:p++; 28 } 29 }
压了下,把基数排序的压进函数,然后看起来还挺好写的,这只是后缀数组的一个阶段啦, 继续努力!