BZOJ 2882 & 后缀数组的傻逼实现
题意:
一个字符环,求一个开头使字典序最小.
SOL:
后缀数组打起来...然后居然卡过...10sec的实现我10936ms...居然卡过???
rank倒三...啦啦啦啦啦....
改个离散化会不会快点?....
Code:
/*========================================================================== # Last modified: 2016-03-19 14:38 # Filename: 2882.cpp # Description: ==========================================================================*/ #define me AcrossTheSky #include <cstdio> #include <cmath> #include <ctime> #include <string> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> #include <set> #include <map> #include <stack> #include <queue> #include <vector> #define lowbit(x) (x)&(-x) #define FOR(i,a,b) for((i)=(a);(i)<=(b);(i)++) #define FORP(i,a,b) for(int i=(a);i<=(b);i++) #define FORM(i,a,b) for(int i=(a);i>=(b);i--) #define ls(a,b) (((a)+(b)) << 1) #define rs(a,b) (((a)+(b)) >> 1) #define getlc(a) ch[(a)][0] #define getrc(a) ch[(a)][1] #define maxn 700000 #define maxm 100000 #define pi 3.1415926535898 #define _e 2.718281828459 #define INF 1070000000 using namespace std; typedef long long ll; typedef unsigned long long ull; template<class T> inline void read(T& num) { bool start=false,neg=false; char c; num=0; while((c=getchar())!=EOF) { if(c=='-') start=neg=true; else if(c>='0' && c<='9') { start=true; num=num*10+c-'0'; } else if(start) break; } if(neg) num=-num; } /*==================split line==================*/ int s[maxn]; int t[maxn],t2[maxn],c[maxn],sa[maxn]; int n; void build_sa(int m){ int *x=t,*y=t2; FORP(i,0,m) c[i]=0; FORP(i,0,n-1) c[x[i]=s[i]]++; FORP(i,1,m) c[i]+=c[i-1]; FORM(i,n-1,0) sa[--c[x[i]]]=i; for (int k=1;k<=n;k <<= 1){ int p=0; FORP(i,n-k,n-1) y[p++]=i; FORP(i,0,n-1) if (sa[i]>=k) y[p++]=sa[i]-k; FORP(i,0,m) c[i]=0; FORP(i,0,n-1) c[x[y[i]]]++; FORP(i,1,m) c[i]+=c[i-1]; FORM(i,n-1,0) sa[--c[x[y[i]]]]=y[i]; swap(x,y); p=1; x[sa[0]]=0; FORP(i,1,n-1) if (y[sa[i-1]]==y[sa[i]] && y[sa[i-1]+k]==y[sa[i]+k]) x[sa[i]]=p-1; else x[sa[i]]=p++; if (p>=n) break; m=p; } } int main(){ read(n); int tmp=n; int m=0; FORP(i,0,n-1) { read(s[i]); m=max(m,s[i]); s[n+i]=s[i];} s[n+n]=m+1; n=n+n+1; build_sa(m+2); for (int i=0;i<tmp;i++) printf("%d%c",s[(sa[0])%tmp+i],i==tmp-1?'\n':' '); }
Sometimes it s the very people who no one imagines anything of. who do the things that no one can imagine.