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. 把Java程序打包成jar文件包并执行

    1.首先要确认自己写的程序有没有报错. 2.第一次我写的是Web Project到现在,我一直没有执行成功,所以最好创建的是java Project 打包步骤: 1.在项目上,右键,选择Export. ...

  2. android ListView详解继承ListActivity

    [转]http://www.cnblogs.com/allin/archive/2010/05/11/1732200.html 在android开发中ListView是比较常用的组件,它以列表的形式展 ...

  3. 【No.2 Ionic】Android打包

    项目做完之后 接下来就是打包操作了,接下来直接说Android 打包操作 生成签名证书 keytool -genkey -alias vincentguo -keyalg RSA -validity  ...

  4. php项目域名绑定和替换

    ---恢复内容开始--- 在apache的httpd.conf里面找到 DocumentRoot "E:/xampp/htdocs"<Directory "E:/x ...

  5. 分享一个开源的流程图绘制软件--Diagram Designer

    最近在写专利文件,在制作说明书附图时想到自己还只会用wps进行简单的绘制,于是想学习下,填补下这方面的短板.这两天查到了DiagramDesigner这个小工具,派上了大用场.用它写完了一个发明专利, ...

  6. java jmx

    http://blog.csdn.net/qiao000_000/article/details/6063949 一.JMX简介 什么是JMX?在一篇网文中是这样说的:"JMX(Java M ...

  7. linux实现c多进程

    线程(thread)技术早在60年代就被提出,但真正应用多线程到操作系统中去,是在80年代中期,solaris是这方面的佼佼者.传统的Unix也支持线程的概念,但是在一个进程(process)中只允许 ...

  8. 规则html表单对象赋值

    function grid_load_callback(data, status) {            if (data.rows.length > 0)            {     ...

  9. start from here

    简单介绍下,小码农一只,工作两年左右,从事移动端游戏开发工作. 人类,精神正常,男,爱好游戏音乐足球美女,if(有妹子) 喜欢旅游;,取向正常. 很喜欢java,喜欢关注新技术, 应一些朋友的建议,今 ...

  10. Spark Programming--Actions

    first def first(): T first返回RDD中的第一个元素,不排序. 例子: count def count(): Long count返回RDD中的元素数量 例子: reduce ...