zoukankan      html  css  js  c++  java
  • next_permutation & prev_permutation

    1、碰到next_permutation(permutation:序列的意思)

    今天在TC上碰到一道简单题(SRM531 - Division Two - Level One),是求给定数组不按升序排列的最小字典序列(Sequence of numbers A is lexicographically smaller than B if A contains a smaller number on the first position on which they differ)。

    解法很简单,就是将数组排序(升序),然后从尾到头找到第一个可以交换的位置(因为可能有重复的数字)。

    最后看别人的解法,排序后,用了STL中的一个函数next_permutaion,直接求到第一个不按升序排列的序列。

    2、next_permutation实现原理

    在《STL源码解析》中找到了这个函数,在此也简单叙述一下原理:

    在STL中,除了next_permutation外,还有一个函数prev_permutation,两者都是用来计算排列组合的函数。前者是求出下一个排列组合,而后者是求出上一个排列组合。所谓“下一个”和“上一个”,书中举了一个简单的例子:对序列 {a, b, c},每一个元素都比后面的小,按照字典序列,固定a之后,a比bc都小,c比b大,它的下一个序列即为{a, c, b},而{a, c, b}的上一个序列即为{a, b, c},同理可以推出所有的六个序列为:{a, b, c}、{a, c, b}、{b, a, c}、{b, c, a}、{c, a, b}、{c, b, a},其中{a, b, c}没有上一个元素,{c, b, a}没有下一个元素。
    
    
    
    next_permutation的函数原型如下:
    

    Cpp代码 收藏代码

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

    对于第二个重载函数的第三个参数,默认比较顺序为小于。如果找到下一个序列,则返回真,否则返回假。

    函数实现原理如下:

    在当前序列中,从尾端往前寻找两个相邻元素,前一个记为*i,后一个记为*ii,并且满足*i < *ii。然后再从尾端寻找另一个元素*j,如果满足*i < *j,即将第i个元素与第j个元素对调,并将第ii个元素之后(包括ii)的所有元素颠倒排序,即求出下一个序列了。
    

    代码实现如下:

    Cpp代码 收藏代码

    template<class BidirectionalIterator>  
    bool next_permutation(  
          BidirectionalIterator first,   
          BidirectionalIterator last  
    )  
    {  
        if(first == last)  
            return false; //空序列  
    
        BidirectionalIterator i = first;  
        ++i;  
        if(i == last)  
            return false;  //一个元素,没有下一个序列了  
    
        i = last;  
        --i;  
    
        for(;;) {  
            BidirectionalIterator ii = i;  
            --i;  
            if(*i < *ii) {  
                BidirectionalIterator j = lase;  
                while(!(*i < *--j));  
    
                iter_swap(i, j);  
                reverse(ii, last);  
                return true;  
            }  
    
            if(i == first) {  
                reverse(first, last);  //全逆向,即为最小字典序列,如cba变为abc  
                return false;  
            }  
        }  
    
    }  
    

    prev_permutation实现类似,就是反向查找

    3、使用next_permutation

    思考问题,序列{a, d, c, e, b}的下一个序列是什么呢?请利用前面的分析推出答案,并用代码验证。

    我这里分别用数组和vector来表示序列,用next_permutation得到下一个序列(编译环境:Dev-C++):

    Cpp代码 收藏代码

    #include <cstdlib>  
    #include <iostream>  
    #include <algorithm>  
    #include <vector>  
    
    using namespace std;  
    
    void TestArray()   
    {  
        char chs[] = {'a', 'd', 'c', 'e', 'b'};  
        int count = sizeof(chs)/sizeof(char);  
    
        next_permutation(chs+0, chs + count);  
    
        printf("TestArray:
    ");  
        for(int i = 0; i < count; i++) {  
                printf("%c	", chs[i]);  
        }  
    
        printf("
    ");  
    }  
    
    void TestVector()  
    {  
         char chs[] = {'a', 'd', 'c', 'e', 'b'};  
         int count = sizeof(chs)/sizeof(char);  
         vector<char> vChs(chs, chs + count);  
    
         next_permutation(vChs.begin(), vChs.end());  
    
         printf("TestVector:
    ");  
         vector<char>::iterator itr;  
         for(itr = vChs.begin(); itr != vChs.end(); itr++) {  
                 printf("%c	", *itr);  
         }  
         printf("
    ");  
    }  
    
    int main(int argc, char *argv[])  
    {  
        TestArray();  
        printf("
    ");  
        TestVector();  
    
        system("PAUSE");  
        return EXIT_SUCCESS;  
    }  
    

    运行结果:

    4、小结

    用next_permutation和prev_permutation求排列组合很方便,但是要记得包含头文件#include <algorithm>。
    
        虽然最后一个排列没有下一个排列,用next_permutation会返回false,但是使用了这个方法后,序列会变成字典序列的第一个,如cba变成abc。prev_permutation同理。
    

    原文链接:http://leonard1853.iteye.com/blog/1450085

  • 相关阅读:
    UVa11324 最大团
    UVa11624 BFS
    UVa10047 BFS
    UVa11054 欧拉回路
    CF413B 水题
    UVa LA 4287 强连通 (类似 hdu 3836)
    hdu1540 线段树(区间合并)
    最少的扇形区域 ——贪心
    Little Busters! — 并查集
    筛1-n中每个数的因子(nlogn)
  • 原文地址:https://www.cnblogs.com/vocaloid01/p/9514268.html
Copyright © 2011-2022 走看看