next_permutation函数的使用

在网络上发现了好多篇类似的文章,下面给出链接:

首先,我要声明一点,现在的NOI/NOIP可以用STL模板,也就意味着我们可以用next_permutation来生成全排列。

1、http://www.slyar.com/blog/stl_next_permutation.html

下午研究了一下全排列算法,然后发现C++的STL有一个函数可以方便地生成全排列,这就是next_permutation

在C++ Reference中查看了一下next_permutation的函数声明:

#include <algorithm>
bool next_permutation( iterator start, iterator end );

The next_permutation() function attempts to transform the given range of elements [start,end) into the next lexicographically greater permutation of elements. If it succeeds, it returns true, otherwise, it returns false.

从说明中可以看到 next_permutation 的返回值是布尔类型。按照提示写了一个标准C++程序:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <algorithm>
#include <string>
 
using namespace std;
 
int main()
{
    string str;
    cin >> str;
    sort(str.begin(), str.end());
    cout << str << endl;
    while (next_permutation(str.begin(), str.end()))
    {
        cout << str << endl;
    }
    return 0;
}

其中还用到了 sort 函数和 string.begin()、string.end() ,函数声明如下:

#include <algorithm>
void sort( iterator start, iterator end );

sort函数可以使用NlogN的复杂度对参数范围内的数据进行排序。

#include <string>
iterator begin();
const_iterator begin() const;

#include <string>
iterator end();
const_iterator end() const;

string.begin()和string.end() 可以快速访问到字符串的首字符和尾字符。

在使用大数据测试的时候,发现标准C++的效率很差...换成C函数写一下,效率提升了不止一倍...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <cstdio>
#include <algorithm>
#include <cstring>
#define MAX 100
 
using namespace std;
 
int main()
{
    int length;
    char str[MAX];
    gets(str);
    length = strlen(str);
    sort(str, str + length);
    puts(str);
    while (next_permutation(str, str + length))
    {
        puts(str);
    }
    return 0;
}

2、http://www.cnblogs.com/qsort/archive/2011/08/11/2134338.html

How to systematically generate all the permutations of a given sequence?

see http://en.wikipedia.org/wiki/Next_permutation

1, Find the largest index k such that a[k] < a[k + 1]. If no such index exists, the permutation is the last permutation.

2, Find the largest index l such that a[k] < a[l]. Since k+1 is such an index, l is well defined and satisfies k < l.

3, Swap a[k] with a[l].

4, Reverse the sequence from a[k+1] up to and including the last element a[n].

第一步以后,a[k]以后的是一个递减序列,已经是最大的了,再折腾也没用;

第二步,如果带上a[k],那么lexicographical order的下一个一定是以比a[k]大的一个数打头的,从后面找到刚好比a[k]大的那一个,假设是a[l];

第三步,将a[l]提到前面,与a[k]互换,这时候,a[k]后面的仍然是降序的。

第四步,把a[k]后面的逆转一下,从降序到升序,这样就得到了恰好比之前序列大一号的序列(打头的是刚好更大的那个,后面的是升序)。

3、http://blog.csdn.net/aipb2008/article/details/2227490

 

在标准库算法中,next_permutation应用在数列操作上比较广泛.这个函数可以计算一组数据的全排列.但是怎么用,原理如何,我做了简单的剖析.

 

首先查看stl中相关信息.
函数原型:

 

template<class BidirectionalIterator>
   bool next_permutation(
      BidirectionalIterator _First
      BidirectionalIterator _Last
   );
template<class BidirectionalIterator, class BinaryPredicate>
   bool next_permutation(
      BidirectionalIterator _First, 
      BidirectionalIterator _Last,
      BinaryPredicate _Comp
   );

 


两个重载函数,第二个带谓词参数_Comp,其中只带两个参数的版本,默认谓词函数为"小于".

 

返回值:bool类型

 

分析next_permutation函数执行过程:

 

假设数列 d1,d2,d3,d4……

 

范围由[first,last)标记,调用next_permutation使数列逐次增大,这个递增过程按照字典序。例如,在字母表中,abcd的下一单词排列为abdc,但是,有一关键点,如何确定这个下一排列为字典序中的next,而不是next->next->next……

 

若当前调用排列到达最大字典序,比如dcba,就返回false,同时重新设置该排列为最小字典序。

 

返回为true表示生成下一排列成功。下面着重分析此过程:

 

根据标记从后往前比较相邻两数据,若前者小于(默认为小于)后者,标志前者为X1(位置PX)表示将被替换,再次重后往前搜索第一个不小于X1的数据,标记为X2。交换X1,X2,然后把[PX+1,last)标记范围置逆。完成。

 

要点:为什么这样就可以保证得到的为最小递增。

 

从位置first开始原数列与新数列不同的数据位置是PX,并且新数据为X2。[PX+1,last)总是递减的,[first,PX)没有改变,因为X2>X1,所以不管X2后面怎样排列都比原数列大,反转[PX+1,last)使此子数列(递增)为最小。从而保证的新数列为原数列的字典序排列next。

 

明白了这个原理后,看下面例子:

 

int main(){
 int a[] = {3,1,2};
do{
     cout << a[0] << " " << a[1] << " " << a[2] << endl;
}
 while (next_permutation(a,a+3));
 return 0;
}

 

输出:312/321         因为原数列不是从最小字典排列开始。

 

所以要想得到所有全排列

 

 int a[] = {3,1,2};   change to  int a[] = {1,2,3};

 

 

 

 

 

另外,库中另一函数prev_permutation与next_permutation相反,由原排列得到字典序中上一次最近排列。

 

所以

 

int main(){
 int a[] = {3,2,1};
do{
     cout << a[0] << " " << a[1] << " " << a[2] << endl;
}
 while (prev_permutation(a,a+3));
 return 0;
}

 

才能得到123的所有排列。

4、http://technet.microsoft.com/zh-cn/library/e7d3xas6(v=vs.71)

微软公司专门写给用C++的程序员,告诉他们怎样使用next_permutation

5、http://www.cplusplus.com/reference/algorithm/next_permutation/

C++专业网站,自己点进去看一下,在这里就不全篇转载了。

 

posted @ 2013-05-11 16:48  TonyFang  阅读(1361)  评论(0编辑  收藏  举报