0829-全排列函数-POJ 1256

版权声明:虽然我只是个小蒟蒻但转载也请注明出处哦 https://blog.csdn.net/weixin_42557561/article/details/82185663

传送门

大致题意

给定字典序: 'A'<'a'<'B'<'b'<...<'Z'<'z'.

让你对输入的一串字母,按照上面定义的大小关系,从小到大输出所有的全排列情况

分析

普遍做法: dfs

但今天get了一个超棒的求全排列的函数,虽然复杂度和手写的 dfs 差不多,但一下子代码就简单起来了,多棒啊!

-->参考<--

头文件    #include<algorithm>

主程序   next_permutation ( start , end ) 

作用       求当前排列的下一个排列

返回值   存在下一个排列的时候返回true,并修改原排列中的值;不存在的时候返回false

注意事项  数组中的数一开始应该是从小到大排好的,这样调用函数的时候才会把所有情况都列举出来

 由于这个函数的排列也可以自定义,那么针对这道题就好办啦,把所有大写的都看做小写进行比较,稍特殊一点的就是同一个字母的大小写,由于大写字母的ascii码小于小写字母的ascii码,就直接比较即可

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<queue> 
using namespace std;
inline bool cmp(char a,char b){
	if(tolower(a)!=tolower(b))	return tolower(a)<tolower(b);//这又是一个懒人必备函数,返回当前字母的小写字母
	return a<b;
}
int n;
char st[15];
int main(){
	scanf("%d",&n);
	int i,j,k;
	for(i=1;i<=n;++i){
		scanf("%s",st);
		sort(st,st+strlen(st),cmp);
		do{
            printf("%s\n",st);
        }while((next_permutation(st,st+strlen(st),cmp)));
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42557561/article/details/82185663