POJ 1731 Orders(带重复字母的全排列 + 暴力)

POJ 1731 Orders

Description

The stores manager has sorted all kinds of goods in an alphabetical order of their labels. All the kinds having labels starting with the same letter are stored in the same warehouse (i.e. in the same building) labelled with this
letter. During the day the stores manager receives and books the orders of goods which are to be delivered from the store. Each order requires only one kind of goods. The stores manager processes the requests in the order of their booking.

You know in advance all the orders which will have to be processed by the stores manager today, but you do not know their booking order. Compute all possible ways of the visits of warehouses for the stores manager to settle all the demands piece after piece
during the day.

Input

Input contains a single line with all labels of the requested goods (in random order). Each kind of goods is represented by the starting letter of its label. Only small letters of the English alphabet are used. The number of orders
doesn‘t exceed 200.

Output

Output will contain all possible orderings in which the stores manager may visit his warehouses. Every warehouse is represented by a single small letter of the English alphabet -- the starting letter of the label of the goods.
Each ordering of warehouses is written in the output file only once on a separate line and all the lines containing orderings have to be sorted in an alphabetical order (see the example). No output will exceed 2 megabytes.

Sample Input

bbjd

Sample Output

bbdj
bbjd
bdbj
bdjb
bjbd
bjdb
dbbj
dbjb
djbb
jbbd
jbdb
jdbb

题目大意:给出一串字符串,要求输出其不重复的全排列,要求起全排列从小到大。

解题思路:因为输出的全排列要求从小到大,所以先对起始字符串进行排序,之后用递归和DFS的思想输出其全排列序列。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int vis[205], len;
char ch[205], temp[205];
void print(int cnt) {
	char c = 0;
	if (cnt == len) {
		printf("%s\n", temp);
		return;
	}
	for (int i = 0; i < len; i++) {
		if (!vis[i] && c != ch[i]) {    //实现不重复输出同一节点,并对不同的相同值节点进行处理
			temp[cnt] = ch[i];
			c = temp[cnt];
			vis[i] = 1;  //标记  正在访问
			print(cnt + 1);
			vis[i] = 0;  //释放
		}
	}
}
int main() {
	memset(ch, 0, sizeof(ch));
	memset(temp, 0, sizeof(temp));
	memset(vis, 0, sizeof(vis));
	scanf("%s", ch);
	len = strlen(ch);
	sort(ch, ch + len);
	print(0);
	return 0;
}
时间: 2024-08-17 12:57:01

POJ 1731 Orders(带重复字母的全排列 + 暴力)的相关文章

POJ - 1731 Orders

题意:排序后字符串全排列 思路:好久没水一题了 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAXN = 220; char str[MAXN]; int main() { while (scanf("%s", str) != EOF) { int n = strle

九章算法面试题54 带重复元素的全排列

九章算法官网-原文网址 http://www.jiuzhang.com/problem/54/ 题目 给定一个带重复元素的整数集合,求出这个集合中所有元素的全排列.对于集合[1,1,2],其本质不同的全排列有三个,分别为: [1,1,2] [1,2,1] [2,1,1] 在线测试本题 http://lintcode.com/problem/unique-permutations/ 解答 首先做这个题目之前,要先会不带重复元素的全排列. 程序参考:http://www.ninechapter.co

POJ 1731 Orders(STL运用)

题目地址:POJ 1731 这题能够直接用STL函数做,非常轻松..next_permutation函数非常给力.. 代码例如以下: #include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <queue> #include <cmath> #include <st

带重复的全排列问题

明白带重复的全排列首先要明白不带重复的全排列(不带重复的全排列链接) 在不带重复的全排列中说到在排列1,2,3,4插入5有5种方式,会生成5种新的排列. 如果我们在1,1,2,3中插如4也有五种方式, (1)4,1,1,2,3 (2)1,4,1,2,3 (3)1,1,4,2,3 (4)1,1,2,4,3 (5)1,1,2,3,4 生成了五种排列 但如果在1,2,3,4中插如1 (1)1,1,2,3,4 (2)1,1,2,3,4 (3)1,2,1,3,4 (4)1,2,3,1,4 (5)1,2,3

lintcode 中等题:subsets II 带重复元素的子集

题目 带重复元素的子集 给定一个可能具有重复数字的列表,返回其所有可能的子集 样例 如果 S = [1,2,2],一个可能的答案为: [ [2], [1], [1,2,2], [2,2], [1,2], [] ] 注意 子集中的每个元素都是非降序的 两个子集间的顺序是无关紧要的 解集中不能包含重复子集 挑战 你可以同时用递归与非递归的方式解决么? 解题 一个很简单的想法就是在上一题目中增加判断是否已经存在某个子集 class Solution: """ @param S: A

将字符串中连续出现的重复字母进行压缩

原文地址:http://leihuang.net/2014/05/19/List-Interviews/ 单链表的一些常见面试题汇总 单链表反转/逆序 求单链表倒数第N个数 找到单链表的中间结点 如何判断链表是否有环的存在 单链表建环,无环链表变有环 如何知道环的长度? 如何找出环的连接点在哪里? 删除单链表中的重复元素 下面我先简单叙述一下每道题的思路,然后把实现的程序一起贴出来,不会讲得太细,我觉得只要有了思路之后,接下来的难点就是语言上的一些细节问题了,这个不自己去实现,听别人讲是体会不到

[LeetCode] Remove Duplicate Letters 移除重复字母

Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical order among all possible results. Example: Given "bcabc&q

每天一道算法题(39)——含有重复字符的全排列

思路 (1)对于含有重复字符的全排列必须使用isSwap函数 (2)整体思路 a,交换当前子字符串(i----n-1)字符与子字符串后面的每一个位置的字符(满足交换条件下) b,子字符串位置后移(i+1-----n).递归处理子字符串 c,将a中的交换复原. 代码 #include <iostream> #include"string" using namespace std; void swap(char& a,char& b){//交换 char tem

poj 2912 Rochambeau(带权并查集 + 暴力)

题目:poj 2912 Rochambeau(带权并查集 + 暴力) 题目大意:题目给出三个团队和一个裁判,这三个团队和裁判一起玩剪刀石头布,然后规定每个团队必须出一样的,只有裁判可以任意出.然后给出关系,x > y 代表 x 赢y , x < y代表 y 赢 x , 相等则出的一样.问这样的关系可以推出裁判是哪个吗?可以需要说明从第一条到第几条推出来的,不可以也要说明是不可能出现这样的关系,还是裁判不唯一. 解题思路:这题重点是裁判在里面会扰乱关系,并且n * m 才 100000,完全可以