C++ STL 全排列函数
C++ 全排列函数。。。一听名字就在<algorithm>中。。。
首先第一个说的是next_permutation:
#include <algorithm> bool next_permutation( iterator start, iterator end );
The next_permutation() function attempts to transform the given range of elements [start,end) into the next lexicographically greater permutation of elements. If it succeeds, it returns true, otherwise, it returns false.
从说明中可以看到 next_permutation 的返回值是布尔类型。
注意说明中说的前闭后开区间;
下面是我打的一段程序说明函数:
- #include <iostream>
- #include <algorithm>
- #include <string>
- #include <functional>
- using namespace std;
- int main()
- {
- string str;
- cin >> str;
- long long ans=;
- sort(str.begin(), str.end(),less<char>());//升序排列
- cout << str << endl;
- while (next_permutation(str.begin(), str.end()))
- {
- cout << str << endl;
- ans++;
- }
- cout<<"the next string:"<<str<<endl;
- cout<<"total forms:"<<ans<<endl;
- return ;
- }
- //在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和prev_permutation求排列组合很方便,
- 但是要记得包含头文件#include <algorithm>。
- 虽然最后一个排列没有下一个排列,用next_permutation会返回false,
- 但是使用了这个方法后,序列会变成字典序列的第一个,如cba变成abc。prev_permutation同理。
- */
代码+说明
2.prev_permutation
上一个代码内讲到了它
这就不细说了:
- //prev_permutation
- #include <iostream>
- #include <algorithm>
- #include <string>
- #include <functional>
- using namespace std;
- int main()
- {
- string str;
- cin >> str;
- long long ans=;
- sort(str.begin(), str.end(),greater<char>());//降序排列
- cout << str << endl;
- while (prev_permutation(str.begin(), str.end()))
- {
- cout << str << endl;
- ans++;
- }
- cout<<"the next string:"<<str<<endl;
- cout<<"total forms:"<<ans<<endl;
- return ;
- }
- //在STL中,除了prev_permutation外,还有一个函数next_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}没有下一个元素。
- /*
- 33 用next_permutation和prev_permutation求排列组合很方便,
- 34 但是要记得包含头文件#include <algorithm>。
- 35 虽然第一个排列没有上一个排列,用prev_permutation会返回false,
- 36 但是使用了这个方法后,序列会变成字典序列的最后一个,如abc变成cba。next_permutation同理。
- 37 */
- 程序+说明
直接上代码
部分转载自:http://leonard1853.iteye.com/blog/1450085
以上完毕。
C++ STL 全排列函数的更多相关文章
- 2017年上海金马五校程序设计竞赛:Problem A : STEED Cards (STL全排列函数)
Description Corn does not participate the STEED contest, but he is interested in the word "STEE ...
- C++ STL 全排列函数详解
一.概念 从n个不同元素中任取m(m≤n)个元素,按照一定的顺序排列起来,叫做从n个不同元素中取出m个元素的一个排列.当m=n时所有的排列情况叫全排列.如果这组数有n个,那么全排列数为n!个. 比如a ...
- POJ1833 排列 调用全排列函数 用copy函数节省时间 即使用了ios同步代码scanf还是比较快
排列 Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21268 Accepted: 8049 Description 题 ...
- next_permutation() 全排列函数
next_permutation() 全排列函数 这个函数是STL自带的,用来求出该数组的下一个排列组合 相当之好用,懒人专用 适用于不想自己用dfs写全排列的同学(结尾附上dfs代码) 洛谷oj可去 ...
- ACM题目————STL + 全排列
今天碰到一个函数,感觉挺好用的,全排列函数 next_permutation! 求全排列的函数,基本上与自己写的DFS时间复杂度差不多,毕竟是标准库.(2018-1-4 添加) 话不多说,直接上题. ...
- 分享stl sort函数坑点导致coredump问题
在<Effective STL> 的条款21中就有讨论:永远让比较函数对相同元素返回false! 也就是说在实现stl sort函数自定义比较器时,一定要满足这种严格弱序化的问题.
- STL - next_permutation 全排列函数
学习: http://blog.sina.com.cn/s/blog_9f7ea4390101101u.html http://blog.csdn.net/ac_gibson/article/deta ...
- C++ 全排列函数 nyoj 366
C++ STL中提供了std::next_permutation与std::prev_permutation可以获取数字或者是字符的全排列,其中std::next_permutation提供升序.st ...
- C++ 全排列函数 std::next_permutation与std::prev_permutation
C++ STL中提供了std::next_permutation与std::prev_permutation可以获取数字或者是字符的全排列,其中std::next_permutation提供升序.st ...
随机推荐
- rails里面添加妹子ui
妹子ui看起来很不错,以为在rails里面添加自定义的css和js和平时一样,结果可想而知,不过弄完以后发现还是比较简单的,这里记录一下 妹子ui需要加载的css和js如下 http://cdn.am ...
- css怎么设置2个div同行,第一个固定宽度,第二个占满剩余的部分
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- python多标签分类模版
from sklearn.multioutput import MultiOutputClassifier from sklearn.ensemble import RandomForestClass ...
- kafka 常用命令
Kafka 解压,进入kafka目录下的bin目录 启动zk nohup ./zookeeper-server-start.sh ../config/zookeeper.properties & ...
- Java GC日志查看
Java GC类型 Java中的GC有哪几种类型? 参数 描述 UseSerialGC 虚拟机运行在Client模式的默认值,打开此开关参数后, 使用Serial+Serial Old收集器组合进行垃 ...
- 2017百度之星初赛B-1002(HDU-6115)
一.思路 这题“看似”比较难搞的一点是,一个节点上有多个办公室,这怎么求?其他的,求树中任意两个节点的距离(注意:没有最远或最最进这一说法,因为树上任意两个节点之间有且仅有一条路径.不然就有回路了,对 ...
- node的socket.io的之事件篇
socket.io类库不但可以相互发送消息,而且还可以通过socket端口对象的emit方法互相发送事件. emit在之前的事件上说过现在一句话带过:emit是用来手动触发事件的. socket.em ...
- node的超时timeout
如果在指定的时间内服务器没有做出响应(可能是网络间连接出现问题,也可能是因为服务器故障或网络防火墙阻止了客户端与服务器的连接),则响应超时,同时触发http.ServerResponse对象的time ...
- buffer cache 深度解析
本文首先详细介绍了oracle中buffer cache的概念以及所包含的内存结构.然后结合各个后台进程(包括DBWRn.CKPT.LGWR等)深入介绍了oracle对于buffer cache的管理 ...
- 解决 service iptables start 无法启动的问题
解决方式: iptables -F // 初始化iptables. service iptables save // 保存 service iptables restart // 重启