POJ3617 Best Cow Line (字典序最小问题)
2017-02-23 13:59
351 查看
Best Cow Line
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
Sample Output
分析:
题目大意:
FJ要参加“年度农场主”这个比赛,每个农场主要把他们的牛排成一条直线,将每头牛名称的首字母合成作为注册名,例如,FJ带了Bessie, Sylvia, 和Dora三头牛,则注册名就叫做BSD。注册名字典序靠前的可以提前参加比赛,FJ今年很忙,所以想要把它的牛重新排列,每次只能选择原直线的队首或队尾的牛到新直线的队尾。给定牛的一个初始顺序,给出牛重新排列后的最小字典序排序。
这是一个贪心题。每次比较队首和队尾的元素大小,小的则输出,大的则留下继续比较,相等则继续比较下一元素,直至找到小的一方,如果一直比较下去也找不到小的一方,则随意输出一方的元素。、
AC代码:
#include<stdio.h>
int main()
{
int n;
char s[2100],t[2100];
scanf("%d",&n);
getchar();
for(int i=0; i<n; i++)
scanf("%c%*c",&s[i]);
int j=0,k=n-1,f=0,c=0;//设置最大值和最小值,从前后同时遍历!
while(j<=k)
{
//随着j,k的增加减少,原字符串也逐渐变短
for(int i=0; i<n; i++)//必须每次都进行遍历寻找小值,因为存在相同的情况
{
if(s[i+j]<s[k-i])//谁先遇到小值就先取那边的
{
f=1;
break;
}
if(s[j+i]>s[k-i])
{
f=0;
break;
}
}
if(f==0)//相同的情况下,就要根据里边的字母谁小去选择哪边
printf("%c",s[k--]);
else
printf("%c",s[j++]);
c++;
if(c%80==0)
printf("\n");
}
return 0;
}
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 22346 | Accepted: 6088 |
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
分析:
题目大意:
FJ要参加“年度农场主”这个比赛,每个农场主要把他们的牛排成一条直线,将每头牛名称的首字母合成作为注册名,例如,FJ带了Bessie, Sylvia, 和Dora三头牛,则注册名就叫做BSD。注册名字典序靠前的可以提前参加比赛,FJ今年很忙,所以想要把它的牛重新排列,每次只能选择原直线的队首或队尾的牛到新直线的队尾。给定牛的一个初始顺序,给出牛重新排列后的最小字典序排序。
这是一个贪心题。每次比较队首和队尾的元素大小,小的则输出,大的则留下继续比较,相等则继续比较下一元素,直至找到小的一方,如果一直比较下去也找不到小的一方,则随意输出一方的元素。、
AC代码:
#include<stdio.h>
int main()
{
int n;
char s[2100],t[2100];
scanf("%d",&n);
getchar();
for(int i=0; i<n; i++)
scanf("%c%*c",&s[i]);
int j=0,k=n-1,f=0,c=0;//设置最大值和最小值,从前后同时遍历!
while(j<=k)
{
//随着j,k的增加减少,原字符串也逐渐变短
for(int i=0; i<n; i++)//必须每次都进行遍历寻找小值,因为存在相同的情况
{
if(s[i+j]<s[k-i])//谁先遇到小值就先取那边的
{
f=1;
break;
}
if(s[j+i]>s[k-i])
{
f=0;
break;
}
}
if(f==0)//相同的情况下,就要根据里边的字母谁小去选择哪边
printf("%c",s[k--]);
else
printf("%c",s[j++]);
c++;
if(c%80==0)
printf("\n");
}
return 0;
}
相关文章推荐
- 字典序最小问题-贪心-Best Cow Line-POJ3617
- POJ3617 Best Cow Line(字典序最小问题)
- 贪心法(最小字典序)poj3617 Best Cow Line// acmclub 12701/12695
- 字典序最小问题——Best Cow Line
- POJ 3617 Best Cow Line(字典序最小问题)
- 043_字典序最小问题(best cow line)
- POJ No.3617-Best Cow Line(字典序最小问题)
- 字典序最小问题——Best Cow Line
- 字典序最小问题Best Cow Line
- POJ3617 Best Cow Line(贪心,字典序最小)
- 2.2.3 字典序最小问题 -Best Cow Line(POJ 3617)
- POJ NO.3617 Best Cow Line(字典序最小问题)
- 贪心算法-Best cow line-字典序问题
- POJ 3617 Best Cow Line [贪心] [字典序最小]
- 贪心算法-Best cow line-字典序问题
- POJ 3617 Best Cow Line (字典序最小_贪心)
- POJ3617---Best Cow Line(字典序,贪心)
- POJ 3617 Best Cow Line(贪心+最小字典序)
- POJ3617 Best Cow Line
- Best Cow Line---poj3617(贪心)