/*
贪婪算法的一道水题
*/
#include <iostream>
#include <stdio.h>
#define MAXN 2011
char s[MAXN];
using namespace std;
int main(void)
{
int N;
while (scanf_s("%d", &N) != EOF)
{
for (int i = 0; i < N; i++)
cin >> s[i];
int head = 0;
int tail = N - 1;
int count = 0;
while (head < tail)
{
if (s[head] < s[tail])
printf("%c", s[head++]);
else if (s[head] > s[tail])
printf("%c", s[tail--]);
else
{
int temp_head = head;
int temp_tail = tail;
//这里是为了确保整个字符串的字典序最小,所以如果头尾相同就看里面谁更小吧
while (s[++temp_head] == s[--temp_tail]);
if (s[temp_head] < s[temp_tail])
printf("%c", s[head++]);
else if (s[temp_head] > s[temp_tail])
printf("%c", s[tail--]);
}
count++;//八十个字符以后记得换行,不然就等着PE吧!!!
if (count % 80 == 0)
putchar('\n');
}
putchar(s[head]);
putchar('\n');
}
return 0;
}
POJ - 3617 - Best Cow Line
最新推荐文章于 2020-09-17 19:15:15 发布