下一个全排列_下一个排列

下一个全排列

Problem statement:

问题陈述:

Given a permutation print permutation just greater than this.

给定一个排列,打印排列就比这个更大。

Example:

例:

    Permutation:
1 3 2 5 4
Output:
1 3 4 2 5

Solution:

解:

What is permutation?

什么是排列?

Permutation is the process of arranging the members of a set into a sequence or order, or, if the set is already ordered, rearranging (reordering) its elements. (Ref. wiki: Permutation)

置换是将集合的成员排列为序列或顺序的过程,或者,如果已经对集合进行了排序,则重新排列 (重新排序)其元素。 (参考维基:置换 )

Example:

例:

    Let a set s= {1, 2, 3}
Then it has six permutations which are
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
This all are ordered 
Thus the next permutation of (1, 3, 2) is (2, 1, 3) 
and next of (3, 2, 1) is (1, 2, 3). (Yah! It's cyclic)

Generating Next permutation

产生下一个排列

This problem has a simple but robust algorithm which handles even repeating occurrences. However for this problem we restrict our discussion to single occurrence of numbers in the permutation.

这个问题有一个简单但健壮的算法,可以处理重复的事件。 但是,对于这个问题,我们将讨论限制为排列中数字的单一出现。

Pre-requisite:

先决条件:

Input permutation of length n

长度为n的输入排列

Algorithm:

算法:

1.  Find the largest k such that a[k]<a[k+1] , kЄ [0, n-1] //k=-1 initially
2.  IF
k is not updated (k is -1 still) that means 
current is the largest permutation (descending order).
So the next will be the smallest one (ascending order).
ELSE
3.  Find largest l such that a[k]<a[l]
4.  Swap a[k] and a[l]
5.  Reverse a[k+1] to a[n-1]

Example with Explanation:

解释示例:

K initialized to be -1
Example1:
Permutation:
1 3 2 5 4
k=2     //a[k]=2
l=4     //a[l]>a[k] i.e. 4>2
After swapping a[k] and a[l]
Permutation
1 3 4 5 2
Reversing a[k+1] to a[n-1]
1 3 4 2 5 //output 
Example 2:
Permutation:
5 4 3 2 1   //largest in the possible permutations
k=1         //since no such kfound, k is not updated
Sort the current permutation in ascending order
Next Permutation
1 2 3 4 5 //output

C++ implementation

C ++实现

#include <bits/stdc++.h>
using namespace std;
//to print a vector
void print(vector<int> a,int n){ 
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
}
void nextPermutation(vector<int> a,int n){
int k=-1,l,temp;
//finding largest k s.t. a[k]<a[k+1]
for(int i=0;i<n-1;i++){
if(a[i]<a[i+1])
k=i;
}
//if k not updated sort  and print
if(k==-1){
sort(a.begin(),a.end());
print(a,n);
return;
}
//find the largest l s.t. a[k]<a[l]
for(int i=k+1;i<n;i++){
if(a[i]>a[k])
l=i;
}
//swap a[k] and a[l]
temp=a[k];
a[k]=a[l];
a[l]=temp;
//print upto a[k]
for(int i=0;i<=k;i++)
cout<<a[i]<<" ";
//reverse printing for a[k+1] to a[n-1] 
for(int i=n-1;i>k;i--)
cout<<a[i]<<" ";
cout<<endl;
return;
}
int main(){
int n,item;
cout<<"enter length of permutation\n";
scanf("%d",&n);
cout<<"enter the permutation leaving spaces betweeen two number\n";
vector<int> a;	
for(int j=0;j<n;j++){
scanf("%d",&item);
a.push_back(item);
}
cout<<"Current permutation is:\n";
print(a,n);
cout<<"next permutation is:\n";
nextPermutation(a,n);
return 0;
}

Output

输出量

First run:
enter length of permutation
5
enter the permutation leaving spaces betweeen two number
1 3 2 5 4 
Current permutation is:
1 3 2 5 4
next permutation is:
1 3 4 2 5
Second run:
enter length of permutation
5
enter the permutation leaving spaces betweeen two number
5 4 3 2 1
Current permutation is:
5 4 3 2 1
next permutation is:
1 2 3 4 5

翻译自: https://www.includehelp.com/icp/next-permutation.aspx

下一个全排列

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/543760.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[转载]PhotoShop性能优化

现在随着Photoshop版本越来越高功能也越来越强大&#xff0c;而往往强大的功能需要电脑有好的配置运行&#xff0c;比如HDR、图像合成或者3D和视频等类似的功能&#xff0c;还有处理比较大尺寸的图像时&#xff0c;如果电脑配置不够强往往非常卡&#xff0c;这时我们就要好好设…

in-nan(ind)_NaN16 Constant in Julia

in-nan(ind)Julia| NaN16常数 (Julia | NaN16 Constant) NaN16 is a constant of the Float16 type in Julia programming language, it represents "not-a-number" value. NaN16是Julia编程语言中Float16类型的常量&#xff0c;它表示“非数字”值。 Syntax: 句法&…

iOS 架构模式

MVVM指南&#xff08;课程学习&#xff09; iOS 架构模式 iOS 之 依赖注入 Square对iOS App架构的新尝试---Ziggurat 基于彻底解耦合的实验性iOS架构转载于:https://www.cnblogs.com/SimonGao/p/5112299.html

分披萨问题_比萨疯狂问题

分披萨问题Problem statement: 问题陈述&#xff1a; There is a shop which sells pizza of three different sizes- Small, Medium, and Large Pizza. IncludeHelp is crazy for Pizzas. A small Pizza has an area of S unit2, a medium Pizza has an area of M unit2 and …

oracle 10g学习之分组函数

一、 &#xff08;1&#xff09;分组查询语句的顺序 select ... from ... where ... group by ... having ... order by ... 注意&#xff1a; where-->group by分组-->执行组函数-->having筛选->order by 如果select/having语句后面出现了组函数 那么se…

0.1uf与47uf并联_UF是什么形式?

0.1uf与47uf并联UF&#xff1a;超滤 (UF: Ultrafiltration) UF is an abbreviation of Ultrafiltration. It is a kind of membrane filtration which is used in UF water purifiers. Through a hollow fiber threaded semi-permeable membrane, the water is made to proceed…

机器学习相关——协同过滤

在现今的推荐技术和算法中&#xff0c;最被大家广泛认可和采用的就是基于协同过滤的推荐方法。本文将带你深入了解协同过滤的秘密。下面直接进入正题 1 什么是协同过滤 协同过滤是利用集体智慧的一个典型方法。要理解什么是协同过滤 (Collaborative Filtering, 简称 CF)&#x…

二维的完整形式是什么?

2D&#xff1a;二维 (2D: Two Dimensional) 2D is an abbreviation of "Two-Dimensional". 2D是“二维”的缩写 。 It is the dimension of any virtual object that has no manifestation of profundity. For example, if a graphic or picture representation of …

InfoQ中文站2015年度优秀社区编辑评选揭晓

\又到了年终岁末&#xff0c;在过去的一年里&#xff0c;InfoQ网站的月独立UV接近130万&#xff0c;月PV突破200万&#xff0c;每周独立访问用户接近30万&#xff0c;网站访问量过万的文章超过60篇。每月活跃的数十位社区编辑为InfoQ的内容生产贡献着力量。正是这点点汇聚的星光…

wfm扩展_WFM的完整形式是什么?

wfm扩展WFM&#xff1a;为我工作 (WFM: Works For Me) WFM is an abbreviation of "Works For Me". WFM是“ Works For Me”的缩写 。 It is an expression, which is commonly used in messaging or chatting on social media networking sites like Facebook, Yah…

【设计模式】—— 访问者模式Visitor

对于某个对象或者一组对象&#xff0c;不同的访问者&#xff0c;产生的结果不同&#xff0c;执行操作也不同。此时&#xff0c;就是访问者模式的典型应用了。 应用场景 1 不同的子类&#xff0c;依赖于不同的其他对象 2 需要对一组对象&#xff0c;进行许多不相关的操作&#x…

ruby宝石区块链最新消息_Ruby宝石| Ruby工具

ruby宝石区块链最新消息Ruby宝石 (Ruby Gems) Every language has its package manager which helps it by providing libraries and a standard format to distribute Ruby program. It is a type of tool which is developed to easily facilitate the installation of Gems.…

CMake入门(二)

CMake入门&#xff08;二&#xff09; 最后更新日期&#xff1a;2014-04-25 by kagula 阅读前提&#xff1a;《CMake入门&#xff08;一&#xff09;》、Linux的基本操作 环境: Windows 8.1 64bit英文版。Visual Studio 203 Update1英文版。CMake 2.8.12.2、 Cent OS 6.5。内容…

Scala中的评论

Scala评论 (Scala comments) Comments are things that are readable by the programmer. They are added to the code to add an explanation about the source code. Commenting on a program to make it easier to understand by the programmer. 注释是程序员可以读取的东西…

最小生成树prim (c++ 已大改)

2019独角兽企业重金招聘Python工程师标准>>> #include <iostream> #include <vector> #include <set> #include <map> #include <initializer_list> #include <memory> template<typename T> class Graph{private:std::m…

assoc_Ruby assoc()函数

assocRuby中的assoc()函数 (assoc() function in Ruby) We have studied functions to process single dimensional array so far but if we talk about assoc() function, it does not work for single dimensional arrays. assoc() function only works on Array of Arrays o…

前端接入HTTP协议浅析

【摘要】&#xff1a;本文整理并简要分析了HTTP协议的交互过程和内容格式&#xff0c;包括HTTP请求、HTTP应答的头域和实体内容&#xff0c;HTTP 1.0与HTTP 1.1的差异&#xff0c;并举例说明了Chunked编码的工作过程原理。1、HTTP协议简介浏览器和Web服务器之间一问一答的交互过…

互联网传真 传真指令_传真的完整形式是什么?

互联网传真 传真指令传真&#xff1a;传真 (FAX: Facsimile) FAX is an abbreviation of "Facsimile". 传真是“传真”的缩写 。 It is commonly written and spoken as FAX. It is a telephonic transmission of a scanned copy of text and images printed on a p…

C#使用七牛云存储上传下载文件、自定义回调

项目需要将音视频文件上传服务器&#xff0c;考虑并发要求高&#xff0c;通过七牛来实现。 做了一个简易的压力测试&#xff0c;同时上传多个文件&#xff0c;七牛自己应该有队列处理并发请求&#xff0c;我无论同时提交多少个文件&#xff0c;七牛是批量一个个排队处理了。 一…

netfilter que_QUE的完整形式是什么?

netfilter que问题&#xff1a;问题 (QUE: Questions) QUE is an abbreviation of "Questions". QUE是“ Questions”的缩写 。 It is an expression, which is commonly used in the Gmail platform. It is written in the body or the subject of the email to te…