1 /**************************************************************
2 Problem: 1692
3 User: Tunix
4 Language: C++
5 Result: Accepted
6 Time:248 ms
7 Memory:3616 kb
8 ****************************************************************/
9
10 //BZOJ 1640
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=1e5+10,INF=~0u>>2;
29 typedef long long LL;
30 /******************tamplate*********************/
31 int n,m,a[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(int *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)
52 x[sa[i]]=cmp(y,sa[i-1],sa[i],j) ? p-1 : p++;
53 }
54 }
55 int main(){
56 #ifndef ONLINE_JUDGE
57 freopen("1640.in","r",stdin);
58 freopen("1640.out","w",stdout);
59 #endif
60 n=getint(); m=0; int tmp=0;
61 char s[5];
62 rep(i,n){
63 scanf("%s",s);
64 a[i]=s[0]-‘A‘+1;
65 }
66 int tot=n*2+1;
67 F(i,1,n) a[n+i]=a[n-i];
68 DA(a,sa,tot+1,28);
69 F(i,1,tot) rank[sa[i]]=i;
70 int l=0,r=n+1;
71 while(l+r-n-1<n){
72 if (rank[l]<rank[r]) putchar(a[l++]+‘A‘-1);
73 else putchar(a[r++]+‘A‘-1);
74 if (!((l+r-n-1)%80)) puts("");
75 }
76 return 0;
77 }