此题就是寻找给定序列的下一个序列(按照字典序)
//
// main.cpp
// uva 146 - ID Codes
//
// Created by XD on 15/7/25.
// Copyright (c) 2015年 XD. All rights reserved.
//
//解题思想:比当前序列大的序列与当前序列的特征是,前面部分相同,后面不同,不同部分的长度为 从当前序列逆向的第一个字母(位置为i)的ascii码//小于位置为i+1的字母的assii码
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include<vector>
#include <string.h>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cstdio>
using namespace std ;
int main(int argc, const char * argv[]) {
char s[55] ;
while (scanf(" %s" , s)==1&&s[0]!='#') {
int len = (int ) strlen(s) ;
int start =-1 ;
for (int i = len-1; i >0; i--) {
if (s[i] <= s[i-1]) {
continue ;
}else{
start = i-1 ;
break ;
}
}
if (start ==-1) {
printf("No Successor\n") ;
}
else{
//使用stl函数来确定下一个序列是什么,然后拼接起来就可以了
char temp[55] ;
for (int i = 0; i < len - start; i++) {
temp[i] = s[i+start] ;
}
temp[len-start] = '\0' ;
next_permutation(temp, temp + len - start) ;
for (int i = 0 ; i < start; i++) {
printf("%c" , s[i]) ;
}
for (int i = start; i < len; i++) {
printf("%c" , temp[i- start]) ;
}
printf("\n") ;
}
}
return 0;
}