You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
# include <bits/stdc++.h>
using namespace std ;
/*
只能交换一次,且要保证字典序最小,思路肯定就是找到字符串后面最小的那个字符,和前面的交换
交换的原则有两点:
1.越前面越好
2.只能与比我们找到的字符要大的进行交换
*/
int main ( ) {
string s ;
cin > > s ;
int i ;
for ( i = 0 ; i < s . size ( ) - 1 ; i + + )
if ( s [ i ] > = s [ i + 1 ] ) break ;
// 说明严格递增, 如abcdefg,但又必须交换一次,所以直接交换最后两个,可以保证最小
if ( i = = s . size ( ) - 1 ) {
swap ( s [ i ] , s [ i - 1 ] ) ;
cout < < s < < endl ;
} else { // 说明i前面严格递增, 后面不一定, 如abcdcdac,i=3,s[6]=d
int mi = i ;
for ( ; i < s . size ( ) ; i + + ) // 找到后面最小的字母, i=6,s[6]=a
if ( s [ mi ] > = s [ i ] ) mi = i ;
for ( i = 0 ; i < mi ; i + + ) // 找到前面递增序列第一个比后面最小字母大的那个字母交换即可
if ( s [ i ] > s [ mi ] ) break ;
swap ( s [ mi ] , s [ i ] ) ;
cout < < s < < endl ;
}
return 0 ;
}