下一个全排列_下一个排列
下一個全排列
Problem statement:
問題陳述:
Given a permutation print permutation just greater than this.
給定一個排列,打印排列就比這個更大。
Example:
例:
Permutation:1 3 2 5 4Output:1 3 4 2 5Solution:
解:
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. IFk 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 -1Example1: 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 .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}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 printif(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 5Second 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
下一個全排列
總結
以上是生活随笔為你收集整理的下一个全排列_下一个排列的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql卸载后重装失败_小筑教育BIM
- 下一篇: 键盘特殊_特殊键盘