uva146

用库函数next_permutation(p,p+n)做的,超时了。

看了下不用库函数做的思路也挺简单的,从最后一个字母向前找,找到第一个比最后字母小的,交换位置,然后把后面字母增序排列。

上一个超时的代码,很简洁漂亮

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>

using namespace std;

int main()
{
	char p[60],dest[60];
	while (cin>>p&&p[0]!='#')
	{
		int n=strlen(p);
		strcpy(dest,p);
		sort(p,p+n);
		while (strcmp(dest,p)!=0)
			next_permutation(p,p+n);
		if (next_permutation(p,p+n))
		{
			for (int i=0;i<n;i++) cout<<p[i];
			cout<<endl;
		}
		else cout<<"No Successor"<<endl;
	}
	
	return 0;
}


你可能感兴趣的:(ACM,uva,下一个排列)