全排列函数(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_ ...
随机推荐
- 夯实Java基础系列21:Java8新特性终极指南
本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https://github.com/h2pl/Java-Tutorial 喜欢的话麻烦点下 ...
- SpringBootSecurity学习(21)前后端分离版之OAuth2.0非对称加密
JWT转换器 前面的例子中,都是在授权服务配置类中配置了一个很简单的jwt转换器,如下: 可以看到我们只用setSigningKey方法配置了一个秘钥,这里使用的是简单的对称加密的方式来加密jwt内容 ...
- Linux 命令个人笔记
[表示命令]man -f [] 显示一个命令的功能whatis [] 显示一个命令的功能ls -lR | grep '^-' | wc -l 统计一个目录下总共有多少个文件head [-n numbe ...
- 500行代码,教你用python写个微信飞机大战
这几天在重温微信小游戏的飞机大战,玩着玩着就在思考人生了,这飞机大战怎么就可以做的那么好,操作简单,简单上手. 帮助蹲厕族.YP族.饭圈女孩在无聊之余可以有一样东西让他们振作起来!让他们的左手 / 右 ...
- 04-10 Bagging和随机森林
目录 Bagging算法和随机森林 一.Bagging算法和随机森林学习目标 二.Bagging算法原理回顾 三.Bagging算法流程 3.1 输入 3.2 输出 3.3 流程 四.随机森林详解 4 ...
- JVM垃圾回收(下)
接着上一篇,介绍完了 JVM 中识别需要回收的垃圾对象之后,这一篇我们来说说 JVM 是如何进行垃圾回收. 首先要在这里介绍一下80/20 法则: 约仅有20%的变因操纵着80%的局面.也就是说:所有 ...
- LRU算法实现,HashMap与LinkedHashMap源码的部分总结
关于HashMap与LinkedHashMap源码的一些总结 JDK1.8之后的HashMap底层结构中,在数组(Node<K,V> table)长度大于64的时候且链表(依然是Node) ...
- 《java编程思想》P22-P37(第二章一切都是对象)
1.JAVA操纵的标识符实际上是对象的一个"引用";如String s;里的s是String类的引用并非对象. 2.程序运行时,有五个不同的地区可以存储数据. (1)寄存器:最快的 ...
- 拿起键盘就是干:跟我一起徒手开发一套分布式IM系统
1.引言 老读者应该还记得我在去年国庆节前分享过一篇<技术干货:从零开始,教你设计一个百万级的消息推送系统>,虽然我在文中有贴一些伪代码,依然有些朋友希望能直接分享一些可以运行的源码.好吧 ...
- MySQL 拿 WebShell
两种常规方法利用 MySQL getshell 的方法: select … into outfile general_log 一.select … into outfile 介绍 利用需要满足以下条件 ...