STL实现全排列 next_permutation

在#include<algorithm>的头文件中

next_permutation(a,a+len);

返回的是一个bool类型的值;直到找不到全排列为止,返回false。

并且对全排列重复出现的情况可以过滤,即最后得出的全排列的结果中,不会有相同的出现。

并且貌似只能从小到大的产生排列,也就是说,如果a中的字符串为刚好从大到小,那么就一组都产生不了。

 

Orders
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 8117   Accepted: 5059

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


即求字符串从小到大的全排列;



代码实现·:
#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;
}

 

posted on 2012-10-03 22:04  yumao  阅读(3094)  评论(0编辑  收藏  举报

导航