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.
6
A
C
D
B
C
B
ABCBCD
代码:
//有一个测试点不通过,求大佬查看帮忙
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<deque>
#include<map>
#include<queue>
#include<iomanip>
#include<string>
#include<stack>
#include<set>
#include<vector>
#include<list>
typedef long long ll;
using namespace std;
int main()
{
int n;
cin>>n;
//stack <char> s1;
//queue <char> s2;
char s[2003];
char s1[2003];
char s2[2003];
for(int i=1;i<=n;i++)
{
char c;
cin>>c;
s1[i]=c;
s2[n+1-i]=c;
}
/*for(int i=1;i<=n;i++)
{
cout<<s1[i]<<" "<<s2[i]<<endl;
}*/
/*while(s1.empty()!=true)
{
cout<<s1.top()<<" ";
s1.pop(); //删除队首元素
}
cout<<endl;
while(s2.empty()!=true)
{
cout<<s2.front()<<" ";
s2.pop(); //删除队首元素
}*/
int p=1,q=1;
for(int i=1;i<=n;i++)
{
if(s1[p]<s2[q]) {//正串首字符小于反串,选正串首字符
s[i]=s1[p];
p++;
}
else if(s1[p]>s2[q]){//正串首字符大于反串,选反串首字符
s[i]=s2[q];
q++;
}
else {//如果首字符相同,查看下一个字符
if(s1[p+1]<=s2[q+1]){//如果下一个字符相同,随便选一个
s[i]=s1[p];
p++;
}
else if(s1[p+1]>s2[q+1]){
s[i]=s2[q];
q++;
}
}
//cout<<p<<" "<<q<<endl;
//cout<<s<<endl;
}
for(int i=1;i<=n;i++)
cout<<s[i];
//cout<<s1<<" "<<s2<<endl;
return 0;
}