全排列dfs

7-3 字符串的全排列

分数 20

全屏浏览题目

切换布局

作者 吴云鹏

单位 郑州大学

给定一个全由小写字母构成的字符串,求它的全排列,按照字典序从小到大输出。

输入格式:

一行,一个字符串,长度不大于8。

输出格式:

输出所有全排列,每行一种排列形式,字典序从小到大。

输入样例:

在这里给出一组输入。例如:

abc

输出样例:

在这里给出相应的输出。例如:

abc
acb
bac
bca
cab
cba

代码长度限制

16 KB

时间限制

1000 ms

内存限制

64 MB

#include <iostream>
#include <algorithm>
#include <cstring> 

using namespace std;

const int N = 10;
char a[N];
int path[N], st[N]; 
int n;

void dfs(int u)
{
	if(u == n )
	{
		for(int i = 0; i < n; i ++)	cout << a[path[i]];
		puts(""); 
	}
	for(int i = 0; i < n; i ++)
	{
		if(st[i])	continue;
		path[u] = i;
		st[i] = 1;
		dfs(u + 1);
		st[i] = 0;
	}
}


int main()
{
	scanf("%s", a);
	for(int i = 0; a[i]; i ++)	n ++;
	sort(a, a + n); 
	dfs(0); 
	return 0;
} 

posted @   cxy8  阅读(52)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示