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

bbdj
bbjd
bdbj
bdjb
bjbd
bjdb
dbbj
dbjb
djbb
jbbd
jbdb
jdbb 分析: STL里面有个next_permutation(),秒杀,,, AC代码:
 #include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <list>
#include <iomanip>
#include <vector>
#pragma comment(linker, "/STACK:1024000000,1024000000")
#pragma warning(disable:4786) using namespace std; const int INF = 0x3f3f3f3f;
const int MAX = + ;
const double eps = 1e-;
const double PI = acos(-1.0); int main()
{
string str;
while(cin >> str)
{
sort(&str[] , &str[] + str.length());
cout << str << endl;
while(next_permutation(&str[],&str[]+str.length()))
{
cout << str << endl;
}
}
return ;
}

下面有关next_permutation的介绍

 #include <iostream>
#include <algorithm>
using namespace std; int main()
{
int a[]={,,,,,,,,,};
int cnt=;
do{
cnt++;
}while(next_permutation(a,a+));
printf("%d\n",cnt);//输出302400
scanf("pause");
}

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

poj 1731 Orders的更多相关文章

  1. POJ 1731 Orders(STL运用)

    题目地址:POJ 1731 这题能够直接用STL函数做,非常轻松..next_permutation函数非常给力.. 代码例如以下: #include <algorithm> #inclu ...

  2. poj 1731 Orders(暴力)

    题目链接:http://poj.org/problem?id=1731 思路分析:含有重复元素的全排列问题:元素个数为200个,采用暴力枚举法. 代码如下: #include <iostream ...

  3. Orders POJ - 1731

    The stores manager has sorted all kinds of goods in an alphabetical order of their labels. All the k ...

  4. POJ 1731:Orders

    Orders Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9940   Accepted: 6048 Descriptio ...

  5. POJ 1731

    #include<iostream> #include<string> #include<algorithm> using namespace std; int m ...

  6. 是时候学一波STL了。。。

    都到如今了还不会STL,赶紧学习一下. .. 头文件#include<algorithm> 加上 using namespace std. 求下一个排列的函数:next_permutati ...

  7. POJ 1270 Following Orders

    Following Orders Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4902   Accepted: 1982 ...

  8. POJ 1270 Following Orders 拓扑排序

    http://poj.org/problem?id=1270 题目大意: 给你一串序列,然后再给你他们部分的大小,要求你输出他们从小到大的所有排列. 如a b f g 然后 a<b ,b< ...

  9. poj 1270 Following Orders (拓扑排序+回溯)

    Following Orders Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5473   Accepted: 2239 ...

随机推荐

  1. php 请求参数限制

    公司有个群发短信的小项目,项目上线了很久也没有什么问题,最近有商家说 我短信群发不能用 现象是:发现有时候可以发送,有时候不可以发送,看截图发送的手机数量不一样 通过调试php代码发现 php 只接受 ...

  2. Sql Server建立链接服务器访问Access的MDB数据库

    EXEC master.dbo.sp_addlinkedserver @server = N'test', @srvproduct=N'OLE DB Provider for Jet', @provi ...

  3. NRF51822之app_button使用

    我们现在开始使用app_button,为什么要使用这个来替代直接使用GPIOTE呢? 因为我们在手册中可以看到如果一直开启GPIOTE in模式的需要需要很大电流.所以我们需要使用RTC来“周期”的查 ...

  4. docker-compose bug

    annot mount volume over existing file, file exists /var/lib/docker/aufs/mnt/0ac71fed1af802a4ecf4a93b ...

  5. docker sonaqube

    postgresql:   image: orchardup/postgresql:latest   environment:   - POSTGRESQL_USER=sonar   - POSTGR ...

  6. 行高不设单位的好处 line-height:1.8

    今天无意间看了到了line-height:1.8 感觉挺有意思的,然后翻了下行高的知识,发现还挺有文章的,不妨温故而知新. 先回顾下:顶线.中线.基线.底线 vertical-align是元素的垂直对 ...

  7. 针对JD-GUI

    switch(0){ case 1001: JSONObject jsoObj; String data = null; boolean isclose = false; try{ jsoObj = ...

  8. iOS 使用封装的NSLog来打印调试信息

    //DLog代替NSLog //debugMethod() 代替 NSLog(@"%s", __func__) //DLog在release下不会输出 #ifdef DEBUG # ...

  9. iOS 使用Xcode和Instruments调试解决iOS内存泄露(链接转)

    http://blog.csdn.net/totogo2010/article/details/8233565

  10. css3画图之大白(●—●)

    把大白送给你~ <!DOCTYPE html> <html> <head> <title>大白</title> <meta http- ...