Description
Given a sequence, {A1, A2, ..., an} which is guaranteed A1 > A2, ..., an, you be to-cut it into three sub-sequences and re Verse them separately to form a new one which are the smallest possible sequence in alphabet order.
The alphabet order is defined as Follows:for-sequence {A1, A2, ..., an} and {B1, B2, ..., Bn}, we say {A1, A2, ..., an} are Smaller than {B1, B2, ..., Bn} if and only if there exists such I (1≤i≤n) so which we have Ai < Bi and Aj = Bj for ea Ch J < I.
Input
The first line contains N. (n≤200000)
The following n lines contain the sequence.
Output
Output n lines which is the smallest possible sequence obtained.
Sample Input
5
10
1
2
3
4
Sample Output
1
10
2
4
3
Hint
{1, 2, 3, 4}, {ten, 1 | 2 | 3, 4}, {1, 10, 2, 4, 3}
Source
POJ founder Monthly contest–2008.04.13, Yao Jinyu
First, the string is flipped, the suffix array, to obtain a minimum suffix, of course, the starting point of the minimum suffix should be in the second-to-last position of the original string, otherwise it cannot be divided into three sections
Then for the remainder of the string, the equivalent is to be divided into 2 copies and then the dictionary is the smallest, for this, I initially want to use the minimum representation of the linear method to seek, and later found that this may occur in the case of non-segmentation, that is, the dictionary of the string is the smallest
So I repeat the remainder of the string once together, and then the suffix array, so that each string representation of the dictionary ranking, of course, we want the dictionary order is the smallest and position in the first position of the string before the
/************************************************************************* > File Name:POJ3581.cpp > Auth Or:alex > Mail: [email protected] > Created time:2015 April 08 Wednesday 10:15 12 seconds ******************************** ****************************************/#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <cstring>#include <cstdio>#include <cmath>#include <cstdlib>#include <queue>#include <stack>#include <map>#include <bitset>#include <set>#include <vector>using namespace STD;Const DoublePI =ACOs(-1.0);Const intINF =0x3f3f3f3f;Const DoubleEPS =1e-15;typedef Long LongLL;typedefPair <int,int> PLL;intxis[202000];intCntintBinsearch (intVal) {intL =1, R = CNT, mid; while(L <= R) {mid = (L + r) >>1;if(Xis[mid] = = val) { Break; }Else if(Xis[mid] > val) {R = mid-1; }Else{L = mid +1; } }returnMid;}classsuffixarray{ Public:Static Const intN =402000;intInit[n];intX[n];intY[n];intRank[n];intSa[n];intHeight[n];intBuc[n];intSizevoidClear () {size =0; }voidInsertintN) {init[size++] = n; }BOOLcmpint*r,intAintBintL) {return(R[a] = = R[b] && r[a + l] = = R[b + L]); }voidGetsa (intm) {Init[size] =0;intL, p, *x = x, *y = y, n = size +1; for(inti =0; I < m; ++i) {Buc[i] =0; } for(inti =0; I < n; ++i) {++buc[x[i] = init[i]]; } for(inti =1; I < m; ++i) {Buc[i] + = buc[i-1]; } for(inti = n-1; I >=0; -i) {sa[--buc[x[i]] = i; } for(L =1, p =1; L <= N && p < n; m = p, l *=2) {p =0; for(inti = n-l; I < n; ++i) {y[p++] = i; } for(inti =0; I < n; ++i) {if(Sa[i] >= L) {y[p++] = sa[i]-l; } } for(inti =0; I < m; ++i) {Buc[i] =0; } for(inti =0; I < n; ++i) {++buc[x[y[i]]; } for(inti =1; I < m; ++i) {Buc[i] + = buc[i-1]; } for(inti = n-1; I >=0; -i) {sa[--buc[x[y[i]]] = y[i]; }intI for(Swap (x, y), x[sa[0]] =0, p =1, i =1; I < n; ++i) {X[sa[i]] = cmp (y, Sa[i-1], Sa[i], L)? Ns1: p++; } } }voidGetHeight () {inth =0, n = size; for(inti =0; I <= N; ++i) {Rank[sa[i]] = i; } height[0] =0; for(inti =1; I < n; ++i) {if(H >0) {--h; }intj = Sa[rank[i]-1]; for(; i + H < n && j + H < n && init[i + h] = = Init[j + h]; ++h); Height[rank[i]-1] = h; }}}sa;intarr[202000];intMain () {intNscanf("%d", &n); CNT =0; Sa.clear (); for(inti =0; I < n; ++i) {scanf("%d", &arr[i]); XIS[++CNT] = Arr[i]; }if(N <=3) { for(inti =0; I < n; ++i) {printf("%d\n", Arr[i]); }return 0; } sort (XIs +1, XIs +1+ CNT); CNT = unique (XIs +1, XIs +1+ CNT)-XIs-1; for(inti = n-1; I >=0; -I.) {intval = Binsearch (Arr[i]); Sa.insert (Val); } Sa.getsa (CNT +1); Sa.getheight ();intmins = inf, POS; for(inti =2; I < n; ++i) {if(SA. Rank[i] < mins) {mins = SA. Rank[i]; pos = i; }} sa.clear (); for(inti = N-pos-1; I >=0; -I.) {printf("%d\n", Arr[i]); } for(inti = n-1; i > N-pos-1; -I.) {intval = Binsearch (Arr[i]); Sa.insert (Val); } for(inti = n-1; I >= N-pos; -I.) {intval = Binsearch (Arr[i]); Sa.insert (Val); } Sa.getsa (CNT +1); Sa.getheight (); mins = inf;intS for(inti =1; I < POS; ++i) {if(SA. Rank[i] < mins) {mins = SA. Rank[i]; s = i; } } for(inti = n-S-1; I >= N-pos; -I.) {printf("%d\n", Arr[i]); } for(inti = n-1; I > N-S-1; -I.) {printf("%d\n", Arr[i]); }return 0;}
POJ3581---Sequence (suffix array)