poj 3617 best cow line

/** poj 3617 best cow line
* 05.05/2014
*/
#include <cstdio>
#include <cstring>


#define MAXN 4000


int N;
char ca[MAXN];


void solve()
{    
    int starti = 0,endi = N-1;
    int count = 0;
    while (starti <= endi)
    {    
        for(int i =0; starti + i <= endi; i++)
        {
            if(ca[starti + i] < ca[endi-i])
            {
                putchar(ca[starti ++]);count++;
                break;
            }
            else if(ca[starti + i] > ca[endi-i])
            {
                putchar(ca[endi --]);count++;
                break;
                
            }
            else
            {
                if (starti + i == endi)
                {
                    if(ca[starti + i] <= ca[endi-i])
                    {
                        putchar(ca[starti ++]);count++;
                        break;
                    }
                    else if(ca[starti + i] > ca[endi-i])
                    {
                        putchar(ca[endi --]);count++;
                        break;
                
                    }
                }
            }

        }
        if(count == 80)
        {
            putchar('\n');
            count =0;
        }
    }
    putchar('\n');
    return;
}
int main()
{    
    scanf("%d",&N);
    for(int i = 0; i < N; i++)
        scanf("\n%c",&ca[i]);
    solve();

    return 0;
    
}

题目:

从字符串两端抽取,使之按字典升序排列

 

分析:

每次取字典序较小的那一端。贪心。

 

心得:

1 这题WA了很久,主要一开始没有把 大于 小于 和等于 的三种情况分开。等于情况要单独讨论,一直循环比较。

2 单行逐个读入字符的技巧get

posted @ 2014-05-06 16:28  Difei  阅读(220)  评论(0编辑  收藏  举报