您的位置:首页 > 其它

51nod 1384 全排列(next_permutation)

2016-09-09 10:02 281 查看
51nod 1384 全排列

一个牛B函数的应用,它叫next_permutation函数

next_permutation()函数功能是输出所有比当前排列大的排列,顺序是从小到大。

ac代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
using namespace std;
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define N 510

int main()
{
string str;
cin >> str;
sort(str.begin(),str.end());
do
{
cout << str << endl;
}while(next_permutation(str.begin(),str.end()));
return 0;
}


next_permutation函数的原理如下:

在当前序列中,从尾端向前寻找两个相邻元素,前一个记为 * i ,后一个记为 * t,并且满足 * i < * t。然后再从尾端寻找另一个元素 * j,如果满足 * i < * j,即将第i个元素与第j个元素对调,并将第t个元素之后(包括t)的所有元素颠倒排序,即求出下一个序列了。

(摘自http://blog.csdn.net/acdreamers/article/details/8544505

自己查证:

平均复杂度即为O(n)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: