poj 1731 Order

Description 
对于给出的一个字符串,输出其所有不重复全排列 
Input 
一个字符串 
Output 
该字符串的全部不重复全排列 
Sample Input 
bbjd 
Sample Output 
bbdj 
bbjd 
bdbj 
bdjb 
bjbd 
bjdb 
dbbj 
dbjb 
djbb 
jbbd 
jbdb 

jdbb 


1.直接用next_permutation(),它不会产生重复串

直接上代码

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

int main()
{
	char s[300];
	int cnt;
	while(scanf("%s",s) != EOF)
	{
		sort(s,s+strlen(s));
		cnt = strlen(s);
		do
		{
			puts(s);
		}while(next_permutation(c,c+cnt));
	}
	return 0;
}
2.递归  先排序 再去重 (利用一个字符)

附代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int len,flag[300];
char str[300],res[300];
void dg(int x)
{
	char c = 0;
	if(x == len)
	{
		printf("%s\n",res);
		return;
	}
	for(int i = 0;i<len;i++)
	{
		if(!flag[i] && c != str[i])
		{
			res[x] = str[i];
			c = res[x];
			flag[i] = 1;
			dg(x + 1);
			flag[i] = 0;
		}
	}
}
int main()
{
	while(scanf("%s",str) != EOF)
	{
		memset(flag,0,sizeof(flag));
		len = strlen(str);
		sort(str,str+len);
		dg(0);
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值