题目:POJ3623
FJ is about to take his N (1 ≤ N ≤ 2,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
Lines 2…N+1: Line i+1 contains a single initial (‘A’…‘Z’) of the cow in the ith position in the original line
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
翻译:
FJ即将把他的N(1≤N≤2000)牛一年一度的“农民”竞争。在这个比赛中,每个农民把他的牛排成一列,让它们赶过裁判。
今年,比赛组织者采用了一种新的注册方案:只需按照奶牛出现的顺序(即,每头奶牛的名字)注册它们的首字母即可。,如果FJ按照这个顺序接收Bessie、Sylvia和Dora,那么他只需注册BSD)。在登记阶段结束后,根据奶牛名字的首字母串增加词汇顺序来判断每一组奶牛。
福建今年很忙,必须赶回他的农场,所以他想尽早被判断。他决定在登记之前重新安排已经排好队的奶牛。
福建标志着一个新的位置的竞争奶牛线。然后,他通过重复地将原行(其余部分)中的第一头母牛或最后一头母牛发送到新行末尾,将奶牛从旧行组织到新行。当他完成后,FJ以这个新顺序带着他的奶牛注册。
给定他的奶牛的初始顺序,确定他用这种方法所能得到的最小的单词首字母串。
输入
第1行:单个整数:N线2 . .
N+1:第i+1行包含奶牛在原行第i个位置的单个初始值(“A”…“Z”)
输出
这是他能造出的最小的字母串。每一行(也许最后一行除外)都包含新行中80头奶牛的首字母缩写(“A”…“Z”)。
#include<iostream>
using namespace std;
int main()
{
char a[4000];
int n,i;
cin>>n;
int x=0,y=n-1;
bool jg=false;
for(i=0;i<n;i++)
cin>>a[i];
while(x<=y)
{
for(int i=0;i<=y-x;i++)
{
if(a[x]>a[y])
{
jg=false;
break;
}
else
if(a[x]<a[y])
{
jg=true;
break;
}
}
if(jg)
cout<<a[x++];
else
cout<<a[y--];
}
return 0;
}