poj3617(字典序最小问题)

#include 
#include 
#include 
using namespace std;
int n,m;
char S[2100],P[2100];

int main()
{
#ifdef xxz
    freopen("in.txt","r",stdin);
#endif // xxz
    ios::sync_with_stdio(false);
    cin.tie(0);
    int sum = 0;
    while(cin>>n)
    {
        for(int i = 0; i < n; i++) cin>>S[i];

        int a = 0, b = n-1,cent = 0;
        while(a <= b)
        {
            bool left = false;//将从左起和由起的字符串比较

            for(int i = 0; a + i <= b; i++)
            {
                if(S[a+i] < S[b-i])
                {
                    left = true;
                    break;
                }
                else if(S[a+i] > S[b-i])
                {
                    left = false;
                    break;
                }
            }

            if(left) P[cent++] = S[a++];
            else P[cent++] = S[b--];
        }
        for(int i = 0; i < n; i++)
        {
            sum += 1;
            cout<

你可能感兴趣的:(贪心)