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
明らかに、問題の解決策は貪欲です。 辞書式順序のために、最初と最後から小さなものを入力する必要があります。 問題は、頭と尾が等しい場合です。
私の最初の試みでは、両方を使用しようとしました。1つはヘッド付き、もう1つはテール付きです。 このように、多くの文字列があり、それらを再度並べ替えることができます。 したがって、少しの再帰から始めます。 後で送信すると、メモリを超えるか、制限時間を超えます。
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
コメント