逆天的全排列函数next_permutation()
next_permutation 是算法库(<algorithm>)里的一个用于求全排列的函数,其定义为
next_permutation(_BidIt _First, _BidIt _Last)
可以看出,对于next_permutation需要可迭代容器的范围(即头尾),然后会根据字典序来对此序列来进行全排列并存储在原先的容器里,其排列顺序也是按照字典序输出的
注意,这里的“字典序”对于字符串是根据ASci码来比较,对于数字就是按照其大小来比较
给出个例子,可以看到对于1,2,3,其自动将其全排列求出并存在原数组中

而如果数组一开始就是别的序列呢,比如

可以看到,next_permutation是非常智能的,可以识别当前排列并顺着字典序继续进行其下一个排列,不会重复
给出一个很好的例子
P1088 [NOIP2004 普及组] 火星人 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
这道题如果清楚的知道next_permutation可以直接输出其下一步的全排列,输出答案只需要三行代码---->读入,next_permutation M次,输出,这样的数组中存储的就是答案中的全排列
同理next_permutation相应的就是prev_permutation,其求的是对应容器中序列的上一个全排列
逆天的全排列函数next_permutation()的更多相关文章
- C++中全排列函数next_permutation用法
最近做了TjuOj上关于全排列的几个题,室友告诉了一个非常好用的函数,谷歌之,整理如下: next_permutation函数 组合数学中经常用到排列,这里介绍一个计算序列全排列的函数:next_pe ...
- C++中全排列函数next_permutation 用法
今天蓝桥杯刷题时发现一道字符串排序问题,突然想起next_permutation()函数和prev_permutation()函数. 就想写下next_permutation()的用法 next_pe ...
- HDU 1027 Ignatius and the Princess II[DFS/全排列函数next_permutation]
Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ( ...
- 全排列函数next_permutation(a,a+n)
#include<iostream> #include<algorithm> using namespace std; int main(){ ]; int n; cin> ...
- C++全排列函数next_permutation()和prev_permutation()
头文件:#include<algorithm> * * * 1. next_permutation(): next_permutation()函数的返回类型是bool类型. 即:如果有一个 ...
- next_permutation() 全排列函数
next_permutation() 全排列函数 这个函数是STL自带的,用来求出该数组的下一个排列组合 相当之好用,懒人专用 适用于不想自己用dfs写全排列的同学(结尾附上dfs代码) 洛谷oj可去 ...
- C++ STL 全排列函数
C++ 全排列函数...一听名字就在<algorithm>中... 首先第一个说的是next_permutation: #include <algorithm> bool n ...
- 2017年上海金马五校程序设计竞赛:Problem A : STEED Cards (STL全排列函数)
Description Corn does not participate the STEED contest, but he is interested in the word "STEE ...
- POJ1833 排列 调用全排列函数 用copy函数节省时间 即使用了ios同步代码scanf还是比较快
排列 Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21268 Accepted: 8049 Description 题 ...
- C++中全排列算法函数next_permutation的使用方法
首先,先看对next_permutation函数的解释: http://www.cplusplus.com/reference/algorithm/next_permutation/?kw=next_ ...
随机推荐
- vue axiox网络请求
一.首先安装axios ,vue-axios 前提:搭建一个vue3的项目 项目搭建参考:https://www.cnblogs.com/yclh/p/15356171.html 使用npm安装axi ...
- vue学习笔记 九、父子组件实例-基本结构
系列导航 vue学习笔记 一.环境搭建 vue学习笔记 二.环境搭建+项目创建 vue学习笔记 三.文件和目录结构 vue学习笔记 四.定义组件(组件基本结构) vue学习笔记 五.创建子组件实例 v ...
- XSS、CSRF 以及如何防范
- 广搜 广搜 poj 3984
***求最短路径,然后再输出路径, BFS+路径输出*** #include <iostream> #include <cstdio> #include <cstring ...
- python之configparser类的使用
一.定义配置文件格式如下:data.conf [interface] url=http://192.168.37.8:7777/api/mytest2 [switch] switch_car=on [ ...
- TOEFL | 202307 改革 · 新版题型总结
目录 Listening(36min) Reading(35min) Speaking(16min) Writing(29min) Listening(36min) 2 conversation,3 ...
- cs 保研经验贴 | 英语口试
很多夏令营都有英语面试环节.但这其实是有迹可循的,多说几遍就熟练了. 无论是笔试面试,还是联系导师 联系 hr,这种自我推销的事情,都会越做越熟练的.希望发表也是如此吧-(来自博零菜鸟的碎碎念-) 目 ...
- 新建Maven工程没有src/main...目录?
0.必看:详细的Maven项目介绍 1.问题 我新建的Maven项目的pom.xml为空,且无法被识别,同时项目目录没有src/main等等 2.解决 这里设置的JDK版本不对,我选用了JDK19 但 ...
- 【SHELL】命令补全
# 指定文件 dodo_path=/home/skull/work/scripts/dodo echo "hello skull" ## COMP_WORDS 是一个 bash 内 ...
- UEditor 添加在线管理图片删除功能 (转载)
第一,需要添加一个 php 文件来实现删除功能,文件添加到: ueditor\php\action_delete.php 代码内容: <?php /*---------------------- ...