Language:
Best Cow Line, Gold
Description FJ is about to take his N (1 ≤ N ≤ 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 marshal the cows from the old line to the new one by repeatedly sending either the first or last cow in the (remainder of the) 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 Output The least lexicographic string he can make. Every line (except perhaps the last one) contains the initials of 80 cows ('A'..'Z') in the new line. Sample Input 6 A C D B C B Sample Output ABCBCD |
方法一:后缀数组
思路:首先把原串反转后拼接在原串后面,然后求rank数组,然后从0和n+1位置比较rank的大小
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=30010*2;
int sa[maxn],rank[maxn],t[maxn],t2[maxn],c[maxn];
int n,len;
char str[maxn];
void build_sa(int m,int n)
{
int *x=t,*y=t2;
for(int i=0;i<m;i++)c[i]=0;
for(int i=0;i<n;i++)c[x[i]=str[i]]++;
for(int i=1;i<m;i++)c[i]+=c[i-1];
for(int i=n-1;i>=0;i--)sa[--c[x[i]]]=i;
for(int k=1;k<=n;k<<=1)
{
int p=0;
for(int i=n-k;i<n;i++)y[p++]=i;
for(int i=0;i<n;i++)if(sa[i]>=k)y[p++]=sa[i]-k;
for(int i=0;i<m;i++)c[i]=0;
for(int i=0;i<n;i++)c[x[y[i]]]++;
for(int i=1;i<m;i++)c[i]+=c[i-1];
for(int i=n-1;i>0;i--)sa[--c[x[y[i]]]]=y[i];
swap(x,y);
x[sa[0]]=0;p=1;
for(int i=1;i<n;i++)
x[sa[i]]=(y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k]?p-1:p++);
if(p>=n)break;
m=p;
}
}
void getheight(int n)
{
int k=0;
for(int i=1;i<=n;i++)rank[sa[i]]=i;
}
void solve()
{
int pos1=0,pos2=n+1;
int cnt=0;
while(cnt<n)
{
if(rank[pos1]>rank[pos2])
{
printf("%c",str[pos2]);
pos2++;
}
else if(rank[pos2]>rank[pos1])
{
printf("%c",str[pos1]);
pos1++;
}
cnt++;
if(cnt%80==0)puts("");
}
}
int main()
{
//freopen("in.txt","r",stdin);
while(scanf("%d",&n)!=EOF)
{
getchar();
for(int i=0;i<n;i++)
{
scanf("%c",&str[i]);
getchar();
}
str[n]='$';
for(int i=n+1;i<2*n+1;i++)
str[i]=str[2*n-i];
str[2*n+1]=0;
len=2*n+2;
build_sa(123,len);
getheight(len-1);
solve();
}
return 0;
}
方法二:贪心
思路:每次取最小的,如果想等,则比较下一个
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int nMax=30005;
char str[nMax];
bool check(int a,int b){
while(a<b){
if(str[a]>str[b])return 1;
if(str[a]<str[b])return 0;
a++;
b--;
}
return 0;
}
int main(){
int n,i,j,head,tail;
while(scanf("%d",&n)!=EOF){
int cnt=0;
head=0,tail=n-1;
for(i=0;i<n;i++){
cin>>str[i];
}
while(head<=tail){
if(check(head,tail)==0){
printf("%c",str[head]);
cnt++;
head++;
}
else{
printf("%c",str[tail]);
cnt++;
tail--;
}
if(!(cnt%80))
printf("\n");
}
if(cnt%80)
printf("\n");
}
return 0;
}