Best Cow Line(POJ 3617) 貪心

來自《挑戰程序設計競賽》

1.題目原文

Best Cow Line
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 19765   Accepted: 5457

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

Source


給定一個長度爲N的字符串S,要構造一個長度爲N的字符串T。起初,T是一個空串,隨後反覆進行如下操作:
1.從S的頭部刪除一個字符,加到T的尾部。
2.從S的尾部刪除一個字符,加到T的尾部。
目標是構造字典序儘可能小的字符串T。

2.解題思路

字典序比較類的問題經常能用到貪心算法。
我們可以試一下,如下貪心算法:不斷取S的頭部和尾部中較小的字符放到T的尾部。
這個算法幾乎是正確的,但是對於S的開頭和末尾字符相同的情況沒有定義。在這種情況下,我們想儘可能早點使用較小的字符,就需要比較下一個字符,下一個字符也可能相同。因此,就有如下算法:
按照字典序比較S和S反轉後的字符串S'。如果S較小,就把S開頭的字符加到T末尾。如果S’較小,就把S末尾的字符加到T末尾。如果相同,加入哪一個都可以。

3.AC代碼

#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<string>
#include<set>
#include<vector>
#include<cmath>
#include<bitset>
#include<stack>
#include<sstream>
#include<deque>
#include<utility>
using namespace std;
const int maxn=2005;

int n;
char s[maxn];

void solve()
{
    int a=0,b=n-1;
    int sum=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;
            }
        }
        sum++;
        if(left) cout<<s[a++];
        else cout<<s[b--];
        if(sum%80==0){
            cout<<endl;
        }
    }
}
int main()
{
    while(scanf("%d",&n)!=EOF){
        for(int i=0;i<n;i++){
            cin>>s[i];
        }
        solve();
    }
    return 0;
}


發佈了119 篇原創文章 · 獲贊 16 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章