全排列函数(next_permutation())

平常需要全排列的时候,一般都是dfs然后字符串匹配啥的……今天看题解的时候突然发现了这个神器。
next_permutation()函数在c++的algorithm库里,作用是传入一个数组,输出这个数组的后一个排序。同样还有prev_permutation()输出前一个排序。这个“后一个”指的是,不存在另一个排序方案,字典序大于目前排序而小于将要输出的排序。(换句话说,把它的左右排列按字典序升序排序,输出当前状态的下一个)
next_permutation()是一个bool函数,当前序列不存在下一个排列时,函数返回false,否则返回true。
一个例子:(当然,也可以排序char类型,或处理一个string而不是数组)
#include <bits/stdc++.h>
using namespace std;
int arr[] = {, , };
int main(){
do{
cout << arr[] << arr[] << arr[] << endl;
}while(next_permutation(arr, arr+));
return ; //by MiserWeyte in cnblogs
}
输出结果:
特别说明一下,以这段代码为例,如果执行循环前数组是按升序排列好的,则输出该数组的全排列,否则只是比初始状态大的一部分。
next_permutation()除了传入begin和end之外,也可以再传一个cmp函数进去。规则类似于sort(),cmp函数与重载小于号作用相同。
参考wzj792506536的博客中提到的poj 1256:Anagram。
Description
You are to write a program that has to generate all possible words from a given set of letters.
Example: Given the word "abc", your program should - by exploring all different combination of the three letters - output the words "abc", "acb", "bac", "bca", "cab" and "cba".
In the word taken from the input file, some letters may appear more than once. For a given word, your program should not produce the same word more than once, and the words should be output in alphabetically ascending order.Input
The input consists of several words. The first line contains a number giving the number of words to follow. Each following line contains one word. A word consists of uppercase or lowercase letters from A to Z. Uppercase and lowercase letters are to be considered different. The length of each word is less than 13.Output
For each word in the input, the output should contain all different words that can be generated with the letters of the given word. The words generated from the same input word should be output in alphabetically ascending order. An upper case letter goes before the corresponding lower case letter.Sample Input
3
aAb
abc
acbaSample Output
Aab
Aba
aAb
abA
bAa
baA
abc
acb
bac
bca
cab
cba
aabc
aacb
abac
abca
acab
acba
baac
baca
bcaa
caab
caba
cbaaHint
An upper case letter goes before the corresponding lower case letter.
So the right order of letters is 'A'<'a'<'B'<'b'<...<'Z'<'z'.
利用自定义cmp解答:
#include <bits/stdc++.h>
using namespace std;
char str[];
int cmp(const char &a, const char &b) {
if(tolower(a) != tolower(b)) return tolower(a) < tolower(b);
else return a < b;
}
int main() {
int n;
scanf("%d", &n);
while(n--) {
scanf("%s", str);
sort(str, str+strlen(str), cmp);
do {
printf("%s\n", str);
} while(next_permutation(str, str+strlen(str), cmp));
}
return ;
}
全排列函数(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_ ...
随机推荐
- 尝鲜 vue3.x 新特性 - CompositionAPI
0. 基础要求 了解常见的 ES6 新特性 ES6 的导入导出语法 解构赋值 箭头函数 etc... 了解 vue 2.x 的基本使用 组件 常用的指令 生命周期函数 computed.watch.r ...
- Python 必备面试基础知识-3
今天继续分享 Python 相关的面试题,你准备好了嘛! 网络编程篇 1. 简述 OSI 七层协议 是网络传输协议,人为的把网络传输的不同阶段划分成不同的层次. 七层划分为:应用层.表示层.会话层.传 ...
- ssm配置文件
mybatis配置文件SqlMapConfig.xml <?xml version="1.0" encoding="UTF-8"?> <!DO ...
- Windows搭建SVN服务器
安装 1.下载地址:https://www.visualsvn.com/files/VisualSVN-Server-4.0.3-x64.msi 2.开始安装: 3.安装配置: 4.Next直到完成 ...
- A-04 坐标轴下降法
目录 坐标轴下降法 一.坐标轴下降法流程 二.坐标轴下降法和梯度下降法的异同 更新.更全的<机器学习>的更新网站,更有python.go.数据结构与算法.爬虫.人工智能教学等着你:http ...
- A-06 最小角回归法
目录 最小角回归法 一.举例 二.最小角回归法优缺点 2.1 优点 2.2 缺点 三.小结 更新.更全的<机器学习>的更新网站,更有python.go.数据结构与算法.爬虫.人工智能教学等 ...
- HashMap底层数据结构详解
一.HashMap底层数据结构 JDK1.7及之前:数组+链表 JDK1.8:数组+链表+红黑树 关于HashMap基本的大家都知道,但是为什么数组的长度必须是2的指数次幂,为什么HashMap的加载 ...
- Java虚拟机重点知识归纳总结
一. JVM内存分区 分为程序计数器.虚拟机栈.本地方法栈.Java堆.方法区5个区域 其中Java堆和方法区是线程共享的,虚拟机栈.本地方法栈.程序计数器是线程隔离的. 程序计数器: 1.可 ...
- Vue中插槽指令
08.29自我总结 Vue中插槽指令 意义 就是在组件里留着差值方便后续组件内容新增 而且由于插件是写在父级中数据可以直接父级中传输而不需要传子再传父有些情况会减少写代码量 示例 <div id ...
- 二、docker 镜像容器常用操作(让我们用docker 溜得飞起)
前言 上篇讲了我们如何安装docker,现在该我们一展拳脚的时候了.接下来让我们一起学习一下docker常见的操作,让我们能够会使用 docker. 基本概念 在讲使用之前,还是先将一下docker ...