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;  
}  


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