STL - next_permutation 全排列函数

学习:

http://blog.sina.com.cn/s/blog_9f7ea4390101101u.html

http://blog.csdn.net/ac_gibson/article/details/45308645

https://blog.csdn.net/HowardEmily/article/details/68064377

 

next_permutation(start,end)和 prev_permutation(start,end)。

这两个函数作用是一样的,区别就在于:

前者求的是当前排列的下一个排列

后者求的是当前排列的上一个排列

至于这里的“前一个”和“后一个”,我们可以把它理解为序列的字典序的前后,

严格来讲,就是对于当前序列pn,他的下一个序列pn+1满足:不存在另外的序列pm,使pn<pm<pn+

 

用法: 通常用调入字符串或数组 头指针 和 头指针+对前几位进行全排列。

 

POJ 1256

 1 #include<iostream> //poj 1256 Anagram
 2 #include<string>
 3 #include<algorithm>
 4 using namespace std;
 5 int cmp(char a,char b)      //自定义字典序
 6 {
 7      if(tolower(a)!=tolower(b))//tolower 是将大写字母转化为小写字母.
 8      return tolower(a)<tolower(b);
 9      else
10      return a<b;
11 }
12 int main()
13 {
14     char ch[20];
15     int n;
16     cin>>n;
17     while(n--)
18     {
19         scanf("%s",ch);
20         sort(ch,ch+strlen(ch),cmp);
21          do
22         {
23             printf("%s\n",ch);
24         }while(next_permutation(ch,ch+strlen(ch),cmp));
25     }
26      return 0;
27 }

 

posted @ 2019-02-27 19:57  莜莫  阅读(169)  评论(0编辑  收藏  举报