dfs生成全排列

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
*/
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序媛小y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值