全排列函数(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_ ...
随机推荐
- Scala 学习笔记之集合(8) Try和Future
import util._ import concurrent.ExecutionContext.Implicits.global import concurrent.Future import co ...
- React 项目引入 Dva
背景 现在手上在做的 React 项目因为年代久远,用的 Redux,写代码的体验不太好,所以想升级一下引入 dva.以往使用 dva 都是使用 dva-cli 直接生成 dva 项目,或者在使用 a ...
- QCustomPlot 基础
QCutomPlot简介 官网网址及介绍 https://www.qcustomplot.com/ QCustomPlot is a Qt C++ widget for plotting and da ...
- hihoCode 1075 : 开锁魔法III
时间限制:6000ms 单点时限:1000ms 内存限制:256MB 描述 一日,崔克茜来到小马镇表演魔法. 其中有一个节目是开锁咒:舞台上有 n 个盒子,每个盒子中有一把钥匙,对于每个盒子而言有且仅 ...
- redis数据库的使用
一.安装redis与可视化操作工具 可视化工具:RedisDesktopManager redis载地址:https://github.com/MSOpenTech/redis/releases. 二 ...
- 友价商城SQL注入
友价商城SQL注入 源码出自:https://www.0766city.com/yuanma/9990.html 下载安装好后打开是这样的: 8不说了 ,seay审计一把梭哈 从开始审计 直 ...
- Cocos2d-x 学习笔记(11.5) SkewTo SkewBy
1. SkewTo SkewBy node朝X和Y方向的歪斜.SkewTo是SkewBy的父类. 1.1 成员变量 create方法 // 两者成员变量一致 float _skewX; float _ ...
- 同一台机上配置多个redis服务
"D:\Program Files\Redis\redis-server.exe" --service-run F:Redis2\redis.windows-service6380 ...
- 微信小程序单选/多选框样式重新
/* 重写 checkbox 样式 */ /* 未选中的 背景样式 */ checkbox .wx-checkbox-input{ border-radius: 50%;/* 圆角 */ width: ...
- Python编程系列---Python中装饰器的几种形式及万能装饰器
根据函数是否传参 是否有返回值 ,可以分析出装饰器的四种形式: 形式一:无参无返回值 def outer(func): def wrapper(): print("装饰器功能1" ...