STL实现全排列 next_permutation
2012-10-03 22:04
330 查看
在#include<algorithm>的头文件中
next_permutation(a,a+len);
返回的是一个bool类型的值;直到找不到全排列为止,返回false。
并且对全排列重复出现的情况可以过滤,即最后得出的全排列的结果中,不会有相同的出现。
并且貌似只能从小到大的产生排列,也就是说,如果a中的字符串为刚好从大到小,那么就一组都产生不了。
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
Sample Output
next_permutation(a,a+len);
返回的是一个bool类型的值;直到找不到全排列为止,返回false。
并且对全排列重复出现的情况可以过滤,即最后得出的全排列的结果中,不会有相同的出现。
并且貌似只能从小到大的产生排列,也就是说,如果a中的字符串为刚好从大到小,那么就一组都产生不了。
Orders
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 8117 | Accepted: 5059 |
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
bbjdbdbj
bdjb
bjbd
bjdb
dbbj
dbjb
djbb
jbbd
jbdb
jdbb
即求字符串从小到大的全排列;
代码实现·:
#include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; char a[210]; int main(){ while(scanf("%s",a)!=EOF){ int len=strlen(a); sort(a,a+len); cout<<a<<endl; while(next_permutation(a,a+len)){ cout<<a<<endl; } } return 0; }
相关文章推荐
- STL实现全排列 next_permutation
- stl库之next_permutation实现全排列(二)
- 用STL的next_permutation算法实现全排列
- C++ STL next_permutation的实现原理
- STL算法之 next_permutation、prev_permutation 的原理和实现
- 全排列(一)next_permutation方式实现
- STL next_permutation(a,a+n) 生成一个序列的全排列。满足可重集。
- stl next_permutation的全排列原理及实现样例
- 枚举全排列 STL next_permutation
- 打印全排列和stl::next_permutation
- STL: <algorithm>中 next_permutation()生成全排列
- 详解STL中next_permutation()函数实现
- 自己实现了一下C++STL中的next_permutation,名为ant_next_permutation,发下代码
- 九度OJ 1120 全排列 -- 实现C++STL中next_permutation()
- C++实现全排列(调用next_permutation)
- 【STL】next_permutation的实现
- 九度OJ 1120 全排列 -- 实现C++STL中next_permutation()
- stl全排列 qsort+next_permutation(a,a+n) -->全排列
- stl全排列 qsort+next_permutation(a,a+n) -->全排列 .
- 【C++_STL_next_permutation】全排列讲解