POJ 3617 - Best Cow Line(贪心)

来源:互联网 发布:sql 数值转化为字符串 编辑:程序博客网 时间:2024/06/08 06:55

题目:

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
原创粉丝点击