题目大意
解题思路
很明显的一道贪心问题。
- 贪心策略: 每次选首位字典序最小的字符。如果首位字符相同,则比较第二个字符和倒数第二个字符,若仍然相同,则继续比较,直到不同。如果全部相同,则在首位中任意选一个。很明显,判断大小可以用递归完成
代码
#include<iostream>
using namespace std;
const int MAXN = 2000+5;
char str[MAXN];
bool is_lower(int s, int e)
{
if(str[s] != str[e])
return str[s] < str[e];
if(s > e)
return true;
return is_lower(s+1, e-1);
}
int main()
{
int n;
cin >> n;
for(int i=0; i<n; i++)
cin >> str[i];
int s, e;
s = 0; e = n-1;
int cnt = 1;
while(s <= e)
{
if(is_lower(s, e))
{
cout << str[s];
s++;
}
else
{
cout << str[e];
e--;
}
if(cnt % 80 == 0)
cout << endl;
cnt++;
}
cout << endl;
return 0;
}