c++中的全排列
next_permutation函数
组合数学中经常用到排列,这里介绍一个计算序列全排列的函数:next_permutation(start,end),和prev_permutation(start,end)。这两个函数作用是一样的,区别就在于前者求的是当前排列的下一个排列,后一个求的是当前排列的上一个排列。至于这里的“前一个”和“后一个”,我们可以把它理解为序列的字典序的前后,严格来讲,就是对于当前序列pn,他的下一个序列pn+1满足:不存在另外的序列pm,使pn<pm<pn+1.
对于next_permutation函数,其函数原型为:
#include <algorithm>
bool next_permutation(iterator start,iterator end)
当当前序列不存在下一个排列时,函数返回false,否则返回true
同时,相对应的,上一个排列即为prev_permutation(int *begin, int *end)
看如下代码:
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int num[]={,,};
do
{
cout<<num[]<<" "<<num[]<<" "<<num[]<<endl;
}while(next_permutation(num,num+));
return ;
}
运行结果如图:

注意:当我们把while(next_permutation(num,num+3))中的3改为2时,输出就变为了下图所示:

说明此时只针对1,2进行了全排列,有两个,后面的3没有变化,同时改变了数组前两个的值。
由此可以看出,next_permutation(num,num+n)函数是对数组num中的前n个元素进行全排列,同时并改变num数组的值。
另外,需要强调的是,next_permutation()在使用前需要对欲排列数组按升序排序,否则只能找出该序列之后的全排列数。
比如,如果数组num初始化为3,1,2,那么输出就变为了:

维基百科上全排列的实现:
循环法:
#include <iostream>
using namespace std;
bool arrsame(int* arr, int len, int num) {
int i;
for (i = ; i < len; i++)
if (arr[i] == num)
break;
return i != len;
}
bool next_perm(int* perm, const int k, const int n) {
int i = k - ;
do
perm[i]++;
while (arrsame(perm, i, perm[i]) || (perm[i] >= n && i--));
if (perm[] >= n)
return ;
for (int num = , seat = i + ; seat < k; num++)
if (!arrsame(perm, i + , num))
perm[seat++] = num;
return ;
}
int main() {
int n, k;
cout << "perm(n,k):" << endl;
cin >> n >> k;
if (n < k || k <= )
return ;
int* perm = new int[k];
for (int i = ; i < k; i++)
perm[i] = i;
do
for (int i = ; i < k; cout << ((++i < k) ? ',' : '\n'))
cout << perm[i] + ;
while (next_perm(perm, k, n));
delete[] perm;
return ;
}
递归法:
#include <bits/stdc++.h>
using namespace std; struct prem {
int len;
vector<int> used, position;
function<void(vector<int>&)> action;
prem(int l = , function<void(vector<int>&)> a = [](vector<int>& position) {}) : len(l), used(l, -), position(l), action(a) {}
void run(int now = -) {
if (now == len - ) {
action(position);
return;
}
int next = now + ;
for (int i = ; i < len; i++) {
if (used[i] == -) {
used[i] = next;
position[next] = i;
run(next);
used[i] = -;
}
}
}
};
int main() {
ios::sync_with_stdio(false), cin.tie();
int len = ;
prem p(len, [&](vector<int>& p) {
for (int i = ; i < len; i++) {
cout << p[i] << " ";
}
cout << endl;
});
p.run();
return ;
}
next_permutation 可以自定义比较函数 例如:POJ 1256
题目中要求的字典序是:A'<'a'<'B'<'b'<...<'Z'<'z',所以在用函数之前必须得按照题目要求的进行排序
#include<iostream> //poj 1256 Anagram
#include<string.h>
#include<algorithm>
using namespace std;
int cmp(char a,char b)
{
if(tolower(a)!=tolower(b))//tolower 是将大写字母转化为小写字母.
return tolower(a)<tolower(b);
else
return a<b;
}
int main()
{
char ch[];
int n;
cin>>n;
while(n--)
{
scanf("%s",ch);
sort(ch,ch+strlen(ch),cmp);
do
{
printf("%s\n",ch);
}while(next_permutation(ch,ch+strlen(ch),cmp));
}
return ;
}
.
c++中的全排列的更多相关文章
- [LeetCode] Permutation in String 字符串中的全排列
Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. I ...
- STL中关于全排列next_permutation以及prev_permutation的用法
这两个函数都包含在algorithm库中.STL提供了两个用来计算排列组合关系的算法,分别是next_permutation和prev_permutation. 一.函数原型 首先我们来看看这两个函数 ...
- [LeetCode] 567. Permutation in String 字符串中的全排列
Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. I ...
- STL中的全排列实现
permutation: 在遇到全排列问题时,在数据量较小的情况下可以使用dfs的做法求得全排列,同时我们也知道在STL中存在函数next_permutation和prev_permutation,这 ...
- java实现全排列问题
1.问题描述: 一组字符串的全排列,按照全排列的顺序输出,并且每行结尾无空格. 2.输入: 输入一个字符串 3.输入示例: 请输入全排列的字符串: abc 4.输出示例: a b c a c b b ...
- c语言的全排列
在c语言中实现全排列,对于刚接触c语言,还没学习算法的人来说,比较困难了吧.估计大佬也不会看这种基础的东西,全排列实现的办法很多,在c++中有一个专门的函数可以使用,但是在c中实现就有点困难了.如果你 ...
- c语言中一种典型的排列组合算法
c语言中的全排列算法和组合数算法在实际问题中应用非常之广,但算法有许许多多,而我个人认为方法不必记太多,最好只记熟一种即可,一招鲜亦可吃遍天 全排列: #include<stdio.h> ...
- 【C/C++】n皇后问题/全排列/递归/回溯/算法笔记4.3
按常规,先说一下我自己的理解. 递归中的return常用来作为递归终止的条件,但是对于返回数值的情况,要搞明白它是怎么返回的.递归的方式就是自己调用自己,而在有返回值的函数中,上一层的函数还没执行完就 ...
- 2014亚马逊在线笔试题目及解决方案(MMChess问题)
整体思路:关键是需要知道当前Steps数组中的全排列即可,而且需要不重复的全排列.即关键在于非递归的全排列实现即可~ 其实直接利用STL中的next_permutation算法的,这里我又自己实现了一 ...
随机推荐
- Linux 笔记:文件名
文件名 Linux 系统区分英文字符的大小写.比如,myfile, Myfile 和 myFILE表示的是三个不同的文件.同样,用户密码和登录名也需要区分大小写(这里沿用了Unix 和 C 语言的命名 ...
- 【代码学习】PHP面向对象之封装与继承
一.封装(访问控制) 就是将属性私有,并提供公有的setter放置与getter取值方法注:封装是进行访问控制,不是拒绝访问 public(公有) protected(受 ...
- CSS背景透明设置
style="margin-top:300px;background:rgba(255,255,255,这里设置小于1比如0.6这样); color:black;" style=& ...
- mssql-osql
mssql导入单行字段值非常长,或者sql文件非常大,比如上百M或者更大,常规方法是导不进去的,所以推荐下面方式进行导入. osql -S . -U sa -P 123456 -d TS_TEST - ...
- 设计模式课程 设计模式精讲 3-7 接口隔离原则讲解及Coding
1 主讲内容 1.1 核心内容 1.2 优点 1.3 课程记录 2 代码演练 2.1 接口隔离原则反比 2.2 接口隔离原则正比 1 主讲内容 1.1 核心内容 总结:细粒度可以进行再组装,粗粒度不可 ...
- javascript入门教程02
JavaScript中的运算符 (1)算术运算符 + :相加 var a=123,b=45; document.write(a+b); - :相减 document.write(a-b); *:相乘 ...
- java动态代理中的invoke方法是如何被自动调用的
转载声明:本文转载至 zcc_0015的专栏 一.动态代理与静态代理的区别. (1)Proxy类的代码被固定下来,不会因为业务的逐渐庞大而庞大:(2)可以实现AOP编程,这是静态代理无法实现的:(3) ...
- Hibernate笔记二
1.延迟加载(懒加载) 概念 需要用到该数据的时候才要加载 种类 类的延迟加载 案例 说明:注意:使用的是Load方法 1. 执行22行代码的时候,不发出sql语句,说明类的延迟加载和主键没有关系 ...
- spring mvc注解之@RequestBody和@RequestParm
@RequestBody http://localhost:8080/user/login { "username":"jack", "passwar ...
- 学习笔记(16)- InsuranceQA_zh
仓库地址:https://github.com/l11x0m7/InsuranceQA_zh Convolutional Neural Network for Chinese InsuranceQA ...