题目链接: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;
}