zoukankan      html  css  js  c++  java
  • POJ3617:Best Cow Line (贪心&&后缀数组)

    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("
    "),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("
    "),cnt=0;
        }
        return 0;
    }
  • 相关阅读:
    Codeforces #533 div2 做题记录
    算法竞赛模板 打印素数表
    算法竞赛模板 计算组合数
    Educational Codeforces Round 34 (Rated for Div. 2)
    ACM感悟 -----sdust
    Codeforces Round #450 (Div. 2)
    EOJ Monthly 2017.12
    Wannafly挑战赛5
    二分图匹配
    今日颓废
  • 原文地址:https://www.cnblogs.com/hua-dong/p/8810069.html
Copyright © 2011-2022 走看看