STL中的next_permutation
给定一个数组a[N],求下一个数组.
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
.....
在STL中就有这个函数:
1.参数是(数组的第一个元素,数组的末尾),注意这是前闭后开区间,(a,a+n)
2.返回值是bool型,表示这个数组是不是最后一个元素.
3.这个函数不仅可以实现n个互异的数的全排列,也能够生成组合数.如1 2 3 3 4 4 5 6 6这样数组的全排列.
4.第一个数组是升序排列,最后一个数组是降序排列.
,,,};
do{
re(i, )cout << a[i] << " ";
puts("");
} ));
那么这个函数内部是如何实现的呢?
bool next(int*a, int sz){
;
&& a[i] <= a[i - ])i--;
)return false;
;
;
while (j > i)swap(a[j--], a[i++]);
i = pos + ;
while (a[i] <= a[pos])i++;
swap(a[i], a[pos]);
return true;
}
int main(){
,,,};
do{
re(i, )cout << a[i] << " ";
puts("");
} ));
;
}
输出一共12项.
STL中的next_permutation的更多相关文章
- 枚举所有排列-STL中的next_permutation
枚举排列的常见方法有两种 一种是递归枚举 另一种是STL中的next_permutation //枚举所有排列的另一种方法就是从字典序最小排列开始,不停的调用"求下一个排列"的过程 ...
- c++ STL中的next_permutation
default (1) template <class BidirectionalIterator> bool next_permutation (BidirectionalIterato ...
- c++中STL中的next_permutation函数基本用法
对于next_permutation函数是针对于排列组合问题的库函数,它的排序方式是按照字典的方式排列的·: 如以下代码对于next_permutation函数的初步解释: #include<c ...
- STL中的所有算法(70个)
STL中的所有算法(70个)----9种类型(略有修改by crazyhacking) 参考自: http://www.cppblog.com/mzty/archive/2007/03/14/1981 ...
- hdu1027 Ignatius and the Princess II (全排列 & STL中的神器)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=1027 Ignatiu ...
- STL中的算法
STL中的所有算法(70个) 参考自:http://www.cppblog.com/mzty/archive/2007/03/14/19819.htmlhttp://hi.baidu.com/ding ...
- STL::next_permutation();
next_permutation()可以按字典序生成所给区间的全排列. 在STL中,除了next_permutation()外,还有一个函数prev_permutation(),两者都是用来计算排列组 ...
- STL中的全排列实现
permutation: 在遇到全排列问题时,在数据量较小的情况下可以使用dfs的做法求得全排列,同时我们也知道在STL中存在函数next_permutation和prev_permutation,这 ...
- STL中的set容器的一点总结
1.关于set C++ STL 之所以得到广泛的赞誉,也被很多人使用,不只是提供了像vector, string, list等方便的容器,更重要的是STL封装了许多复杂的数据结构算法和大量常用数据结构 ...
随机推荐
- Mysql数据库笔记
出错记录: 1.mysql服务启动不了,进程意外终止 1067 错误显示:can not connect to mysql server on local hosts(1061) 解决方法 ...
- codevs 1002 搭桥
codevs 第一道题 先贴描述 1002 搭桥 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description ...
- SQL存储过程的调用及写法
调用函数: public class SqlProcess { ; public DataSet ReturnSet = null; public SqlDataAdapter adapter = n ...
- 转: 使用Eclipse的Working Set,界面清爽多了
from: http://iyuanbo.iteye.com/blog/1158136 使用Eclipse的Working Set,界面清爽多了 想必大家的Eclipse里也会有这么多得工程... ...
- python黑客编程之端口爆破
#coding:utf-8 from optparse import OptionParser import time,re,sys,threading,Queue import ftplib,soc ...
- poj[2392]space elevator
Description The cows are going to space! They plan to achieve orbit by building a sort of space elev ...
- 改变图片的颜色,UIImage改变颜色
定义 #import <UIKit/UIKit.h> @interface UIImage (ChangeImageColor) /** * 改变图片的颜色 * * @param tint ...
- DEDECMS之二 如何修改模板页
使用织梦系统最经常是为了仿站,那么模板应该怎么改? 这里主要谈谈关于比较常用的几个模板页 网站主页.列表页.内容页.栏目的调用 1.主页模板 常用组合方法:index.htm + head.htm + ...
- SQL SERVER 系统库查询
本文内容主要来自网络,如有错误请路过的大牛指点迷津. 1.sqlserver 数据库最大并发连接数 sqlserver的最大连接数虽然说是不限制,但实际的限制数量是32767,如果需要超出这个数量,一 ...
- eap
本文介绍了eap