AP-Week8 ID Codes

题目链接:Fudan Algorithm Practice (2) - Virtual Judge

//https://vjudge.net/contest/466331#problem/A
//2021-11-05 17:40-19.18

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
int main(){
    char s[60];
    while(~scanf("%s", s) && s[0] != '#'){
        getchar();
        if( s == "#")
            break;

        //找最后的升序
        int last_up = strlen(s) - 1;
        while(last_up > 0 && s[last_up] <= s[last_up-1])
            last_up--;
        if(strlen(s) <=1 || last_up==0){
            printf("No Successor\n");
            continue;
        }
        //printf("last_up:%c\n", s[last_up - 1]);


        //比最后升序开头大的最后一个字母并和开头互换
        int last_lager = last_up;
        for(int i=last_up; i < strlen(s); i++)
            if(s[i] > s[last_up - 1])
                last_lager = i;
        //printf("last_lager:%c\n", s[last_lager]);
        swap(s[last_lager], s[last_up - 1]);


        //重排升序开头后所有字符
        sort(s+last_up, s+strlen(s));
        printf("%s\n", s);
    }
    return 0;
}












评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值