51nod 1384 全排列(next_permutation)
2016-09-09 10:02
281 查看
51nod 1384 全排列
一个牛B函数的应用,它叫next_permutation函数
next_permutation()函数功能是输出所有比当前排列大的排列,顺序是从小到大。
ac代码
next_permutation函数的原理如下:
在当前序列中,从尾端向前寻找两个相邻元素,前一个记为 * i ,后一个记为 * t,并且满足 * i < * t。然后再从尾端寻找另一个元素 * j,如果满足 * i < * j,即将第i个元素与第j个元素对调,并将第t个元素之后(包括t)的所有元素颠倒排序,即求出下一个序列了。
(摘自http://blog.csdn.net/acdreamers/article/details/8544505)
自己查证:
平均复杂度即为O(n)
一个牛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)
相关文章推荐
- 51Nod 1384 全排列(next-permutation)
- 51nod 1384 全排列 (dfs/next_permutation)
- 【51nod】1384 全排列 next_permutation
- 51nod 1384 全排列(next_permutation函数)
- 全排列(STL next_permutation)
- 排列函数 prev_permutation和next_permutation的使用
- nyoj19(排列组合next_permutation(s.begin(),s.end()))
- 全排列生成算法:next_permutation
- 全排列生成算法:next_permutation
- 51Nod 1384 全排列
- 求全排列 D的小L 函数next_permutation ()
- stl next_permutation的全排列原理及实现样例
- 51nod 1384 全排列
- POj-2718 Smallest Difference--全排列next_permutation()的应用文章标题
- 全排列函数next_permutation
- HDU 1716 排列2 (stl,next_permutation)
- 51Nod-1384 全排列(回溯)
- stl全排列 qsort+next_permutation(a,a+n) -->全排列
- 51Nod-1384-全排列
- NYOJ--19--next_permutation()--擅长排列的小明