FJ is an about-to-take he N (1≤ n ≤2,000) cows to the annual ' Farmer of the Year ' competition. In this contest every farmer arranges he 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 would appear (i.e., If FJ takes Bessie, Sylvia, and Dora in that order he just registers BSD). After the registration phase ends, every group was judged in increasing lexicographic order according to the string of the Initials of the cows ' names.
FJ is very busy this year and have to hurry back to his farm, so he wants to be judged as early as possible. He decides to rearrange him cows, who has already lined up, before registering them.
FJ marks a location for a new line of the competing cows. He then proceeds to marshal the cows from the old line to the new one by repeatedly sending either the first or last cow I n the (remainder of the) original line to the end of the new line. When he's finished, FJ takes his cows for registration on this new order.
Given the initial order of he cows, determine the least lexicographic string of initials he can make this.
Input
* Line 1: A single integer: N
* Lines 2. N+1:line i+1 contains a single initial (' A ' ... ' Z ') of the cow in the i-th position in the original line
Output
The least lexicographic string he can make. Every line (except perhaps, the last one) contains the initials of the cows (' A ' ... ' Z ') in the new line.
Sample Input
6ACDBCB
Sample Output
Abcbcd
----------------because of the reserve of Marseille, so has been reading and blogging (hope to win a). Before the rhythm too fast, should think more consolidation, so reduce the time to write code---------------
Test Instructions: Given a string s, now turn this s into a string T: Every time a character is added to T from the head or tail of s, it requires the smallest dictionary order.
idea: obviously greedy solution, if s head is not equal to s tail, then take small side add to T.
However, if the same, can not be taken, such as Bcab, if any take B, may become babc or BBAC, the latter is clearly substandard.
Should always be compared, know not the same or all the same: take the small side of the same, all the same will be taken at random.
The process of comparison can be violent, or it can be a suffix array.
Common Greedy Code:
#include <cstdio>#include<cstdlib>#include<iostream>#include<algorithm>using namespacestd;Const intmaxn= .;CharC[MAXN];BOOLLeft (intLintR) { while(l<=1) { if(c[l]!=C[r])returnc[l]<C[r]; L++; r--; } return true;}intMain () {intn,cnt; scanf ("%d",&N); for(intI=1; i<=n;i++) cin>>C[i]; intL=1, R=n; Cnt=0; while(l<=R) { if(Left (l,r)) cout<<c[l++]; Elsecout<<c[r--]; CNT++; if(cnt== the) printf ("\ n"), cnt=0; } return 0;}
Suffix Array Code:
#include <cmath>#include<math.h>#include<cstdio>#include<cstdlib>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;Const intmaxn=4010;intNCharC[MAXN];structsa{intrank[maxn],a[maxn],b[maxn],cnta[maxn],cntb[maxn],sa[maxn],tsa[maxn],ht[maxn],min[maxn][ A]; voidsort () { for(intI=0; i<= -; i++) cnta[i]=0; for(intI=1; i<=n;i++) cnta[c[i]]++; for(intI=1; i<= -; i++) cnta[i]+=cnta[i-1]; for(inti=n;i>=1; i--) sa[cnta[c[i]]--]=i; rank[sa[1]]=1; for(intI=2; i<=n;i++) rank[sa[i]]=rank[sa[i-1]]+ (c[sa[i]]==c[sa[i-1]]?0:1); for(intL=1; rank[sa[n]]<n;l<<=1){ for(intI=0; i<=n;i++) cnta[i]=cntb[i]=0; for(intI=1; i<=n;i++) cnta[a[i]=rank[i]]++; for(intI=1; i<=n;i++) cntb[b[i]=i+l<=n? RANK[I+L]:0]++; for(intI=1; i<=n;i++) cnta[i]+=cnta[i-1],cntb[i]+=cntb[i-1]; for(inti=n;i>=1; i--) tsa[cntb[b[i]]--]=i; for(inti=n;i>=1; i--) sa[cnta[a[tsa[i]]]--]=Tsa[i]; rank[sa[1]]=1; for(intI=2; i<=n;i++) rank[sa[i]]=rank[sa[i-1]]+ (a[sa[i]]==a[sa[i-1]]&&b[sa[i]]==b[sa[i-1]]?0:1); }}}s;intMain () {intn,cnt; scanf ("%d", &n); n=n+n+1; for(intI=1; i<=n;i++) cin>>C[i]; for(inti=n;i>=1; i--) c[n-i]=C[i]; intL=1, R=n; Cnt=0; S.sort (); while(l<=R) { if(S.rank[l]<=s.rank[n-r]) cout<<c[l++]; Elsecout<<c[r--]; CNT++; if(cnt== the) printf ("\ n"), cnt=0; } return 0;}
Poj3617:best Cow Line (greedy && suffix array)