Best Cow Line(POJ No.3617)

问题:

  链接:http://poj.org/problem?id=3617

思路:

  按照字典序比较S和将S反转后的字符串S'

  如果S较小,就从S的开头取出一个字符,加到T的末尾(更新下标值)

  如果S’较小,从S’的末尾取出一个字符,加到T的末尾(更新下标值)

代码:

  

# include <iostream>
# include <string>

using namespace std;

int N;
char str[2001];
char toStr[2001];

int main()
{
    cin>>N;
    for(int i = 0; i < N; i++)
    {
        cin>>str[i];
    }

    int left = 0;
    int right = N - 1;
    int icount = 0;

    while(left < right)
    {
        int leftVar = left;
        int rightVar = right;

        while(str[leftVar] == str[rightVar])
        {
            leftVar++;
            rightVar--;
        }

        if (str[leftVar] < str[rightVar])
        {
            toStr[icount++] = str[left];
            left++;
        }
        else
        {
            toStr[icount++] = str[right];
            right--;
        }
    }
    toStr[icount++] = str[left];

    for(int i = 0; i < icount; i++)
    {
        cout<<toStr[i];
        if( (i+1) % 80 == 0 && i < icount - 1)
            cout<<endl;
    }
    
    return 0;
}

 

posted @ 2013-11-05 19:52  viczzx  阅读(235)  评论(0编辑  收藏  举报