Reprint Please specify Source: http://www.cnblogs.com/fraud/ --by Fraud
Sequence
| Time Limit: 5000MS |
|
Memory Limit: 65536K |
| Case Time Limit: 2000MS |
Description
Given a sequence, {a1, a2, ..., an} which is guaranteed a1 > a2, ..., an, you-to-cut it into three sub-sequences and reverse them separately to form a new one which is the Smalle St 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} is smaller than {B1, b 2, ..., Bn} if and only if there exists such i (1≤ i ≤ n) so that we hav E Ai < Bi and Aj = Bj for each 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
5101234
Sample Output
110243
Hint
{1, 2, 3, 4}, {ten, 1 | 2 | 3, 4}, {1, 10, 2, 4, 3}
Test instructions
The number of n is given, the sequence is divided into three segments, and the three-segment inversion is joined together to become a new string, and a new string with the smallest dictionary order is obtained.
Ideas:
Since the first number is guaranteed to be larger than all other numbers, the first paragraph is taken directly after the inverted dictionary with the smallest suffix. It can be obtained by using the suffix array.
The remaining string is then divided into two paragraphs, making the dictionary order minimal. First, it is easy to think of the same approach to the first paragraph, that is, the smallest suffix of the dictionary sequence after the reversal.
But for this method, we can easily find a set of counter-examples, namely 10 1 2 2 3
According to the above method, we will get the first paragraph of 10 1, invert, change to 1 10
The remaining string is 2 2 3, and for this string, 3 2 2 is reversed, and the dictionary order is the smallest of 2. The entire string becomes 1 10 2 3 2
Obviously, when we take 2 2, the dictionary order of the whole string is the smallest, 1 10 2 2 3.
For a string of length M s[1]......s[m], set us to divide it into s[1]......s[k] and s[k+1]......s[m]
After it is reversed then s[k]......s[1]s[m]......s[k+1], we can see that this string is the substring of s[m]......s[k+1]s[k]......s[1]s[m]......s[k+1]s[k]......s[1], And we can get the answer by finding the suffix of the string that is greater than the minimum number of the M dictionary order.
For the above example 3 2 2, can be changed to 3 2 2 3 2 2, the minimum length of the dictionary sequence is greater than M suffix is 2 2 3 2 2. Take the preceding paragraph as the second segment of the request.
In addition, this is titled a single set of input, otherwise it will WA, this pit me for a long time ....
1#include <iostream>2#include <algorithm>3#include <cstdio>4 using namespacestd;5 #defineMAXN 4000106 intn,k;7 intSA[MAXN],RANK[MAXN],A[MAXN],B[MAXN],C[MAXN],TMP[MAXN];8 BOOLcmpintIintj) {9 if(Rank[i]!=rank[j])returnrank[i]<Rank[j];Ten Else { One intri=i+k<=n?rank[i+k]:-1e8; A intrj=j+k<=n?rank[j+k]:-1e8; - returnri<RJ; - } the } - voidBuildintLenint*s) { -n=Len; - for(intI=0; i<=n;i++) sa[i]=i,rank[i]=i<n?s[i]:-1e8; + for(k=1; k<=n;k<<=1){ -Sort (sa,sa+n+1, CMP); +tmp[sa[0]]=0; A for(intI=1; i<=n;i++){ attmp[sa[i]]=tmp[sa[i-1]]+ (CMP (sa[i-1],sa[i])?1:0); - } - for(intI=0; i<=n;i++) rank[i]=Tmp[i]; - } - } - intMain () in { - intN; toscanf"%d",&N); + //while (scanf ("%d", &n)!=eof) { - for(intI=0; i<n;i++) scanf ("%d", A +i); the for(intI=0; i<n;i++) b[i]=a[n-1-i]; * build (n,b); $ intP1;Panax Notoginseng for(intI=0; i<=n;i++){ -p1=n-sa[i]-1; the if(p1>=0&&p1+3<=n) Break; + } A intM=n-p1-1; the for(intI=0; i<m;i++) c[i]=a[i+p1+1]; + for(intI=0; i<m;i++) b[i]=b[i+m]=c[m-1-i]; -Build2*m,b); $ intP2; $ for(intI=0; i<=2*m;i++) - { -p2=m-sa[i]-1; the if(p2>=0&&p2<=m-2) Break; - }Wuyip2+=p1+1; the for(inti=p1;i>=0; i--) printf ("%d\n", A[i]); - for(inti=p2;i>p1;i--) printf ("%d\n", A[i]); Wu for(inti=n-1; i>p2;i--) printf ("%d\n", A[i]); - //} About return 0; $}code June
poj3581sequence (suffix array)