Description
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
很显然,题目的解决方法是贪心。我为了字典顺序小,我肯定从头和尾抽一个小的填进来。问题就在于头和尾相等的时候。
在第一次尝试时,我尝试将两者都用上,用头的做一种情况,用尾的做一种情况。这样就有很多个字符串了,再sort一下就可以。所以开始用一点递归。后来在提交的时候不是超出内存就是超出了时间限制。
void connect(string str,int start,int end){
if(start==end){
str+=origin_str[start];
strs.push_back(str);
return;
}
if(origin_str[start]<origin_str[end]){
str+=origin_str[start];
connect(str,start+1,end);
}else if(origin_str[start]>origin_str[end]){
str+=origin_str[end];
connect(str,start,end-1);
}else if(origin_str[start]==origin_str[end]){
str+=origin_str[start];
connect(str,start+1,end);
connect(str,start,end-1);
}
}
所以,在相等的时候,肯定只要判断相等的后面,哪个相等取走后利益高,就用哪个。所以向中判断。让程序判断取走后的下一个,下下一个......这些情况哪些好就可以了。
void connect(string str,int start,int end){
if(start==end){
str+=origin_str[start];
if(best_str=="") best_str=str;
else if(str<best_str) best_str=str;
return;
}
if(origin_str[start]<origin_str[end]){
str+=origin_str[start];
connect(str,start+1,end);
}else if(origin_str[start]>origin_str[end]){
str+=origin_str[end];
connect(str,start,end-1);
}else if(origin_str[start]==origin_str[end]){
str+=origin_str[start];
string left="",right="";
for(int i=start+1,j=end-1;i<j;i++,j--){
left+=origin_str[i];
right+=origin_str[j];
}
if(left<right){
connect(str,start+1,end);
}else connect(str,start,end-1);
}
}
可惜这样还是超时了,然后试图用一下substr,诶,就过了。就是substr的长度要判断好。
实际上不需要也不能从中间切开,直接提取去从头到尾的和反转的从头到尾就可以了。我脑子有点抽。
void connect(string str,int start,int end){
if(start==end){
str+=origin_str[start];
if(best_str=="") best_str=str;
else if(str<best_str) best_str=str;
return;
}
if(origin_str[start]<origin_str[end]){
str+=origin_str[start];
connect(str,start+1,end);
}else if(origin_str[start]>origin_str[end]){
str+=origin_str[end];
connect(str,start,end-1);
}else if(origin_str[start]==origin_str[end]){
str+=origin_str[start];
string left="",right="";
left=origin_str.substr(start+1,(end-start)/2);
if((end+start)%2==0) right=origin_str.substr((start+end)/2,(end-start)/2);
else right=origin_str.substr((start+end)/2+1,(end-start)/2);
reverse(right.begin(),right.end());
if(left<right){
connect(str,start+1,end);
}else connect(str,start,end-1);
}
}
#include <iostream> #include <algorithm> #include <cstring> using namespace std; int length; string origin_str=""; string best_str=""; void connect(string str,int start,int end){ if(start==end){ str+=origin_str[start]; if(best_str=="") best_str=str; else if(str<best_str) best_str=str; return; } if(origin_str[start]<origin_str[end]){ str+=origin_str[start]; connect(str,start+1,end); }else if(origin_str[start]>origin_str[end]){ str+=origin_str[end]; connect(str,start,end-1); }else if(origin_str[start]==origin_str[end]){ str+=origin_str[start]; string left="",right=""; left=origin_str.substr(start+1,(end-start)/2); if((end+start)%2==0) right=origin_str.substr((start+end)/2,(end-start)/2); else right=origin_str.substr((start+end)/2+1,(end-start)/2); reverse(right.begin(),right.end()); if(left<right){ connect(str,start+1,end); }else connect(str,start,end-1); } } int main(){ // freopen("TrueExample.txt","r",stdin); cin >> length; for(int abc=0;abc<length;abc++){ char c; cin >> c; origin_str+=c; } connect("",0,length-1); for(int i=0;i<best_str.size();i++){ cout << best_str[i]; if((i+1)%80==0) cout << endl; } return 0; }
阴间输入
333
D
A
C
C
C
E
D
D
E
E
C
C
A
D
E
B
B
C
C
D
E
E
D
D
D
B
E
D
A
E
C
B
A
E
C
B
E
E
B
B
A
E
C
A
B
C
E
A
C
A
B
D
A
E
E
A
A
D
C
E
C
D
D
C
E
A
B
B
C
A
A
C
B
A
C
A
A
C
A
B
A
E
D
C
D
A
D
E
A
B
C
E
D
C
C
B
E
D
A
C
D
E
D
C
B
E
B
C
B
C
D
D
C
D
A
E
C
B
E
E
B
E
D
C
D
A
D
D
B
D
B
C
B
E
B
B
C
E
A
B
D
C
D
E
D
E
E
D
C
E
D
E
E
E
C
C
C
D
E
D
B
C
B
A
D
A
A
B
C
D
B
B
E
E
D
B
B
E
D
C
C
C
C
D
C
E
A
B
C
B
E
E
D
B
B
A
B
C
B
A
A
D
B
E
E
D
A
A
D
D
A
A
A
B
E
C
A
A
B
A
D
C
A
D
D
A
A
D
A
E
D
D
D
C
C
B
D
C
A
C
E
E
C
C
D
B
E
D
C
C
D
C
B
A
E
D
E
E
A
B
E
D
C
D
E
D
E
A
C
E
E
A
A
E
C
A
E
B
A
D
B
B
C
B
C
C
E
A
B
D
D
B
C
B
D
D
E
E
A
E
A
C
A
E
B
D
E
E
C
A
B
B
A
A
E
E
D
C
B
E
E
B
B
C
E
E
B
E
E
E
C
B
E
输出
DACCCEBCEDDEECCADEBBCCDEEDDDBEDAECBAECBEEBBAECABCEACABDAEEAADCECDDCEABBCAACBACAA
CABAEDCDADEABCEDCCBEDACDEDCBEBCBCDDCDAECBEEBEDCDADDBDBCBEBBCEABDCDEDEEDCEDEEEBEE
CBBEEBCDEEAABBACEEDBEACAEAEEDDBCBDDBAECCBCBBDABEACEAAEECAEDEDCDEBAEEDEABCDCCDEBD
CCEECACDBCCDDDEADAADDACDABAACEBAAADDAADEEBDAABCBABBDEEBCBAECDCCCCDEBBDEEBBDCBAAD
ABCBDEDCCCEEE
文章评论