uva146--求下一个排列

89 阅读1分钟

题意:

给出一个字符串,求其字典序的下一个排列。

思路:

本来想用生成可重集排列的代码,无意间翻到了函数next_permutation,这个专门用来求字典序下一个排列的函数,简直就是为这道题目而准备的,第一次用这么短的代码交题目。要想学的好,还是得多利用工具。

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
char a[55];
int main(){
	while(scanf("%s",a)&&a[0]!='#'){
		int n=strlen(a);
		if(next_permutation(a,a+n)) cout<<a<<endl;
		else cout<<"No Successor"<<endl;
	}
	return 0;
}  


\

\