| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 23474 | Accepted: 6379 |
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
每八十个字符换一行!!!
做一只打不死,拖不跨的小强
#include <cstdio> #include <algorithm> #include <iostream>#define MAX 2009using namespace std;string A; char a[MAX];bool dp(int x,int y)//因为有可能出现首字符与末尾字符相等的情况,所以要不断判断直到出现不相等的情况 { if (a[x]==a[y]) { return dp(x+1,y-1); } if (a[x]<=a[y]) return true; else return false; }int main() { int N; cin>>N;for (int i=0;i<N;i++) { cin>>a[i]; } int left=0,right=N-1,sum=0;while(sum<N) { if (a[left]<a[right]) { A+=a[left]; left++; } else if (a[left]>a[right]) { A+=a[right]; right--; } else if (a[left]==a[right]) { if (dp(left,right)) { A+=a[left]; left++; } else { A+=a[right]; right--; } } sum++; } int temp=0; for (int i=0;i<A.size();i++) { cout<<A[i]; temp++; if (temp==80) { temp=0; cout<<endl; } } return 0; }
本文介绍了一个算法问题,即如何通过重新排列农民FJ的牛群来实现注册时的最小字典序。通过比较牛的名字首字母,决定每次从队首或队尾选择牛加入新队伍的方式,最终达到注册字符串最小化的目标。
255

被折叠的 条评论
为什么被折叠?



