POJ 3617 - Best Cow Line(贪心)

题目:

FJ is about totake his N (1 ≤ N ≤ 2,000)cows to the annual"Farmer of the Year" competition. In this contestevery farmer arranges his cows in a line and herds them past the judges.

The contestorganizers adopted a new registration scheme this year: simply register theinitial letter of every cow in the order they will appear (i.e., If FJ takesBessie, Sylvia, and Dora in that order he just registers BSD). After theregistration phase ends, every group is judged in increasing lexicographicorder according to the string of the initials of the cows' names.

FJ is very busythis year and has to hurry back to his farm, so he wants to be judged as earlyas possible. He decides to rearrange his cows, who have already lined up,before registering them.

FJ marks alocation for a new line of the competing cows. He then proceeds to marshal thecows from the old line to the new one by repeatedly sending either the first orlast 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 initialorder of his cows, determine the least lexicographic string of initials he canmake this way.

题意:

     将字符串重新排列,排成最小字典序,只能从最前或最未取字符。

Thinking:

     将最前与最末字符进行比较,取小的,若相同,则比较里面一层的字符,直至比出大小,在小的一边取数。

    若相等,要用循环,写得我半死,结果是将--写成++了;还有最后输出格式忽略了i从0开始,还有n==0的情况!!

代码:

 

#include <iostream>
#include <cstdio>

using namespace std;
char s[2005], ans[2005];

int main()
{
//freopen("in", "r", stdin);
    int n;
    while(~scanf("%d", &n)) {
        for(int i = 0; i < n; ++i) {
            getchar();
            scanf("%c", &s[i]);
        }
        int i = 0, j = n - 1, k = 0;
        while(i <= j) {
            if(s[i] < s[j]) {
                ans[k++] = s[i];
                i++;
            }
            else if(s[i] > s[j]) {
                ans[k++] = s[j];
                j--;
            }
            else {
                int ii = i, jj = j;
//                while(1) {
//                    if(ii >= jj) {
//                        for(int t = i; t <= j; ++t) {
//                            ans[k++] = s[t];
//                        }
//                        i = ii+1;
//                        j = jj-1;
//                        break;
//                    }
//                    else if(s[ii] > s[jj]) {
//                        for(int t = j; t >= jj; --t) {
//                            ans[k++] = s[t];
//                        }
//                        j = jj-1;
//                        break;
//                    }
//                    else if(s[ii] < s[jj]) {
//                        for(int t = i; t <= ii; ++t) {
//                            ans[k++] = s[t];
//                        }
//                        i = ii+1;
//                        break;
//                    }
//                    ii++; jj--;
//                }
                while(ii <= jj && s[ii] == s[jj]) {
                    ii++;
                    jj--;
                }
                if(s[ii] < s[jj]) {
                    ans[k++] = s[i];
                    i++;
                }
                else if(s[ii] > s[jj]) {
                    ans[k++] = s[j];
                    j--;
                }
                else {
                    for(int t = i; t <= j; ++t) {
                        ans[k++] = s[t];
                    }
                    i = j + 1;
                }
            }
        }
        for(int i = 0; i <= n; ++i) {
            if(i % 80 == 0 && i != 0) {
                puts("");
                if(i == n) break;
            }
            else if(i == n) {
                printf("\n");
                break;
            }
            printf("%c", ans[i]);
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值