【STL】next_permutation的原理和使用
1、碰到next_permutation(permutation:序列的意思)
今天在TC上碰到一道简单题(SRM531 - Division Two - Level One),是求给定数组不按升序排列的最小字典序列(Sequence of numbers A is lexicographically smaller than B if A contains a smaller number on the first position on which they differ)。
解法很简单,就是将数组排序(升序),然后从尾到头找到第一个可以交换的位置(因为可能有重复的数字)。
最后看别人的解法,排序后,用了STL中的一个函数next_permutaion,直接求到第一个不按升序排列的序列。
2、next_permutation实现原理
在《STL源码解析》中找到了这个函数,在此也简单叙述一下原理:
在STL中,除了next_permutation外,还有一个函数prev_permutation,两者都是用来计算排列组合的函数。前者是求出下一个排列组合,而后者是求出上一个排列组合。所谓“下一个”和“上一个”,书中举了一个简单的例子:对序列 {a, b, c},每一个元素都比后面的小,按照字典序列,固定a之后,a比bc都小,c比b大,它的下一个序列即为{a, c, b},而{a, c, b}的上一个序列即为{a, b, c},同理可以推出所有的六个序列为:{a, b, c}、{a, c, b}、{b, a, c}、{b, c, a}、{c, a, b}、{c, b, a},其中{a, b, c}没有上一个元素,{c, b, a}没有下一个元素。
next_permutation的函数原型如下:
template<class BidirectionalIterator>
bool next_permutation{
BidirectionalIterator _First,
BidirectionalIterator _Last
};
template<class BidirectionalIterator, class BinaryPredicate>
bool next_permutation{
BidirectionalIterator _First,
BidirectionalIterator _Last,
BinaryPredicate _Comp
};
对于第二个重载函数的第三个参数,默认比较顺序为小于。如果找到下一个序列,则返回真,否则返回假。
函数实现原理如下:
在当前序列中,从尾端往前寻找两个相邻元素,前一个记为*i,后一个记为*ii,并且满足*i < *ii。然后再从尾端寻找另一个元素*j,如果满足*i < *j,即将第i个元素与第j个元素对调,并将第ii个元素之后(包括ii)的所有元素颠倒排序,即求出下一个序列了。
代码实现如下:
template<class BidirectionalIterator>
bool next_permutation(
BidirectionalIterator first,
BidirectionalIterator last
)
{
if(first == last)
return false; //空序列 BidirectionalIterator i = first;
++i;
if(i == last)
return false; //一个元素,没有下一个序列了 i = last;
--i; for(;;) {
BidirectionalIterator ii = i;
--i;
if(*i < *ii) {
BidirectionalIterator j = lase;
while(!(*i < *--j)); iter_swap(i, j);
reverse(ii, last);
return true;
} if(i == first) {
reverse(first, last); //全逆向,即为最小字典序列,如cba变为abc
return false;
}
} }
prev_permutation实现类似,就是反向查找
3、使用next_permutation
思考问题,序列{a, d, c, e, b}的下一个序列是什么呢?请利用前面的分析推出答案,并用代码验证。
我这里分别用数组和vector来表示序列,用next_permutation得到下一个序列(编译环境:Dev-C++):
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector> using namespace std; void TestArray()
{
char chs[] = {'a', 'd', 'c', 'e', 'b'};
int count = sizeof(chs)/sizeof(char); next_permutation(chs+, chs + count); printf("TestArray:\n");
for(int i = ; i < count; i++) {
printf("%c\t", chs[i]);
} printf("\n");
} void TestVector()
{
char chs[] = {'a', 'd', 'c', 'e', 'b'};
int count = sizeof(chs)/sizeof(char);
vector<char> vChs(chs, chs + count); next_permutation(vChs.begin(), vChs.end()); printf("TestVector:\n");
vector<char>::iterator itr;
for(itr = vChs.begin(); itr != vChs.end(); itr++) {
printf("%c\t", *itr);
}
printf("\n");
} int main(int argc, char *argv[])
{
TestArray();
printf("\n");
TestVector(); system("PAUSE");
return EXIT_SUCCESS;
}
运行结果:

4、小结
用next_permutation和prev_permutation求排列组合很方便,但是要记得包含头文件#include <algorithm>。
虽然最后一个排列没有下一个排列,用next_permutation会返回false,但是使用了这个方法后,序列会变成字典序列的第一个,如cba变成abc。prev_permutation同理。
【STL】next_permutation的原理和使用的更多相关文章
- 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 两 ...
- 打印全排列和stl::next_permutation
打印全排列是个有点挑战的编程问题.STL提供了stl::next_permutation完美的攻克了这个问题. 可是,假设不看stl::next_permutation,尝试自己解决,怎么做? 非常自 ...
- C++ STL, next_permutation用法。
next_permutation 将按字母表顺序生成给定序列的下一个较大的序列,直到整个序列为 #include"iostream" #include"algorithm ...
- STL next_permutation和prev_permutation函数
利用next_permutation实现全排列升序输出,从尾到头找到第一个可以交换的位置, 直接求到第一个不按升序排列的序列. #include <iostream> #include & ...
- 智能指针(一):STL auto_ptr实现原理
智能指针实际上是一个类(class),里面封装了一个指针.它的用处是啥呢? 指针与内存 说到指针自然涉及到内存.我们如果是在堆栈(stack)中分配了内存,用完后由系统去负责释放.如果是自定义类型,就 ...
- STL next_permutation()
用法 字典序全排列 可以发现函数next_permutation()是按照字典序产生排列的,并且是从数组中当前的字典序开始依次增大直至到最大字典序. 代码 #include<iostream&g ...
- hdu1716排列2(stl:next_permutation+优先队列)
排列2 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- STL - next_permutation 全排列函数
学习: http://blog.sina.com.cn/s/blog_9f7ea4390101101u.html http://blog.csdn.net/ac_gibson/article/deta ...
随机推荐
- jQuery属性选择器.attr()和.prop()两种方法
在判断表单单选框是否被选中时,通常会想到使用$('#checkbox').attr('checked')来判断,但在一些情况下,你会发现这种方法并不管用,得到的是undefined. 原来jQuery ...
- Reading Famous blog to prevent me wasting time on blind wandering
I can`t help surfing the useless bbs and some other kind of SNS. The time I begin to do it, it costs ...
- 【bzoj2243】[SDOI2011]染色
题目描述 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的颜色段数量(连续相同颜色被认为是同一段),如"11 ...
- bootstrap实现弹出窗口
bootstrap使用modal-dialog实现弹对话框. 一个对话框包含3部分: 对话框头部 modal-header 对话框内容体 modal-body 对话框底部 modal-footer 如 ...
- mongodb university week4
1.index Creation,background 如果在foreground运行index,会阻塞其他writer,如果background运行,会比较慢,但不会阻塞其他writer,可以并发写 ...
- 目前主流的Android定位有如下几种:
1.通过GPS模块 GPS方式准确度是最高的,但是它的缺点也非常明显:1,比较耗电:2,绝大部分用户默认不开启GPS模块:3,从GPS模块启动到获取第一次定位数据,可能需要比较长的时间:4,室内几乎无 ...
- linux 下 修改mysql账号密码
1.root用户登录到mysql数据库代码示例:/usr/local/mysql/bin/mysql -u root -p (输入密码进入mysql)2.进入mysql,输入:代码示例:use mys ...
- 揭露QPS增高后的秘密
导读 很多人在实际的开发中害怕系统的QPS增高,因为觉得QPS太高会导致系统挂掉;基于这种心理会想着尽量的降低系统的请求量,甚至有人会将很多处理放置到服务中来处理,这样外部发一起请求,服务就把所有的业 ...
- 霸气!Nginx 中缓存静态文件秘籍
导读 这篇教程说明你应该怎样配置 nginx.设置 HTTP 头部过期时间,用 Cache-Control 中的 max-age 标记为静态文件(比如图片. CSS 和 Javascript 文件)设 ...
- HTML5 localStorage 的使用
在前端开发中,我们会常遇到要在两个甚至多个html之间通信,我们可以在url中添加参数,但是当要传递的数据量较大较多时呢,不妨试试html5 的localStorage吧. 1) 检测你的浏览器是 ...