Orders - 1731

Orders
Time Limit: 1000MS
Memory Limit: 10000K
Total Submissions: 9954
Accepted: 6053

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

Source

题意就是给一个字符串,然后按照字典顺序进行全排列,有字母重复的情况算一种。

最简单的方法是用C++的库函数next_permutation函数来实现:

next_permutation的函数声明:#include  <algorithm>

bool next_permutation( iterator start, iterator end);

next_permutation函数的返回值是布尔类型,在STL中还有perv_permutation()函数。

next_permutation()函数功能是输出所有比当前排列大的排列,顺序是从小到大。

prev_permutation()函数功能是输出所有比当前排列小的排列,顺序是从大到小。


next_permutation函数的原理如下:

在当前序列中,从尾端向前寻找两个相邻元素,前一个记为*i,后一个记为*t,并且满足*i < *t。然后再从尾端

寻找另一个元素*j,如果满足*i < *j,即将第i个元素与第j个元素对调,并将第t个元素之后(包括t)的所有元

素颠倒排序,即求出下一个序列了。

实现代码如下:

/***********************************************************************
    Copyright (c) 2015,wangzp
    All rights no reserved.
  
    Name: 《Orders》In PEKING UNIVERSITY ACM
    ID:   PROBLEM 1731
    问题简述: 把一个字符串字母顺序全排列,有重复的字母算一种
	算法描述:用C++类库比较简单,学好C++,走遍天下有人夸。
    Date: Oct 11, 2015 
 
***********************************************************************/
#include   
#include   
#include   
#include   
using namespace std;  
const int MAXN = 220;  

char str[MAXN];  

int main(void)
{  
	while (scanf("%s", str) != EOF)
	{  
		int n = strlen(str);  
		sort(str, str+n);  
		printf("%s\n", str);  
		while (next_permutation(str, str+n))   
			printf("%s\n", str);  
	}  
	return 0;  
}  


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章