SLT:

C++的STL有一个函数可以方便地生成全排列,这就是next_permutation

在C++ Reference中查看了一下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 的返回值是布尔类型。按照提示写了一个标准C++程序:

 #include <iostream>
#include <algorithm>
#include <string> using namespace std; int main()
{
string str;
cin >> str;
sort(str.begin(), str.end());
cout << str << endl;
while (next_permutation(str.begin(), str.end()))
{
cout << str << endl;
}
return ;
}

其中还用到了 sort 函数和 string.begin()、string.end() ,函数声明如下:

#include <algorithm>
void sort( iterator start, iterator end );

sort函数可以使用NlogN的复杂度对参数范围内的数据进行排序。

#include <string>
iterator begin();
const_iterator begin() const;

#include <string>
iterator end();
const_iterator end() const;

string.begin()和string.end() 可以快速访问到字符串的首字符和尾字符。

在使用大数据测试的时候,发现标准C++的效率很差...换成C函数写一下,效率提升了不止一倍...

 #include <cstdio>
#include <algorithm>
#include <cstring>
#define MAX 100 using namespace std; int main()
{
int length;
char str[MAX];
gets(str);
length = strlen(str);
sort(str, str + length);
puts(str);
while (next_permutation(str, str + length))
{
puts(str);
}
return ;
}

转载链接:https://www.slyar.com/blog/stl_next_permutation.html

全排列 ( next_permutation)的更多相关文章

  1. 全排列next_permutation()用法和构造函数赋值

    全排列next_permutation()用法 在头文件aglorithm里 就是1~n数组的现在的字典序到最大的字典序的依次增加.(最多可以是n!种情况) int a[n]; do{ }while( ...

  2. 关于全排列 next_permutation() 函数的用法

    这是一个c++函数,包含在头文件<algorithm>里面,下面是基本格式. 1 int a[]; 2 do{ 3 4 }while(next_permutation(a,a+n)); 下 ...

  3. 全排列 next_permutation 用法

    给一个正整数n,让你求它的全排列 先介绍一个函数,iota(a,a+n,1) 用法就是把a数组的第0位到第n-1位依次赋为1,2,.....n: 然后是next_permutation(a,a+4)函 ...

  4. 排列2(全排列next_permutation 注意格式)

    排列2 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  5. 蓝桥杯 方格填数 DFS 全排列 next_permutation用法

    如下的10个格子(参看[图1.jpg]) 填入0~9的数字.要求:连续的两个数字不能相邻.(左右.上下.对角都算相邻) 一共有多少种可能的填数方案? 请填写表示方案数目的整数.注意:你提交的应该是一个 ...

  6. 全排列 next_permutation() 函数的用法

    在头文件<algorithm>里面有如下代码: int a[]; do { } while(next_permutation(a,a+n)); 可产生1~n的全排列有如下代码: #incl ...

  7. 全排列 next_permutation() 函数的使用

    看来看去还是这篇博客比较简洁明了 https://www.cnblogs.com/My-Sunshine/p/4985366.html 顺便给出牛客网的一道题,虽然这道题用dfs写出全排列也能做,题意 ...

  8. 嵊州D2T2 八月惊魂 全排列 next_permutation()

    嵊州D2T2 八月惊魂 这是一个远古时期的秘密,至今已无人关心. 这个世界的每个时代可以和一个 1 ∼ n 的排列一一对应. 时代越早,所对应的排列字典序就越小. 我们知道,公爵已经是 m 个时代前的 ...

  9. STL中关于全排列next_permutation以及prev_permutation的用法

    这两个函数都包含在algorithm库中.STL提供了两个用来计算排列组合关系的算法,分别是next_permutation和prev_permutation. 一.函数原型 首先我们来看看这两个函数 ...

随机推荐

  1. 智能家居项目(3):编译工具makefile

    board文件夹中,基本的代码结构分为了src,include两个子文件夹.分别存放源码文件. #CC=arm-linux-gcc CC=gcc CFLAGS=-lpthread INCPATH=-I ...

  2. Linux C编程语言学习材料

    C语言作为最基础的编程语言,30年虚弱的患病率. 无论是准备做 PHP/Java/Python/Golang 开发学习.C语言都是基础的,我们非常多基础非常小的互联网执行的开源软件服务都是C语言构筑, ...

  3. 【C语言探索之旅】 第二部分第六课:创建你自己的变量类型

    内容简介 1.课程大纲 2.第二部分第六课: 创建你自己的变量类型 3.第二部分第七课预告:   文件读写 课程大纲 我们的课程分为四大部分,每一个部分结束后都会有练习题,并会公布答案.还会带大家用C ...

  4. SQL server 提示“代理XP”被关闭的解决方法

    打开SQL Server Configuration Manager(SQL Server配置管理器),启用SQL Server Agent(SQL Server 代理).

  5. Unity 二战中加飞机

    一个简短的引论: 谢意: 本申请中使用<Unity3D\2D移动游戏开发>提供资源.著作权属于作者.感谢作者.基于原始时本申请的二次开发. 要素: 1.增加2s cd的机身旋转,旋转时保持 ...

  6. Vim经常使用技巧总结1

    我的主力博客:半亩方塘 1. 反复上次的操作在普通模式下用 . 命令 2. 取消上次的操作在普通模式下用 u 命令 3. 整行删除在普通模式下用 dd 命令,此命令在删除行后 不进入插入模式 4. 整 ...

  7. WWDC2014开源

    A Cocoa OSX App to help you download WWDC2014 videos 地址:https://github.com/iosxtools/WWDC2014 版权声明:本 ...

  8. hdu4499 Cannon (DFS+回溯)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=4499 Cannon ...

  9. 关于查看文件size

    public static long getTotalSize(String device) { StatFs sf = new StatFs (device); //long totalblocks ...

  10. BZOJ 3362 POJ 1984 Navigation Nightmare 并与正确集中检查

    标题效果:一些养殖场是由一些南北或东西向的道路互连. 镶上在不断的过程中会问两个农场是什么曼哈顿的距离,假设现在是不是通信.那么输出-1. 思维:并与正确集中检查,f[i]点i至father[i]距离 ...