ACM_程序设计竞赛:贪心算法:字典最小序

#include <stdio.h>
#include <tchar.h>
#include <queue>
#include <iostream>

using namespace std;

const int N = 8;
char chs[N+1] = "ADHCACBD";

char* solve(char chs[])
{
    int start = 0, end = N - 1;
    bool isLeft = false;
    char dest[N];
    while(start <= end) {
        for(int i = 0; start + i < end; i ++)
        {
            if(chs[start + i] < chs[end - i]) 
            {
                isLeft = true;
                break;
            }

            else if(chs[start + i] > chs[end -i])
            {
                isLeft = false;
                break;
            }
            else 
                continue;
        }
        if(isLeft)
        {
            dest[N-(end - start + 1)] = chs[start ++];
            //putchar(chs[start ++]);
        }
        else
        {
            dest[N-(end - start + 1)] = chs[end --];
            //putchar(chs[end --]);
        }
    }

    return dest;
}

int main() {

    char* result = solve(chs);


    for(int i=0; i<N; i++) 
    {
        putchar(result[i]);
    }
    cout<<endl;
    return 0;
}

你可能感兴趣的:(ACM_程序设计竞赛:贪心算法:字典最小序)