平常需要全排列的时候,一般都是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
acba

Sample Output

Aab
Aba
aAb
abA
bAa
baA
abc
acb
bac
bca
cab
cba
aabc
aacb
abac
abca
acab
acba
baac
baca
bcaa
caab
caba
cbaa

Hint

An upper case letter goes before the corresponding lower case letter. 
So the right order of letters is 'A'<'a'<'B'<'b'<...<'Z'<'z'.
(英语渣写一下大意:有n组数据,每组是一个字符串。对于每组数据,输出它按字母顺序(不是字典序,'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())的更多相关文章

  1. C++中全排列函数next_permutation用法

    最近做了TjuOj上关于全排列的几个题,室友告诉了一个非常好用的函数,谷歌之,整理如下: next_permutation函数 组合数学中经常用到排列,这里介绍一个计算序列全排列的函数:next_pe ...

  2. C++中全排列函数next_permutation 用法

    今天蓝桥杯刷题时发现一道字符串排序问题,突然想起next_permutation()函数和prev_permutation()函数. 就想写下next_permutation()的用法 next_pe ...

  3. 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 ( ...

  4. 全排列函数next_permutation(a,a+n)

    #include<iostream> #include<algorithm> using namespace std; int main(){ ]; int n; cin> ...

  5. C++全排列函数next_permutation()和prev_permutation()

    头文件:#include<algorithm> * * * 1. next_permutation(): next_permutation()函数的返回类型是bool类型. 即:如果有一个 ...

  6. next_permutation() 全排列函数

    next_permutation() 全排列函数 这个函数是STL自带的,用来求出该数组的下一个排列组合 相当之好用,懒人专用 适用于不想自己用dfs写全排列的同学(结尾附上dfs代码) 洛谷oj可去 ...

  7. C++ STL 全排列函数

    C++  全排列函数...一听名字就在<algorithm>中... 首先第一个说的是next_permutation: #include <algorithm> bool n ...

  8. 2017年上海金马五校程序设计竞赛:Problem A : STEED Cards (STL全排列函数)

    Description Corn does not participate the STEED contest, but he is interested in the word "STEE ...

  9. POJ1833 排列 调用全排列函数 用copy函数节省时间 即使用了ios同步代码scanf还是比较快

    排列 Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21268   Accepted: 8049 Description 题 ...

  10. C++中全排列算法函数next_permutation的使用方法

    首先,先看对next_permutation函数的解释: http://www.cplusplus.com/reference/algorithm/next_permutation/?kw=next_ ...

随机推荐

  1. phaser学习总结之Tween详解

    前言 在上一章phaser学习总结之phaser入门教程中,我们已经初步入门了phaser,并通过一个案例了解了phaser,现在我们需要对phaser中的对象进行讲解,本章需要讲解的是tween,即 ...

  2. 利用基本数据封装类(如:Integer,Float)等实现数据类型转换

    /** * 利用基本数据封装类进行数据类型转换 * @author dyh * */ public class TypeConversion { public static void main(Str ...

  3. [PHP] php, apache, VS Code安装与配置

    1. 下载

  4. java并发之内存模型

    java内存模型知识导图 一 并发问题及含义   并发编程存在原子性.可见性.有序性问题. 原子性即一系列操作要么都执行,要么都不执行. 可见性,一个线程对共享变量的修改,另一个线程可能不会马上看到. ...

  5. react native ios 上架

    1.申请开发者账号,去苹果开发者中心申请 2.applicationloader 集申请证书.真机调试.发布于一身,避免繁琐的官网申请过程 http://www.applicationloader.n ...

  6. 乱七八糟的学习资料汇总(python3.x,pyqt,svn,git)

    入门涉猎资料: python3.6.3中文文档:https://www.runoob.com/manual/pythontutorial3/docs/html/interpreter.html pyt ...

  7. vue-cli脚手架项目本地阅览和在线阅览

    一.需求 开发环境:当然啦,前提是你要完整检测整个项目无错误的运行:npm run dev 后项目在本地预览问题没有问题 二.服务器本地阅览 生产环境:项目制作完成后需执行npm run build操 ...

  8. 原生js使用getComputedStyle方法获取CSS内部属性值

    在对网页进行调试的过程中,经常会用到js来获取元素的CSS样式, 1.下面的方法只能JS只能获取写在html标签中的写在style属性中的值(style=”…”),而无法获取定义在<style ...

  9. How to Get What You Want 如何得到你想要的

    [1]If you want something, give it away. [2]When a farmer wants more seeds, he takes his seeds and gi ...

  10. 数据结构中数组反转与STL库Algorithm中的reverse

    数组是个基本的线性数据结构,其实是内存中的一个块,我们可以通过c++的new来分配一个数组 int* a= new int[5]; 然后填数组的每个元素 a[0]=1; a[1]=2; a[2]=6; ...