POJ3617Best Cow Line(贪心)

来源:互联网 发布:皇后谥号 知乎 编辑:程序博客网 时间:2024/06/07 15:27

传送门:http://poj.org/problem?id=3617

Best Cow Line
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 23932 Accepted: 6498

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 theith 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

6ACDBCB

Sample Output

ABCBCD

Source

USACO 2007 November Silver

翻译一下题目就是:给定长度为N的字符串S,要构造一个长度为N的字符串T。起初,T是一个空串T,随后反复进行下列任意操作。

从S的头部删除一个字符,加到T的尾部

从S的尾部删除一个字符,加到T的尾部

目标是要构造字典序尽可能小的字符串T。

简单说就是每次都可以从字符串的首部或者尾部提取字母,使得最后的字符串的字典序最小。

输出,切记,每80个字符输出一行。

限制条件

1《=N《=2000

字符串S只包含大写英文字母

题解:从字典序的性质(字典序指的是从前到后比较两个字符串大小的方法。首先比较第一个字符,如果不同则第一个字符较小的字符串更小,如果相同则继续比较第2个字符……如此继续,来比较整个字符串的大小)来说,无论T结尾多大只要前面部分小就可以。

贪心即可:

不断取S开头和结尾较小的一个字符放到T末尾。

按照字典序比较S和将S反转后的字符串S1

以下是我的TLE超时代码:

#include<iostream>#include<cstdio>using namespace std;int N;char S[2001];void solve(){int a=0,b=N-1,cnt=0;while(a<=b){bool left=false;for(int i=0;a+i<=b;i++){if(S[a+i]<S[b-i]){left=true;break;}else if(S[a+i]>S[b-i]){left=false;break; }if(left)putchar(S[a++]);else putchar(S[b--]);cnt++;if(cnt==80)printf("\n"); cnt=0;}}printf("\n"); }int main(){cin >> N;for (int i = 0; i < N; i++)cin >> S[i];solve();return 0;}

如果把
int main(){cin >> N;for (int i = 0; i < N; i++)cin >> S[i];solve();return 0;}
改成

int main(){while (~scanf("%d", &N)) solve();return 0;}
会变成OTE。

最后我的AC代码如下

#include<iostream>#include<cstdio>using namespace std;int N;char S[2001];void solve(){int a=0,b=N-1,cnt=0;while(a<=b){bool left=false;for(int i=0;a+i<=b;i++){if(S[a+i]<S[b-i]){left=true;break;}else if(S[a+i]>S[b-i]){left=false;break; }}if(left)putchar(S[a++]);else putchar(S[b--]);cnt++;if(cnt==80){printf("\n"); cnt=0;}}printf("\n"); }int main(){cin >> N;for (int i = 0; i < N; i++)cin >> S[i];solve();return 0;}




原创粉丝点击