POJ3623: Best Cow Line, Gold (suffix array)
Description
FJ is about to take hisN(1 ≤NLess than 30,000) cows to the annual "Farmer of the Year" competition. In this contest every farmer arranges his cows in a line and herds them past the judges.
The contest organizers adopted a new registration scheme this year: simply register the initial letter of every cow in the order they will appear (I. e ., if FJ takes Bessie, Sylvia, and Dora in that order he just registers BSD ). after the registration phase ends, every group is judged in increasing lexicographic order according to the string of the initials of the cows 'names.
FJ is very busy this year and has to hurry back to his farm, so he wants to be judged as early as possible. he decides to rearrange his cows, who have already lined up, before registering them.
FJ marks a location for a new line of the competing cows. he then proceeds to convert al the cows from the old line to the new one by repeatedly sending either the first or last cow in the (remainder of) original line to the end of the new line. when he's finished, FJ takes his cows for registration in this new order.
Given the initial order of his cows, determine the least lexicographic string of initials he can make this way.
Input
* Line 1: A single integer:N
* Lines 2 ..N+ 1: LineI+ 1 contains a single initial ('A' .. 'Z') of the cow inITh position in the original line
Output
The least lexicographic string he can make. Every line (character t perhaps the last one) contains the initials of 80 cows ('A'... 'Z') in the new line.
Sample Input
6ACDBCB
Sample Output
ABCBCD
Source
USACO 2007 December Gold
Question: The minimum Lexicographic Order can only be obtained from both sides each time. The minimum idea is to find the rank array and start from left and right.
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
Using namespace std; # define LS 2 * I # define RS 2 * I + 1 # define UP (I, x, y) for (I = x; I <= y; I ++) # define DOWN (I, x, y) for (I = x; I> = y; I --) # define MEM (a, x) memset (, x, sizeof (a) # define W (a) while (a) # define gcd (a, B) _ gcd (a, B) # define LL long # define N 2000005 # define MOD 1000000007 # define INF 0x3f3f3f # define EXP 1e-8int wa [N], wb [N], wsf [N], wv [N], sa [N]; int rank [N], height [N], s [N]; // sa: in the Lexicographic Order, the starting position of the I-th position is in str sa [I] // rank: that is, the suffix of the I-th position of str is in the Lexicographic Order. // height: the longest common prefix int cmp (int * r, int a, int B, int k) of the suffix of the I and I-1) {return r [a] = r [B] & r [a + k] = r [B + k];} void getsa (int * r, int * sa, int n, int m) // n should contain the added 0 {int I, j, p, * x = wa, * y = wb, * t; for (I = 0; I
= 0; I --) sa [-- wsf [x [I] = I; p = 1; j = 1; for (; p
= J) y [p ++] = sa [I]-j; for (I = 0; I
= 0; I --) sa [-- wsf [wv [I] = y [I]; t = x; x = y; y = t; x [sa [0] = 0; for (p = 1, I = 1; I