1. 程式人生 > >POJ3617:Best Cow Line (貪心&&後綴數組)

POJ3617:Best Cow Line (貪心&&後綴數組)

字符 com bsp ring initials finished LG ntb ++

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

----------------因為準備金馬賽,所以一直在看書和博客(希望能贏一把)。之前節奏太快了,應該多思考鞏固,所以減少了寫代碼的時間---------------

題意:給定一個字符串S,現在把這個S變成一個字符串T:每次從S的頭或尾取一個字符添加到T裏,要求其字典序最小。

思路:顯然是貪心求解,如果S頭不等於S尾,那麽取小的一邊加到T裏。

但是如果相同,不能任取,如BCAB,如果任取B,可能變成了BABC或者BBAC,後者顯然不合標準。

應該一直比較,知道不相同或者全部都相同:不相同就取小的那一邊,全部相同就隨意取。

比較的過程可以暴力,也可以後綴數組。

普通貪心代碼:

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=2010;
char c[maxn];
bool Left(int L,int R){
    while(L<=R){
        if(c[L]!=c[R]) 
           return c[L]<c[R];
        L++; R--;
    }
    return true;
}
int main()
{
    int N,cnt;
    scanf("%d",&N);
    for(int i=1;i<=N;i++) cin>>c[i];
    int L=1,R=N; cnt=0;
    while(L<=R){
        if(Left(L,R)) cout<<c[L++];
        else cout<<c[R--];
        cnt++;
        if(cnt==80) printf("\n"),cnt=0;
    }
    return 0;
}

後綴數組代碼:

#include<cmath>
#include<math.h>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=4010;
int N;char c[maxn];
struct SA
{
    int Rank[maxn],A[maxn],B[maxn],cntA[maxn],cntB[maxn],sa[maxn],tsa[maxn],ht[maxn],Min[maxn][22];
    void sort(){
       for(int i=0;i<=128;i++) cntA[i]=0;
       for(int i=1;i<=N;i++) cntA[c[i]]++;
       for(int i=1;i<=128;i++) cntA[i]+=cntA[i-1];
       for(int i=N;i>=1;i--) sa[cntA[c[i]]--]=i;
       Rank[sa[1]]=1;
       for(int i=2;i<=N;i++) Rank[sa[i]]=Rank[sa[i-1]]+(c[sa[i]]==c[sa[i-1]]?0:1);
       for(int l=1;Rank[sa[N]]<N;l<<=1){
              for(int i=0;i<=N;i++) cntA[i]=cntB[i]=0;
              for(int i=1;i<=N;i++) cntA[A[i]=Rank[i]]++;
              for(int i=1;i<=N;i++) cntB[B[i]=i+l<=N?Rank[i+l]:0]++;
              for(int i=1;i<=N;i++) cntA[i]+=cntA[i-1],cntB[i]+=cntB[i-1];
              for(int i=N;i>=1;i--) tsa[cntB[B[i]]--]=i;
              for(int i=N;i>=1;i--) sa[cntA[A[tsa[i]]]--]=tsa[i];
              Rank[sa[1]]=1;
              for(int i=2;i<=N;i++) Rank[sa[i]]=Rank[sa[i-1]]+(A[sa[i]]==A[sa[i-1]]&&B[sa[i]]==B[sa[i-1]]?0:1);
       }
    }
}S;
int main()
{
    int n,cnt;
    scanf("%d",&n);    N=n+n+1;
    for(int i=1;i<=n;i++) cin>>c[i];
    for(int i=n;i>=1;i--) c[N-i]=c[i];
    int L=1,R=n; cnt=0; S.sort();
    while(L<=R){
        if(S.Rank[L]<=S.Rank[N-R]) cout<<c[L++];
        else cout<<c[R--];
        cnt++;
        if(cnt==80) printf("\n"),cnt=0;
    }
    return 0;
}

POJ3617:Best Cow Line (貪心&&後綴數組)