poj 3617 Best Cow Line (字符串反转贪心算法)

Best Cow Line

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9284   Accepted: 2826

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

AC代码:
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<string>
 6 #include<cmath>
 7 #include<map>
 8 #include<queue>
 9 using namespace std;
10 char s[2010],s1[2010];
11 int compare(int x,int y,int t)
12 {
13     //cout<<x<<"**"<<y<<endl;
14     //cout<<"*"<<s[1]<<"*"<<endl;
15    // cout<<s[x]<<"^^"<<s1[y]<<endl;
16     int j=0;
17     while(j<=t)
18     {
19
20        if(s[x]<s1[y])
21               return 1;
22            else if(s[x]>s1[y])
23                return 2;
24          x++;y++;j++;
25     }
26     return 0;
27 }
28 int main()
29 {
30     int i,j,k,t,m,n;
31     while(cin>>t)
32     {
33         for(int i=0,j=t-1;i<t;i++,j--)
34         {
35                cin>>s[i];
36               s1[j]=s[i];
37         }
38             n=t;
39             int x=0,y=0,ans=0;
40             while(n--)
41             {
42               int kk=compare(x,y,n);
43                if(kk==1 || kk==0)
44                {
45                    cout<<s[x];
46                    x=x+1;
47                }
48                 else if(kk==2)
49                 {
50                     cout<<s1[y];
51                     y=y+1;
52                 }
53                 ans++;
54                 if(ans%80==0)
55                     cout<<endl;
56             }
57     }
58     return 0;
59 }

poj 3617 Best Cow Line (字符串反转贪心算法)

时间: 2024-07-30 10:13:32

poj 3617 Best Cow Line (字符串反转贪心算法)的相关文章

POJ 3617 Best Cow Line (贪心)

Best Cow Line Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11230   Accepted: 3329 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

poj 3617 Best Cow Line(贪心)

 Best Cow Line Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8579   Accepted: 2629 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 h

poj 3617 Best Cow Line

Best Cow Line Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20226   Accepted: 5563 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

POJ 3617 Best Cow Line (贪心)

题意: 给定长度为N的字符串S,要构造一个长度为N的字符串T.起初T是一个空串,随后反复进行下列任意操作 1.从字符串S头部删除一个字符,加到T的尾部 2.从字符串S尾部删除一个字符,加到T的尾部 目的是,构造字典序尽可能小的字符串T 思路:简单贪心,比较当前字符串S首尾字符的大小,选取小的加入T,若两者相同,则比较下一个字符. #include<stdio.h> #include<queue> #include<iostream> #include<algori

贪心/POJ 3617 Best Cow Line

1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 int main() 5 { 6 int n; 7 char s1[2020],s2[2020]; 8 scanf("%d",&n); 9 for (int i=0;i<n;i++) 10 { 11 char ch; 12 scanf(" %c",&ch); 13 s1[i]=ch; 14 }

POJ 3617 - Best Cow Line(字典序最小) 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://poj.org/problem?id=3617 洛谷题目链接:https://www.luogu.org/problem/show?pid=2870 题目大意: 给定一个长度为N(1<=N<=30000)的字母序列,每次可以从序列头部或尾部取出一个字母加入一个队列的尾部,求此队列字典序最小的排列. 每输出80个字母,换行一次. 分析: 每次比较序列首部和尾部,取出较小的一个,如果相等就继续往下比较,直

POJ 3623 Best Cow Line, Gold(字符串处理)

题意:给你一个字符串,让你重新排列,只能从头或者尾部取出一个放到新字符串队列的最后.按照字典序. 解决方法:比较前后两个的大小,谁小输出谁,相等,就往当中比来确定当前应该拿最前面的还是最后面的,如果再相等就继续.... 所以比较这个动作的单一功能,可以写成一个check函数,方便操作也方便递归. #include<iostream> #include<cstring> using namespace std; #define MAX 30005 char str[MAX]; int

POJ 3623 Best Cow Line, Gold(模拟)

题意  给你一个字符序列   你每次可以从它的头部或尾部拿出一个字符组成一个新的字符序列   输出这样做能达到的最小的字符序列   每行最多输出80个字符(开始被这个坑了好久) 直接模拟就行  哪边小就选哪边  相等就往内看 #include<cstdio> #include<iostream> #include<string> using namespace std; const int N = 30010; int main() { char s[N][2]; in

poj3623 Best Cow Line, Gold(贪心)

题目链接: huangjing 思路: 选取字典序最小的串,那么值得考虑的是当两端出现相等时,继续比较,直到出现不同的结果.. 题目: Best Cow Line, Gold Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4574   Accepted: 1682 Description FJ is about to take his N (1 ≤ N ≤ 30,000) cows to the annual"Farme