C++STL 之排列
固然我们可以自己使用递归编写全排列程序,但是既然STL里面已将有了这个功能为什么不直接用呢,下面就写一下直接使用C++ STL生成全排序的程序
函数名:next_permutation
包含头文件:algorithm
函数原型:
template<class BidirectionalIterator>
bool next_permutation(BidirectionalIterator _First, BidirectionalIterator _Last );
template<class BidirectionalIterator, class BinaryPredicate>
bool next_permutation(BidirectionalIterator _First, BidirectionalIterator _Last, BinaryPredicate _Comp );
两个重载函数,第二个带谓词参数_Comp,其中只带两个参数的版本,默认谓词函数为"小于".
返回值:bool类型(默认若当前调用排列到达最大字典序则返回false,同时重新设置该排列为最小字典序,否则返回true,并按照字典递增的顺序输出下一个排列。例如,在字母表中,abcd的下一单词排列为abdc)
所以如果是生成一个数组的全排列,先要对数组按升序排序,然后使用do-while语句循环调用next_permutation函数
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int main()
{
string str;
cin>>str;
int len=str.length();
char *cstr=(char *)str.c_str();
cout<<"排列输出如下"<<endl;
do
{
cout<<cstr<<endl;
}while(next_permutation(cstr,cstr+len));
cout<<"排列之后cstr变为:"<<endl;
cout<<cstr;
return ;
}
上面是一个没有加排序直接调用nextpermation看一下,不同输入的情况下输出结果的比较
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int main()
{
string str;
cin>>str;
int len=str.length();
char *cstr=(char *)str.c_str();
sort(cstr,cstr+len);
cout<<"排列输出如下"<<endl;
do
{
cout<<cstr<<endl;
}while(next_permutation(cstr,cstr+len));
cout<<"排列之后cstr变为:"<<endl;
cout<<cstr;
return ;
}
加上排序之后,看看效果
C++STL 之排列的更多相关文章
- nyoj------擅长排列的小明
擅长排列的小明 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 小明十分聪明,而且十分擅长排列计算.比如给小明一个数字5,他能立刻给出1-5按字典序 ...
- POJ-2718
Smallest Difference Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12158 Accepted: 3 ...
- nyist oj 19 擅长排列的小明(dfs搜索+STL)
擅长排列的小明 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描写叙述 小明十分聪明.并且十分擅长排列计算.比方给小明一个数字5,他能立马给出1-5按字典序的全排列,假设你想 ...
- 枚举所有排列-STL中的next_permutation
枚举排列的常见方法有两种 一种是递归枚举 另一种是STL中的next_permutation //枚举所有排列的另一种方法就是从字典序最小排列开始,不停的调用"求下一个排列"的过程 ...
- suseoj 1208: 排列问题 (STL, next_permutation(A.begin(), A.end()))
1208: 排列问题 时间限制: 1 Sec 内存限制: 128 MB提交: 2 解决: 2[提交][状态][讨论版][命题人:liyuansong] 题目描述 全排列的生成就是对于给定的字符集或 ...
- C++ STL next_permutation(快速排列组合)
排列组合必备!! https://blog.csdn.net/bengshakalakaka/article/details/78515480
- HDU 1027(数字排列 STL)
题意是求 n 个数在全排列中的第 m 个序列. 直接用 stl 中的 next_permutation(a, a+n) (这个函数是求一段序列的下一个序列的) 代码如下: #include <b ...
- hdu1716排列2(stl:next_permutation+优先队列)
排列2 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- poj 1833 排列 STL 全排列公式
排列 Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15173 Accepted: 6148 Description 题 ...
随机推荐
- css的display属性小实验
div与span是常用的盒子模型; 区别: div默认是垂直分布(独占一行) span默认是水平分布(一行可以有多个) 通过float属性可以改变div容器的分布方式达到span容器的效果; 下面 ...
- Careercup - Facebook面试题 - 5998719358992384
2014-05-02 00:22 题目链接 原题: Given a matrix consisting of 's. 题目:给定一个01矩阵,找出由1构成的连通分量中最大的一个. 解法:四邻接还是八邻 ...
- SQL Server数据库事务日志存储序列
原文 原文:http://blog.csdn.net/tjvictor/article/details/5251351 如果你的数据库运行在完整或是批量日志恢复模式下,那么你就需要使用作业(job ...
- SCRUM团队的三个角色
Scrum团队中包括三个角色,他们分别是产品负责人.开发团队和 Scrum Master. Scrum 团队是自组织.跨职能的完整团队.自组织团队决定如何最好地完成他们的工作,而不是由团队外的其他人来 ...
- sqlserver insert into select
Insert into [fenxi].[dbo].[analysisresult]( [dayofweek] ,[quarter] ,[reporttime] ,[type] ,[value]) s ...
- 【BZOJ】【2132】圈地计划
网络流/最小割 Orz Hzwer 这类大概是最小割建模中的经典应用吧…… 黑白染色,然后反转黑色的技巧感觉很巧妙!这个转化太神奇了…… /****************************** ...
- 让32位Eclipse和64位Eclipse同时在64的Windows7上运行
转自让32位Eclipse和64位Eclipse同时在64的Windows7上运行 参考这篇文章:http://wenku.baidu.com/view/57994c270066f5335a81214 ...
- Stateless Iterators
As the name implies, a stateless iterator is an iterator that does not keep any state by itself. The ...
- 理解lua 语言中的点、冒号与self
转载自: http://blog.csdn.net/wangbin_jxust/article/details/12170233 lua编程中,经常遇到函数的定义和调用,有时候用点号调用,有时候用冒号 ...
- mutable关键字
mutalbe的中文意思是“可变的,易变的”,跟constant(既C++中的const)是反义词.在C++中,mutable也是为了突破const的限制而设置的.被mutable修饰的变量(muta ...