链接:http://poj.org/problem?id=1731
Orders
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 9933 | Accepted: 6043 |
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.
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
大意——商店经理将各种各样的商品按照它的标签的字母顺序进行了分类。所有标签开始字母相同的商品将被放置在以这个字母作为标签的仓库里。这些天,商店经理接收到一些商品的订单,将要从仓库里发货了。每个订单仅仅要求一种商品,商店经理将按预订顺序处理这些订单。今天你提前知道了经理将要处理的所有订单,但是你不知道它们的预订顺序,计算出这一天经理为处理订单需要一个一个拜访仓库的所有可能方式。
思路——将问题转化一下即为:给你一个字符串,长度不超过200,求其不重复的全排列,输出不超过2M字节。输出全排列,可以用STL中的next_permutation函数解决。在此之前,只需要将字符串按字典序排序即可,然后每次生成一个全排列就输出一次,直至没有了全排列为止。
复杂度分析——时间复杂度:O(len*log(len)),空间复杂度:O(len)
附上AC代码:
#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <iomanip>
#include <ctime>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <stack>
//#pragma comment(linker, "/STACK:102400000, 102400000")
using namespace std;
typedef long long ll;
const double pi = acos(-1.0);
const double e = exp(1.0);
const double eps = 1e-8;
const short maxlen = 205;
char str[maxlen];
int main()
{
ios::sync_with_stdio(false);
while (~scanf("%s", str))
{
short len = strlen(str);
sort(str, str+len);
do{
printf("%s\n", str);
} while (next_permutation(str, str+len)); // STL,生成不重复的全排列
}
return 0;
}