博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 1731 Orders
阅读量:4483 次
发布时间:2019-06-08

本文共 3126 字,大约阅读时间需要 10 分钟。

http://poj.org/problem?id=1731

Orders
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 9702   Accepted: 5925

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

bbdjbbjdbdbjbdjbbjbdbjdbdbbjdbjbdjbbjbbdjbdbjdbb 分析: STL里面有个next_permutation(),秒杀,,, AC代码:
1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 #include
15 #pragma comment(linker, "/STACK:1024000000,1024000000")16 #pragma warning(disable:4786)17 18 using namespace std;19 20 const int INF = 0x3f3f3f3f;21 const int MAX = 10000 + 10;22 const double eps = 1e-8;23 const double PI = acos(-1.0);24 25 int main()26 {27 string str;28 while(cin >> str)29 {30 sort(&str[0] , &str[0] + str.length());31 cout << str << endl;32 while(next_permutation(&str[0],&str[0]+str.length()))33 {34 cout << str << endl;35 }36 }37 return 0;38 }
View Code

下面有关next_permutation的介绍

1 #include 
2 #include
3 using namespace std; 4 5 int main() 6 { 7 int a[10]={
1,2,2,3,3,3,4,5,6,7}; 8 int cnt=0; 9 do{10 cnt++;11 }while(next_permutation(a,a+10)); 12 printf("%d\n",cnt);//输出30240013 scanf("pause");14 }
View Code

 

next_permutation的返回值如下:如果变换后序列是非减序的则返回0,否则返回1。

所以如果想用do{...}while(next_permutation(...));的方式生成一个集合的全排列,必须先做sort。
即 便做了sort,从上面的例子我们也可以看出,当集合存在重复元素时,循环的次数并不是10!=3628800,302400是什么呢,恰是10!/ (2!*3!),也就是这个多重集的全排列数。可见在处理有重复元素的"集合"时,它是正确的且高效的,只要记住一定要先sort。

 

转载于:https://www.cnblogs.com/jeff-wgc/p/4454507.html

你可能感兴趣的文章
jsp中${}是EL表达式的常规表示方式
查看>>
GoldenGate常见问题及处理
查看>>
Android JNI学习(五)——Demo演示
查看>>
SSRS 呈现Barcode Free
查看>>
java快速排序引起的StackOverflowError异常
查看>>
泛函编程(35)-泛函Stream IO:IO处理过程-IO Process
查看>>
-XX:-PrintClassHistogram 按下Ctrl+Break后,打印类的信息
查看>>
mac 安装php redis扩展
查看>>
css3中Animation
查看>>
JS 判断是否是手机端并跳转操作
查看>>
最短路径问题(dijkstra-模板)
查看>>
c# 导出表格 api
查看>>
使用Android NDK以及JNI编写应用
查看>>
学习笔记之-php数组数据结构
查看>>
初学者--bootstrap(六)组件中的下拉菜单----在路上(10)
查看>>
QMetaObject::connectSlotsByName 总结
查看>>
Android 微信支付步骤
查看>>
js操作table
查看>>
JQuery学习系列篇(一)
查看>>
Centos7 minimal 系列之rabbitmq安装(八)
查看>>