# bzoj1692 [Usaco2007 Dec]队列变换

 1 #include<algorithm>
2 #include<iostream>
3 #include<cstdlib>
4 #include<cstring>
5 #include<cstdio>
6 #include<string>
7 #include<cmath>
8 #include<ctime>
9 #include<queue>
10 #include<stack>
11 #include<map>
12 #include<set>
13 #define rre(i,r,l) for(int i=(r);i>=(l);i--)
14 #define re(i,l,r) for(int i=(l);i<=(r);i++)
15 #define Clear(a,b) memset(a,b,sizeof(a))
16 #define inout(x) printf("%d",(x))
17 #define douin(x) scanf("%lf",&x)
18 #define strin(x) scanf("%s",(x))
19 #define LLin(x) scanf("%lld",&x)
20 #define op operator
21 #define CSC main
22 typedef unsigned long long ULL;
23 typedef const int cint;
24 typedef long long LL;
25 using namespace std;
26 void inin(int &ret)
27 {
28     ret=0;int f=0;char ch=getchar();
29     while(ch<'0'||ch>'9'){if(ch=='-')f=1;ch=getchar();}
30     while(ch>='0'&&ch<='9')ret*=10,ret+=ch-'0',ch=getchar();
31     ret=f?-ret:ret;
32 }
33 int sa[70030],c[70111],t[70030],t2[70030],rank[70030];
34 char s[70030];
35 void build_sa(int m)
36 {
37     int *x=t,*y=t2,n=strlen(s),p=0;
38     re(i,0,m-1)c[i]=0;
39     re(i,0,n-1)c[x[i]=s[i]]++;
40     re(i,1,m-1)c[i]+=c[i-1];
41     rre(i,n-1,0)sa[--c[x[i]]]=i;
42     for(int k=1;k<=n;k<<=1)
43     {
44         p=0;
45         rre(i,n-1,n-k)y[p++]=i;
46         re(i,0,n-1)if(sa[i]>=k)y[p++]=sa[i]-k;
47         re(i,0,m-1)c[i]=0;
48         re(i,0,n-1)c[x[y[i]]]++;
49         re(i,1,m-1)c[i]+=c[i-1];
50         rre(i,n-1,0)sa[--c[x[y[i]]]]=y[i];
51         swap(x,y);
52         x[sa[0]]=0,p=1;
53         re(i,1,n-1)
54             x[sa[i]]=y[sa[i]]==y[sa[i-1]]&&y[sa[i]+k]==y[sa[i-1]+k]?p-1:p++;
55         if(p>=n)break;
56         m=p;
57     }
58 }
59 int n;
60 int main()
61 {
62     freopen("in.in","r",stdin);
63     freopen("out.out","w",stdout);
64     scanf("%d",&n);
65     re(i,0,n-1)
66     {
67         getchar();
68         s[i]=getchar();
69     }
70     s[n]='Z'+1;
71     re(i,0,n-1)s[(n<<1)-i]=s[i];
72     build_sa(1000);
73     re(i,0,n<<1)rank[sa[i]]=i;
74     int l=0,r=n-1;
75     for(int tot=0;tot<n&&l<r;tot++)
76     {
77         if(tot&&tot%80==0)cout<<"\n";
78         if(rank[l]<rank[(n<<1)-r])printf("%c",s[l++]);
79         else printf("%c",s[(n<<1)-(r--)]);
80     }
81     printf("%c",s[l]);
82     return 0;
83 }    

posted @ 2016-02-28 11:00  HugeGun  阅读(89)  评论(0编辑  收藏  举报