zoukankan      html  css  js  c++  java
  • poj 1731 Orders

    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代码:

     1 #include <stdio.h>
     2 #include <algorithm>
     3 #include <iostream>
     4 #include <string.h>
     5 #include <string>
     6 #include <math.h>
     7 #include <stdlib.h>
     8 #include <queue>
     9 #include <stack>
    10 #include <set>
    11 #include <map>
    12 #include <list>
    13 #include <iomanip>
    14 #include <vector>
    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 <iostream>
     2 #include <algorithm>
     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
    ",cnt);//输出302400
    13     scanf("pause");
    14 }
    View Code

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

  • 相关阅读:
    一起谈.NET技术,ASP.NET的Session 详解 狼人:
    一起谈.NET技术,asp.net控件开发基础(22) 狼人:
    一起谈.NET技术,asp.net控件开发基础(18) 狼人:
    一起谈.NET技术,asp.net控件开发基础(13) 狼人:
    一起谈.NET技术,基于TFS2010的代码审查环境部署 狼人:
    一起谈.NET技术,asp.net控件开发基础(23) 狼人:
    Flex中计算某个月有多少天
    showModelDialog的使用(一)
    HDU2309:ICPC Score Totalizer Software
    Windows XP下vs2010中配置OpenCV2.4.3
  • 原文地址:https://www.cnblogs.com/jeff-wgc/p/4454507.html
Copyright © 2011-2022 走看看