POJ 3617 Best Cow Line (贪心)

Best Cow Line
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12314   Accepted: 3579

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




比较容易理解的贪心,不过还是WA了很多发,刚开始只是单纯的想到了每次取最小的就行,相等就随便取,但是没考虑到可能相等之后是不相等的情况,就比如CABC,如果随便取一个C的话就错了,所以如果遇到相等的就要一直再往下接着比对。
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <string>
 5 #include <cctype>
 6 #include <cmath>
 7 #include <queue>
 8 #include <map>
 9 #include <cstdlib>
10 using    namespace    std;
11 
12 int    main(void)
13 {
14     int    n;
15     int    i,j,k,ii,jj;
16     char    s[2005];
17     char    ans[2005];
18 
19     while(scanf("%d",&n) != EOF)
20     {
21         for(i = 0;i < n;i ++)
22             scanf(" %c",&s[i]);
23         for(i = 0,j = n - 1,k = 0;i <= j;k ++)
24             if(s[i] < s[j])
25             {
26                 ans[k] = s[i];
27                 i ++;
28             }
29             else    if(s[i] > s[j])
30             {
31                 ans[k] = s[j];
32                 j --;
33             }
34             else
35             {
36                 for(ii = i,jj = j;ii < jj && s[ii] == s[jj];ii ++,jj --);
37                 if(s[ii] < s[jj])
38                 {
39                     ans[k] = s[i];
40                     i ++;
41                 }
42                 else
43                 {
44                     ans[k] = s[j];
45                     j --;
46                 }
47             }
48         for(i = 1;i <= k;i ++)
49         {
50             putchar(ans[i - 1]);
51             if(i % 80 == 0)
52                 puts("");
53         }
54         puts("");
55 
56     }
57 
58     return    0;
59 }
原文地址:https://www.cnblogs.com/xz816111/p/4461553.html