打印全排列和stl::next_permutation
打印全排列是个有点挑战的编程问题。STL提供了stl::next_permutation完美的攻克了这个问题。
可是,假设不看stl::next_permutation,尝试自己解决,怎么做?
非常自然地,使用递归的办法:
1. 单个元素的排列仅仅有1个。
2. 多个元素的排列能够转化为:
以每一个元素为排列的首个元素,加上其它元素的排列。
有了思路,就能够编码了。
第一个版本号:
prefix, int set[], int n)
i<n; ++i)
= set[i];
== 1)
" ;
shift set[0,i) to right by 1
j>=0; --j)
shift set[0,i) to left by 1
j<i; ++j)
測试:
myints, 4);
通过。
这样的方法的缺点是产生了大量的string对象。
怎么避免呢?
第二个版本号:
i<n; ++i)
= set[i];
== n-1)
it is possible use callback here instead of printing a permutation
j<n; ++j)
' ;
shift set[from,i) to right by 1
j>=from; --j)
shift set[from,i) to left by 1
j<i; ++j)
測试:
通过。
第二个版本号相比第一个版本号的还有一个改进是能够非常easy地改变成回调函数的形式,扩展函数的用途。而不不过打印排列。
似乎非常不错了。
可是和stl::next_permutation相比,以上的方案就太逊了。
1. stl::next_permutation支持部分排列,而不必是全排列。你能够从不论什么一个排列開始,能够随时退出next_permutation循环。
2. stl::next_permutation支持多重集的排列。比如:
= {1,2,2,2};
' << myints[1] << ' ' <<
myints[2] << ' ' << myints[3]
<< '\n';
std::next_permutation(myints,myints+4) );
输出:
1 2 2 2
2 1 2 2
2 2 1 2
2 2 2 1
没有反复的排列。
stl::next_permutation这么强大,非常值得看看它到底是怎么实现的。
_First, _BidIt _Last)
and test for pure ascending, using operator<
== _Last || _First == --_Next)
; )
find rightmost element smaller than successor
*_Next1))
swap with rightmost element that's smaller, flip suffix
!_DEBUG_LT(*_Next, *--_Mid); )
== _First)
pure descending, flip all
_First, _BidIt _Last)
and test for pure ascending, using operator<
_CHECKED_BASE(_Last));
代码不长,但须要研究才干理解。
非常多算法都是这种。
这个算法能够概括为:
假设仅仅有零个或一个元素,返回false,表示回到全排列的起点。
否则。从右边開始。找到第一个不是递减的元素,即E(i) < E(i+1),从E(i+1)一直到E(n)都是不增的。
假设找到。从右边開始。找到大于E(i)的那个元素E(x)【一定会找到】,交换E(i)和E(x),然后把E[i+1, n]范围内的元素反转。
返回true。
假设找不到,把整个范围内的元素反转,返回false,表示回到全排列的起点。
为什么这个算法可行呢?看以下1 2 3 4的全排列。
能够非常easy地看到,
假设把每一个排列看成一个数,那么下一个排列大于上一个排列。
由上可知,第一个排列是最小排列【不减排列】。最后一个排列是最大排列【不增排列】。
最小排列和最大排列是反序的关系。
算法的关键:从E(i+1)一直到E(n)都是不增的。
这个特性说明,这一范围的元素的排列是一个最大排列,下一个排列必然是找到这一范围内大于这一范围的前一元素的元素,交换这两个元素,交换后E[i+1, n]仍为不增排列【最大排列】。反转之后,变成最小排列。这样处理后得到的排列正好是E[0,n]的下一个排列。
1 2 3 4
1 2 4 3
1 3 2 4
1 2
1 4 2 3
1 4 3 2
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 1 3
2 4 3 1
3 1 2 4
3 1 4 2
3 2 1 4
3 2 4 1
3 4 1 2
3 4 2 1
4 1 2 3
4 1 3 2
4 2 1 3
4 2 3 1
4 3 1 2
4 3 2 1
打印全排列和stl::next_permutation的更多相关文章
- STL - next_permutation 全排列函数
学习: http://blog.sina.com.cn/s/blog_9f7ea4390101101u.html http://blog.csdn.net/ac_gibson/article/deta ...
- STL next_permutation(a,a+n) 生成一个序列的全排列。满足可重集。
/** 题目: 链接: 题意: 思路: */ #include <iostream> #include <cstdio> #include <vector> #in ...
- STL next_permutation 全排列
调用方法: ]={,,,}; )){ ;i<;i++) printf("%d ",arr[i]); puts(""); } 测试效果: 注:可以看到1 2 ...
- STL next_permutation和prev_permutation函数
利用next_permutation实现全排列升序输出,从尾到头找到第一个可以交换的位置, 直接求到第一个不按升序排列的序列. #include <iostream> #include & ...
- STL next_permutation 算法原理和自行实现
目标 STL中的next_permutation 函数和 prev_permutation 两个函数提供了对于一个特定排列P,求出其后一个排列P+1和前一个排列P-1的功能. 这里我们以next_pe ...
- STL next_permutation 算法原理和实现
转载自:https://www.cnblogs.com/luruiyuan/p/5914909.html 目标 STL中的next_permutation 函数和 prev_permutation 两 ...
- C++ 全排列函数 std::next_permutation与std::prev_permutation
C++ STL中提供了std::next_permutation与std::prev_permutation可以获取数字或者是字符的全排列,其中std::next_permutation提供升序.st ...
- 全排列(STL)
输入一个整数n,输出1~n的全排列(是不是很水) 在此记录stl做法 #include<bits/stdc++.h> using namespace std; ]; int main(){ ...
- C++中全排列算法函数next_permutation的使用方法
首先,先看对next_permutation函数的解释: http://www.cplusplus.com/reference/algorithm/next_permutation/?kw=next_ ...
随机推荐
- hdu 4603 Color the Tree
这道题细节真的非常多 首先能够想到a和b的最优策略一定是沿着a和b在树上的链走,走到某个点停止,然后再依次占据和这个点邻接的边 所以,解决这道题的过程例如以下: 预处理阶段: step 1:取随意一个 ...
- 2015.03.13,外语,读书笔记-《Word Power Made Easy》 10 “如何讨论交谈习惯”学习笔记 SESSION 26
1.a Spartan virtue 古斯巴达人中一位有名的Laconia国王,其言语比Vermonter(美国佛蒙特州人)还简洁.一个传说,马其顿菲利普国王(亚历山大的老爸)要进攻他们的都城,发了一 ...
- linux删除多行
光标放到行dd:删除所在行 光标放到行Ndd: 删除所在行下的N行
- C-结构体应用(10)
结构体是用来定义多种类型的复合类型,在 C语言中与类的区别在于结构体注重的是数据而类除了数据还包含函数,第2点区别在于结构体所声明的成员默认是"public"点.而类的默认是pri ...
- BZOJ 3720 树分块
借鉴了别人的代码-- //By SiriusRen #include <cmath> #include <cstdio> #include <cstring> #i ...
- linux中openssl生成证书和自签证书
1.首先要生成服务器端的私钥(key文件): 命令: openssl genrsa -des3 -out server.key 1024 运行时会提示输入密码,此密码用于加密key文件(参数des3便 ...
- 从DataTable高效率导出数据到Excel
首先从数据库读取数据到DataTable,这我就不提了,大家都明白.下面直接介绍如何从DataTable高效率导出数据到Excel中的方法,代码如下: using Microsoft.Office.I ...
- shell-6.环境变量配置文件
1. 2. 3. 4. 5. 6.
- Kattis - Babelfish
Babelfish You have just moved from Waterloo to a big city. The people here speak an incomprehensible ...
- 转载:常用 Git 命令清单
转载:常用 Git 命令清单 原文地址:http://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html 作者: 阮一峰 我每天使用 Git , ...