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 have labels starting with the same letter is stored in the same warehouse (i.e. in the same building) lab Elled. During the day the stores manager receives and books the orders of goods which is to being 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'll has to be processed by the stores manager today, but don't know their Booking order. Compute all possible ways of the visits of warehouses in the stores manager to settle all the demands piece after piece D Uring the day.
Input
Input contains a single line with all labels of the requested goods (in random order). Each kind of goods are represented by the starting letter of their label. Only small letters of the Chinese alphabet is used. The number of orders doesn ' t exceed 200.
Output
Output would contain all possible orderings in which the stores manager could visit his warehouses. Every warehouse is represented by a single small letter of the Chinese alphabet-the starting letter of the label of the Goods. Each ordering of warehouses was written in the output file was once on a separate line and all the lines containing orderi NGS has the sorted in a alphabetical order (see the example). No output would exceed 2 megabytes.
Sample Input
Bbjd
Sample Output
Bbdjbbjdbdbjbdjbbjbdbjdbdbbjdbjbdjbbjbbdjbdbjdbb
Analysis:
STL has a next_permutation (), a second kill,,,,
AC Code:
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>Ten#include <Set> One#include <map> A#include <list> -#include <iomanip> -#include <vector> the #pragmaComment (linker, "/stack:1024000000,1024000000") - #pragmaWarning (disable:4786) - - using namespacestd; + - Const intINF =0x3f3f3f3f; + Const intMAX =10000+Ten; A Const DoubleEPS = 1e-8; at Const DoublePI = ACOs (-1.0); - - intMain () - { - stringstr; - while(Cin >>str) in { -Sort (&str[0], &str[0] +str.length ()); tocout << str <<Endl; + while(Next_permutation (&str[0],&str[0]+str.length ())) - { thecout << str <<Endl; * } $ }Panax Notoginseng return 0; -}
View Code
The following is an introduction to Next_permutation
1#include <iostream>2#include <algorithm>3 using namespacestd;4 5 intMain ()6 {7 inta[Ten]={1,2,2,3,3,3,4,5,6,7};8 intCnt=0;9 Do{Tencnt++; One} while(Next_permutation (a,a+Ten)); Aprintf"%d\n", CNT);//Output 302400 -scanf"Pause"); -}
View Code
The return value of the next_permutation is as follows: If the transformed sequence is non-decrement, it returns 0, otherwise 1 is returned.
So if you want to use do{...} while (Next_permutation (...)); The way to generate a set of full permutations, you must first do sort.
Even if we do sort, we can see from the above example that when a set has duplicate elements, the number of cycles is not what 10!=3628800,302400 is, just 10!/(2!*3!), which is the total number of permutations of this multiset. It can be seen that when dealing with a "collection" of duplicate elements, it is correct and efficient, as long as you remember to sort first.
POJ 1731 Orders