本文senlie原版的,转载请保留此地址:http://blog.csdn.net/zhengsenlie

pre_permutation

----------------------------------------------------------------

描写叙述: 取得 [first, last) 所标示之序列的前一个排列组合。

假设没有,返回 false,有,返回true

思路:

从后往前

1.找两个相邻元素,令左端的元素为*i,右端的元素为*ii,且满足 *i > *ii

2.找出第一个小于 *i 的元素,令其为 *j。将*i,*j元素对调

3.将ii右端的全部元素颠倒

template <class BidirectionalIterator>
bool prev_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 (*ii < *i) { //满足 *ii < *i --> next_permutation 时的条件是 *i < *ii
BidirectionalIterator j = last;
while (!(*--j < *i)); // 找到满足 *j < *i 的 *j --> next_permutation 时的条件是 *i < *j
iter_swap(i, j);
reverse(ii, last);
return true;
}
if (i == first) {
reverse(first, last);
return false;
}
}
}

演示样例:

int main()
{
int A[] = {2, 3, 4, 5, 6, 1};
const int N = sizeof(A) / sizeof(int); cout << "Initially: ";
copy(A, A+N, ostream_iterator<int>(cout, " "));
cout << endl; prev_permutation(A, A+N);
cout << "After prev_permutation: ";
copy(A, A+N, ostream_iterator<int>(cout, " "));
cout << endl; next_permutation(A, A+N);
cout << "After next_permutation: ";
copy(A, A+N, ostream_iterator<int>(cout, " "));
cout << endl;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

STL 源代码分析 算法 stl_algo.h -- pre_permutation的更多相关文章

  1. STL 源代码分析 算法 stl_algo.h -- includes

    本文senlie原,转载请保留此地址:http://blog.csdn.net/zhengsenlie includes(应用于有序区间) ------------------------------ ...

  2. STL 源代码分析 算法 stl_algo.h -- merge

    本文senlie原版的,转载请保留此地址:http://blog.csdn.net/zhengsenlie merge (应用于有序区间) ------------------------------ ...

  3. STL 源代码分析 算法 stl_algo.h -- binary_search

    本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie binary_search -------------------------------- ...

  4. STL 源代码分析 算法 stl_heap.h

    本文senlie原版的.转载请保留此地址:http://blog.csdn.net/zhengsenlie heap ----------------------------------------- ...

  5. STL 源代码剖析 算法 stl_algo.h -- search

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie search --------------------------------------- ...

  6. STL 源代码剖析 算法 stl_algo.h -- partial_sort / partial_sort_copy

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie partial_sort / partial_sort_copy ------------- ...

  7. STL 源代码剖析 算法 stl_algo.h -- lower_bound

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie lower_bound(应用于有序区间) ------------------------- ...

  8. STL 源代码剖析 算法 stl_algo.h -- random_shuffle

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie random_shuffle ------------------------------- ...

  9. STL 源代码剖析 算法 stl_algo.h -- merge sort

    本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie merge sort ----------------------------------- ...

随机推荐

  1. java表达式陷阱

    以下是一些常见的面试java问题的表达,我将造成直接的形式附加目光. 问题1: int a = 10; int b = a + (a = 5) + a + (a = 10); System.out.p ...

  2. Javadoc的Html文件传输chm

     Javadoc的Html文件转chm 工具下载地址:http://msdn.microsoft.com/en-us/library/ms669985.aspx 两篇相关文章: MyEclipse ...

  3. Entity Framework mvc Code First data migration

    1. Code First 可以先在代码里写好数据模型,自动生成DB.下一次启动的时候会根据__MigrationHistory判断 数据库是否和模型一致. 详情参考:http://blogs.msd ...

  4. Swing中弹出对话框的几种方式(转)

    http://www.cnblogs.com/mailingfeng/archive/2011/12/28/2304289.html 在swing中,基于业务的考量,会有对话框来限制用户的行为及对用户 ...

  5. Greenplum+Hadoop学习笔记-14-定义数据库对象之创建与管理模式

    6.3.创建与管理模式 概述:DB内组织对象的一种逻辑结构.一个DB内能够有多个模式.在未指定模式时默认放置在public中.能够通过"\dn"方式查看数据库中现有模式: test ...

  6. Leetcode:minimum_depth_of_binary_tree解决问题的方法

    一.     称号 并寻求最深的二元相似.给定的二进制树.求其最小深度. 最小深度是沿从根节点,到叶节点最短的路径. 二.     分析 当我看到这个题目时.我直接将最深二叉树的代码略微改了下,把ma ...

  7. 相关Jquery Validator采用

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs& ...

  8. 先学习Oracle 11g的Automatic Diagnostic Repository新功能

    Oracle 11g之前.当数据库出现故障,通常情况下,第一次需要看alert刊物.什么,看看哪些记录错误,您可以给我们的提示.alert文件名 是alert_<ORACLE_SID>.l ...

  9. 于win7使用虚拟磁盘隐藏文件

    于win7使用虚拟磁盘隐藏文件,我只是win7在验证.其他型号未知. 一.创建虚拟磁盘 1.右键点击"计算机"-----"管理" ------"磁盘管 ...

  10. 这两个成员函数inline重新virtual种类

    inlineType表示在编译时扩展功能,随着在函数调用的函数体而出替换函数调用:和vitual它是c++多态的必要条件,但为了表现出多态性,您将需要等到执行,要知道什么是真正的函数调用.从表面上看这 ...