挑战程序设计竞赛: 字典序最小问题

题目大意

在这里插入图片描述
在这里插入图片描述

解题思路

很明显的一道贪心问题。

  • 贪心策略: 每次选首位字典序最小的字符。如果首位字符相同,则比较第二个字符和倒数第二个字符,若仍然相同,则继续比较,直到不同。如果全部相同,则在首位中任意选一个。很明显,判断大小可以用递归完成

代码

#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;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值