dfs生成全排列
模板
#include<bits/stdc++.h>
using namespace std;
int a[]={1,2,3,4};
void f(int k){
if(k==4){
for(int i=0;i<4;i++){
cout<<a[i]<<" ";
}
cout<<endl;
}
for(int i=k;i<4;i++){
{
int t=a[i];
a[i]=a[k];
a[k]=t;
}
f(k+1);
{
int t=a[i];
a[i]=a[k];
a[k]=t;
}
}
}
int main(){
f(0);
return 0;
}
/*
1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 3 2
1 4 2 3
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 3 1
2 4 1 3
3 2 1 4
3 2 4 1
3 1 2 4
3 1 4 2
3 4 1 2
3 4 2 1
4 2 3 1
4 2 1 3
4 3 2 1
4 3 1 2
4 1 3 2
4 1 2 3
*/
方法二:
可以用 next_permutation
函数,并且该函数会保证生成的全排列是不重复的。
注意:
上面的生成全排列的模板不适用于 数组中出现 重复元素,因为出现重复元素时,交换两个位置的元素,得到的数组不变。
#include<bits/stdc++.h>
using namespace std;
int a[]={0,0,1,1};
void dfs(int k){
if(k==4){
for(int i=0;i<4;i++){
cout<<a[i]<<" ";
}
cout<<endl;
return;
}
for(int i=k;i<4;i++){
{
int t=a[i];
a[i]=a[k];
a[k]=t;
}
dfs(k+1);
{
int t=a[i];
a[i]=a[k];
a[k]=t;
}
}
}
int main(){
dfs(0);
return 0;
}
/*
0 0 1 1
0 0 1 1
0 1 0 1
0 1 1 0
0 1 1 0
0 1 0 1
0 0 1 1
0 0 1 1
0 1 0 1
0 1 1 0
0 1 1 0
0 1 0 1
1 0 0 1
1 0 1 0
1 0 0 1
1 0 1 0
1 1 0 0
1 1 0 0
1 0 1 0
1 0 0 1
1 1 0 0
1 1 0 0
1 0 1 0
1 0 0 1
*/
解决方案:
用next_permutation
函数,它会保证生成的全排列是不重复的。
#include<bits/stdc++.h>
using namespace std;
int a[]={0,0,1,1};
int main(){
do{
for(int i=0;i<4;i++){
cout<<a[i]<<" ";
}
cout<<endl;
}while(next_permutation(a,a+4));
return 0;
}
/*
0 0 1 1
0 1 0 1
0 1 1 0
1 0 0 1
1 0 1 0
1 1 0 0
*/
字符串也可用next_permutation
生成全排列
注意:在使用该函数生成全排列之前要先对排列的数组或字符串进行升序排序
#include<bits/stdc++.h>
using namespace std;
int main(){
string str="dabc";
sort(str.begin(),str.end());
do{
cout<<str<<endl;
}while(next_permutation(str.begin(),str.end()));
return 0;
}
/*
abcd
abdc
acbd
acdb
adbc
adcb
bacd
badc
bcad
bcda
bdac
bdca
cabd
cadb
cbad
cbda
cdab
cdba
dabc
dacb
dbac
dbca
dcab
dcba
*/
如果在排列前不对数组或字符串进行全排列,那么生成的全排列是数组元素之后的全排列
举例:
一个有序数组的全排列应该是
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
#include<bits/stdc++.h>
using namespace std;
int main(){
int a[]={1,2,3};
do{
for(int i=0;i<3;i++){
cout<<a[i]<<" ";
}
cout<<endl;
}while(next_permutation(a,a+3));
return 0;
}
/*
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
*/
但当输入的数组是2,1,3
时,此时生成的全排列为
2 1 3
2 3 1
3 1 2
3 2 1
#include<bits/stdc++.h>
using namespace std;
int main(){
int a[]={2,1,3};
do{
for(int i=0;i<3;i++){
cout<<a[i]<<" ";
}
cout<<endl;
}while(next_permutation(a,a+3));
return 0;
}
/*
2 1 3
2 3 1
3 1 2
3 2 1
*/
用全排列直接求出全排列的某一项
举例:
要求出第三项全排列的式子(第一项从1开始),根据观察可以得出,第三项应该是2,1,3
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
#include<bits/stdc++.h>
using namespace std;
int main(){
int a[]={1,2,3};
int n=1;
do{
if(n==3){
for(int i=0;i<3;i++){
cout<<a[i]<<" ";
}
cout<<endl;
}
n++;//用n来计数
}while(next_permutation(a,a+3));
return 0;
}
输出全排列的前n项
next_permutation(a,a+n)
,其中n表示要对数组中的前几个元素进行全排列。
当n等于数组的长度时,
#include<bits/stdc++.h>
using namespace std;
int main(){
int a[]={1,2,3};
int n=1;
do{
for(int i=0;i<3;i++){
cout<<a[i]<<" ";
}
cout<<endl;
}while(next_permutation(a,a+3));
return 0;
}
/*
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
*/
当n=2时,
#include<bits/stdc++.h>
using namespace std;
int main(){
int a[]={1,2,3};
int n=1;
do{
for(int i=0;i<3;i++){
cout<<a[i]<<" ";
}
cout<<endl;
}while(next_permutation(a,a+2));
return 0;
}
/*
1 2 3
2 1 3
*/
1 2 3
2 1 3
此时只对数组的前两个元素进行全排列
prev_permutation()函数功能是输出所有比当前排列小的排列,顺序是从大到小。
#include<bits/stdc++.h>
using namespace std;
int main(){
int a[]={3,2,1};
int n=1;
do{
for(int i=0;i<3;i++){
cout<<a[i]<<" ";
}
cout<<endl;
}while(prev_permutation(a,a+3));
return 0;
}
/*
3 2 1
3 1 2
2 3 1
2 1 3
1 3 2
1 2 3
*/
举例2:
#include<bits/stdc++.h>
using namespace std;
int main(){
int a[]={2,3,1};
int n=1;
do{
for(int i=0;i<3;i++){
cout<<a[i]<<" ";
}
cout<<endl;
}while(prev_permutation(a,a+3));
return 0;
}
/*
2 3 1
2 1 3
1 3 2
1 2 3
*/
next_permutation()函数功能是输出所有比当前排列大的排列,顺序是从小到大。
自定义排序方法
#include<bits/stdc++.h>
using namespace std;
int cmp(char a,char b) //'A'<'a'<'B'<'b'<...<'Z'<'z'.
{
if(tolower(a)!=tolower(b))
return tolower(a)<tolower(b);
else return a<b;
}
int main()
{
string str="daAC";
sort(str.begin(),str.end(),cmp);//AaCd
do{
printf("%s\n",str.c_str());
}while(next_permutation(str.begin(),str.end(),cmp));
return 0;
}
/*
AaCd
AadC
ACad
ACda
AdaC
AdCa
aACd
aAdC
aCAd
aCdA
adAC
adCA
CAad
CAda
CaAd
CadA
CdAa
CdaA
dAaC
dACa
daAC
daCA
dCAa
dCaA
*/