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 的返回值是布尔类型。

注意说明中说的前闭后开区间;

下面是我打的一段程序说明函数:

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <string>
  4. #include <functional>
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. string str;
  10. cin >> str;
  11. long long ans=;
  12. sort(str.begin(), str.end(),less<char>());//升序排列
  13. cout << str << endl;
  14. while (next_permutation(str.begin(), str.end()))
  15. {
  16. cout << str << endl;
  17. ans++;
  18. }
  19. cout<<"the next string:"<<str<<endl;
  20. cout<<"total forms:"<<ans<<endl;
  21. return ;
  22. }
  23. //在STL中,除了next_permutation外,还有一个函数prev_permutation,
  24. //两者都是用来计算排列组合的函数。前者是求出下一个排列组合,而后者
  25. //是求出上一个排列组合。所谓“下一个”和“上一个”,书中举了一个简单的例子:
  26. //对序列 {a, b, c},每一个元素都比后面的小,按照字典序列,固定a之后,a比bc都小,
  27. //c比b大,它的下一个序列即为{a, c, b},而{a, c, b}的上一个序列即为{a, b, c},
  28. //同理可以推出所有的六个序列为:{a, b, c}、{a, c, b}、{b, a, c}、{b, c, a}、{c, a, b}、{c, b, a},
  29. //其中{a, b, c}没有上一个元素,{c, b, a}没有下一个元素。
  30. /*
  31. 用next_permutation和prev_permutation求排列组合很方便,
  32. 但是要记得包含头文件#include <algorithm>。
  33. 虽然最后一个排列没有下一个排列,用next_permutation会返回false,
  34. 但是使用了这个方法后,序列会变成字典序列的第一个,如cba变成abc。prev_permutation同理。
  35. */

代码+说明

2.prev_permutation

上一个代码内讲到了它

这就不细说了:

  1. //prev_permutation
  2.  
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <string>
  6. #include <functional>
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11. string str;
  12. cin >> str;
  13. long long ans=;
  14. sort(str.begin(), str.end(),greater<char>());//降序排列
  15. cout << str << endl;
  16. while (prev_permutation(str.begin(), str.end()))
  17. {
  18. cout << str << endl;
  19. ans++;
  20. }
  21. cout<<"the next string:"<<str<<endl;
  22. cout<<"total forms:"<<ans<<endl;
  23. return ;
  24. }
  25. //在STL中,除了prev_permutation外,还有一个函数next_permutation,
  26. //两者都是用来计算排列组合的函数。前者是求出下一个排列组合,而后者
  27. //是求出上一个排列组合。所谓“下一个”和“上一个”,书中举了一个简单的例子:
  28. //对序列 {a, b, c},每一个元素都比后面的小,按照字典序列,固定a之后,a比bc都小,
  29. //c比b大,它的下一个序列即为{a, c, b},而{a, c, b}的上一个序列即为{a, b, c},
  30. //同理可以推出所有的六个序列为:{a, b, c}、{a, c, b}、{b, a, c}、{b, c, a}、{c, a, b}、{c, b, a},
  31. //其中{a, b, c}没有上一个元素,{c, b, a}没有下一个元素。
  32. /*
  33. 33 用next_permutation和prev_permutation求排列组合很方便,
  34. 34 但是要记得包含头文件#include <algorithm>。
  35. 35 虽然第一个排列没有上一个排列,用prev_permutation会返回false,
  36. 36 但是使用了这个方法后,序列会变成字典序列的最后一个,如abc变成cba。next_permutation同理。
  37. 37 */
  38. 程序+说明

直接上代码

部分转载自:http://leonard1853.iteye.com/blog/1450085

以上完毕。

C++ STL 全排列函数的更多相关文章

  1. 2017年上海金马五校程序设计竞赛:Problem A : STEED Cards (STL全排列函数)

    Description Corn does not participate the STEED contest, but he is interested in the word "STEE ...

  2. C++ STL 全排列函数详解

    一.概念 从n个不同元素中任取m(m≤n)个元素,按照一定的顺序排列起来,叫做从n个不同元素中取出m个元素的一个排列.当m=n时所有的排列情况叫全排列.如果这组数有n个,那么全排列数为n!个. 比如a ...

  3. POJ1833 排列 调用全排列函数 用copy函数节省时间 即使用了ios同步代码scanf还是比较快

    排列 Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21268   Accepted: 8049 Description 题 ...

  4. next_permutation() 全排列函数

    next_permutation() 全排列函数 这个函数是STL自带的,用来求出该数组的下一个排列组合 相当之好用,懒人专用 适用于不想自己用dfs写全排列的同学(结尾附上dfs代码) 洛谷oj可去 ...

  5. ACM题目————STL + 全排列

    今天碰到一个函数,感觉挺好用的,全排列函数 next_permutation! 求全排列的函数,基本上与自己写的DFS时间复杂度差不多,毕竟是标准库.(2018-1-4 添加) 话不多说,直接上题. ...

  6. 分享stl sort函数坑点导致coredump问题

    在<Effective STL> 的条款21中就有讨论:永远让比较函数对相同元素返回false! 也就是说在实现stl sort函数自定义比较器时,一定要满足这种严格弱序化的问题.

  7. STL - next_permutation 全排列函数

    学习: http://blog.sina.com.cn/s/blog_9f7ea4390101101u.html http://blog.csdn.net/ac_gibson/article/deta ...

  8. C++ 全排列函数 nyoj 366

    C++ STL中提供了std::next_permutation与std::prev_permutation可以获取数字或者是字符的全排列,其中std::next_permutation提供升序.st ...

  9. C++ 全排列函数 std::next_permutation与std::prev_permutation

    C++ STL中提供了std::next_permutation与std::prev_permutation可以获取数字或者是字符的全排列,其中std::next_permutation提供升序.st ...

随机推荐

  1. rails里面添加妹子ui

    妹子ui看起来很不错,以为在rails里面添加自定义的css和js和平时一样,结果可想而知,不过弄完以后发现还是比较简单的,这里记录一下 妹子ui需要加载的css和js如下 http://cdn.am ...

  2. css怎么设置2个div同行,第一个固定宽度,第二个占满剩余的部分

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. python多标签分类模版

    from sklearn.multioutput import MultiOutputClassifier from sklearn.ensemble import RandomForestClass ...

  4. kafka 常用命令

    Kafka 解压,进入kafka目录下的bin目录 启动zk nohup ./zookeeper-server-start.sh ../config/zookeeper.properties & ...

  5. Java GC日志查看

    Java GC类型 Java中的GC有哪几种类型? 参数 描述 UseSerialGC 虚拟机运行在Client模式的默认值,打开此开关参数后, 使用Serial+Serial Old收集器组合进行垃 ...

  6. 2017百度之星初赛B-1002(HDU-6115)

    一.思路 这题“看似”比较难搞的一点是,一个节点上有多个办公室,这怎么求?其他的,求树中任意两个节点的距离(注意:没有最远或最最进这一说法,因为树上任意两个节点之间有且仅有一条路径.不然就有回路了,对 ...

  7. node的socket.io的之事件篇

    socket.io类库不但可以相互发送消息,而且还可以通过socket端口对象的emit方法互相发送事件. emit在之前的事件上说过现在一句话带过:emit是用来手动触发事件的. socket.em ...

  8. node的超时timeout

    如果在指定的时间内服务器没有做出响应(可能是网络间连接出现问题,也可能是因为服务器故障或网络防火墙阻止了客户端与服务器的连接),则响应超时,同时触发http.ServerResponse对象的time ...

  9. buffer cache 深度解析

    本文首先详细介绍了oracle中buffer cache的概念以及所包含的内存结构.然后结合各个后台进程(包括DBWRn.CKPT.LGWR等)深入介绍了oracle对于buffer cache的管理 ...

  10. 解决 service iptables start 无法启动的问题

    解决方式: iptables -F  // 初始化iptables. service iptables save  // 保存 service iptables restart  // 重启