标签:
http://poj.org/problem?id=1731
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 9702 | Accepted: 5925 |
Description
Input
Output
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 }
下面有关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\n",cnt);//输出302400 13 scanf("pause"); 14 }
next_permutation的返回值如下:如果变换后序列是非减序的则返回0,否则返回1。
所以如果想用do{...}while(next_permutation(...));的方式生成一个集合的全排列,必须先做sort。
即 便做了sort,从上面的例子我们也可以看出,当集合存在重复元素时,循环的次数并不是10!=3628800,302400是什么呢,恰是10!/ (2!*3!),也就是这个多重集的全排列数。可见在处理有重复元素的"集合"时,它是正确的且高效的,只要记住一定要先sort。
标签:
原文地址:http://www.cnblogs.com/jeff-wgc/p/4454507.html