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 全排列函数的更多相关文章

  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. CentOS 7 安装 Nodejs npm 及版本冲突解决

    JC&BC 笔记: 可能没安装过 npm 的人会有点疑惑,安装 npm 跟安装 nodejs 有什么关系? 安装 npm 其实就是安装 nodejs 的过程.这一点官方说的很明白,npm 依赖 ...

  2. vptr, vtable, virtual base class table

    #include <iostream> using namespace std; class X { int x, y, z; }; class Y: public virtual X { ...

  3. node中express的中间件之basicAuth

    basicAuth中间件为网站添加身份认证功能.在使用了该中间件后, 用户访问网站时必须输入用户名与密码,在用户输入了用户名与密码并通过验证之后才能访问网站. 当用户输入的用户名和密码符合条件,中间件 ...

  4. apache 自定义404错误页面

    1.有些提供web服务的网站,在用户访问一个不存在的网站文件时,会提示404错误,如下所示: 现在要求自定义一个错误页面,也就是出现404错误代码时,跳转到我们自定义的网址上.下面记录下方法: 1.编 ...

  5. 使用java获取自己的机器网卡

    package org.ibase4j.core.util; import java.io.BufferedReader;import java.io.IOException;import java. ...

  6. e8000051

    Unable to install package (e8000051). Please check used certificate validity and provisioning.. Unab ...

  7. FireDAC 接占线导致另一个 hstmt DataSnap

    [FireDAC][Phys][ODBC][Microsoft][ODBC SQL Server Driver]连接占线导致另一个 hstmt 同样的程序,在2台win10 正常,1台win10 报连 ...

  8. shiro和Spring整合使用注解时没有执行realm的doGetAuthorizationInfo回调方法的解决(XML配置)

    在使用Shiro框架进行项目整合时,使用注解在使用Shiro框架进行项目整合时,使用注解在使用Shiro框架进行项目整合时,使用注解@RequiresPermissions为方法提供是需要的权限,但是 ...

  9. Oracle11gr2_ADG管理之在备库上模拟failover的过程实战

    技术建议和方案. 要求failover后不重建备库,并能够把failover的数据库重新切换回备库 主库为newtest,备库为snewtest 备库上已经开启了闪回 得到一个参考的SCN SQL&g ...

  10. U3D中的又一个坑

    using System.Collections; using System.Collections.Generic; using UnityEditor; using UnityEngine; pu ...