蓝桥杯算法提高-全排列(DFS、STL)

内存限制:256.0MB C/C++时间限制:1.0s Java时间限制:3.0s Python时间限制:5.0s

输出自然数1输出自然数1到n所有不重复的排列,即n的全排列。到n所有不重复的排列,即n的全排列。

方法一:DFS
在基础里讲过

#include<iostream> 

using namespace std;

const int N = 1000;
int path[N];
bool st[N];
int n;
void dfs(int x)
{
	if(x == n)
	{
		for(int i = 0; i < n; i++) cout << path[i];
		cout << endl;
		return;
	}
	for(int i = 1; i <= n; i++)
	{
		if(!st[i])
		{
			st[i] = true;
			path[x] = i;
			dfs(x + 1);
			st[i] = false;
		}
	}
}




int main() {
	
	cin >> n;
	dfs(0);
	
	return 0;
}

方法二:STL
使用的函数为next_permutation(start,end),它与prev_permutation(start,end)是一对,前者是搜索下一个排列,后者是搜索上一个。
首先我们必须了解什么是“下一个”排列组合,什么是“上一个”排列组合。考虑三个字符所组成的序列{a,b,c}。
这个序列有六个可能的排列组合:abc,acb,bac,bca,cab,cba。这些排列组合根据less-than操作符做字典顺序(lexicographical)的排序。也就是说,abc名列第一,因为每一个元素都小于其后的元素。acb是次一个排列组合,因为它是固定了a(序列内最小元素)之后所做的新组合。
同样道理,那些固定b(序列中次小元素)而做的排列组合,在次序上将先于那些固定c而做的排列组合。以bac和bca为例,bac在bca之前,因为次序ac小于序列ca。面对bca,我们可以说其前一个排列组合是bac,而其后一个排列组合是cab。序列abc没有“上一个”排列组合,cba没有“下一个”排列组合。 next_permutation()会取得[first,last)所标示之序列的下一个排列组合,如果没有下一个排列组合,便返回false;否则返回true。这个算法有两个版本。版本一使用元素型别所提供的less-than操作符来决定下一个排列组合,版本二则是以仿函数comp来决定。

#include<iostream> 
#include<algorithm>
using namespace std;

int n;


int main() {
	
	cin >> n;
	int a[n];
	for(int i = 1; i <= n; i++)
	{
		a[i - 1] = i;
	}
	
	do
	{
		for(int i = 0; i < n; i++)
		{
			cout << a[i];
		}
		cout << endl;
	}while(next_permutation(a, a + sizeof(a) / sizeof(a[0])));

	
	return 0;
}

拓展一下next_permutation()用法,如果对没有按照升序排列的数组使用,则不会得到所有的排列:
在这里插入图片描述
可以先用while(next_permutation(a, a + 3))让数组变成最小序列
在这里插入图片描述
但这样会有达到最小序列的开销,可以用一个副本先对原来数组进行备份,然后改变循环条件,从而避免达到最小序列的开销

#include<iostream> 
#include<algorithm>
using namespace std;


bool cmp(int *a, int *b)
{
	int n = sizeof(a) / sizeof(a[0]);
	for(int i = 0; i < n; i++)
	{
		if(a[i] != b[i]) return false;
	}
	return true;
}

int main() {
	
	int a[3] = {2,1,3};
	int b[3];
	memcpy(b, a, 3 * sizeof(int));
	do
	{
		for(int i = 0; i < 3; i++)
		{
			cout << b[i];
		}
		cout << endl;
		next_permutation(b, b + 3);
	}while(!cmp(a, b));
	
		
	
	return 0;
}

在这里插入图片描述
当然这里用vector更好,因为可以直接复制和比较

#include<iostream> 
#include<algorithm>
#include <vector>
using namespace std;




int main() {
	
	vector<int> a = {2,1,3};
	auto b = a;
	do
	{
		for(int i = 0; i < 3; i++)
		{
			cout << b[i];
		}
		cout << endl;
		next_permutation(b.begin(), b.end());
	}while(a != b);
	
		
	
	return 0;
}

在这里插入图片描述

此外,next_permutation(node,node+n,cmp)可以对结构体num按照自定义的排序方式cmp进行排序。
也可以对字符…
next_permutation 自定义比较函数 POJ 1256
题目中要求的字典序是
‘A’<‘a’<‘B’<‘b’<…<‘Z’<‘z’

#include<iostream> //poj 1256 Anagram
#include<string>
#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[20];
	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 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值