精华内容
下载资源
问答
  • Permutation

    2017-06-10 08:30:46
    bobo has a permutation p1,p2,…,pn of 1,2,…,n. Knowing m extra constraints of form pai, bobo wanna count the number of different permutations modulo (109+7). It is guaranteed that there is at least...
  • permutation

    2017-05-25 05:41:15
    Permutation plays a very important role in Combinatorics. For example ,1 2 3 4 5 and 1 3 5 4 2 are both 5-permutations. As everyone's known, the number of n-permutations is n!. According to their ...
  • Permutation matrices

    2020-12-01 14:37:56
    <div><p>A <a href="https://en.wikipedia.org/wiki/Permutation_matrix">permutation matrix</a> is a very special kind of matrix with many applications in mathematics. Within rulinalg, its use is most not...
  • next_permutation、prev_permutation以及is_permutation使用next_permutationprev_permutationis_permutation next_permutation、prev_permutation以及is_permutation均为全排列相关的函数,调用时需要加入头文件#...

    next_permutation、prev_permutation以及is_permutation使用


    next_permutation、prev_permutation以及is_permutation均为全排列相关的函数,调用时需要加入头文件#include <algorithm>

    next_permutation

    next_permutation的函数原型:

    //default
    template <class BidirectionalIterator>
    	bool next_permutation (BidirectionalIterator first, BidirectionalIterator last);
    
    //custom 
    template <class BidirectionalIterator, class Compare>
    	bool next_permutation (BidirectionalIterator first, BidirectionalIterator last, Compare comp);
    

    next就是下一个的意思,其中文直译过来就是下一个全排列,因此在使用next_permutation函数的时候切记要对全排列的对象进行升序排列,不然结果只会出现比当前排列大的排列,依据ASCII计算。
    next_permutation(a, b)表示只对[a, b)区间的数据进行全排列,其余位置不变化

    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    #define SORT
    
    int main()
    {
    	vector<int> V = {3, 2, 1 };
    #ifdef SORT
    	sort(V.begin(), V.end());//sort
    #endif
    	do
    	{
    		for (int i = 0; i < V.size(); i++)
    		{
    			cout << V[i] << " ";
    		}
    		cout << endl;
    	} while (next_permutation(V.begin(), V.end()));
    	return 0;
    }
    //result
    /*
    #define SORT
    1 2 3
    1 3 2
    2 1 3
    2 3 1
    3 1 2
    3 2 1
    */
    
    /*
    没有进行sort排序
    3 2 1
    */
    

    prev_permutation

    prev_permutation和next_permutation功能一样,唯一的区别是next_permutation下一个全排列,而prev_permutation是上一个全排列,所以运用prev_permutation的时候必须降序排列,不然只会出现比结果小的排列

    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    
    //#define SORT
    #define RSORT
    
    int main()
    {
    	vector<int> V = {3, 4, 2, 1};
    #ifdef SORT
    	sort(V.begin(), V.end());
    #else
    	sort(V.rbegin(), V.rend());
    #endif
    	do
    	{
    		for (int i = 0; i < V.size(); i++)
    		{
    			cout << V[i] << " ";
    		}
    		cout << endl;
    	} while (prev_permutation(V.begin() + 1, V.end() - 1));
    	return 0;
    }
    //result
    /*
    #define RSORT
    4 3 2 1
    4 2 3 1
    */
    
    /*
    #define RSORT
    1 2 3 4
    */
    

    is_permutation

    template <class ForwardIterator1, class ForwardIterator2>
       bool is_permutation (ForwardIterator1 first1, ForwardIterator1 last1,
                            ForwardIterator2 first2);
                            
    template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
       bool is_permutation (ForwardIterator1 first1, ForwardIterator1 last1,
                            ForwardIterator2 first2, BinaryPredicate pred);
    

    is_permutation用来判断两个全排列是否相等,哪怕顺序不同,只要元素一样就返回true

    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    int main()
    {
    	vector<int> V1 = { 3, 4, 2, 1 };
    	vector<int> V2 = { 1, 3, 2, 4 };
    	vector<int> V3 = { 1, 3, 2, 4, 1};
    	vector<float> V4 = { 1.0, 3.0, 2.0, 4.0 };
    	vector<float> V5 = { 1.0000001, 3.0, 2.0, 4.0 };
    	if (is_permutation(V1.begin(), V1.end(), V2.begin()))
    	{
    		cout << "V1 and V2 is equal" << endl;
    	}
    	if (is_permutation(V1.begin(), V1.end(), V3.begin()))
    	{
    		cout << "V1 and V3 is equal" << endl;
    	}
    	if (is_permutation(V1.begin(), V1.end(), V4.begin()))
    	{
    		cout << "V1 and V4 is equal" << endl;
    	}
    	if (is_permutation(V1.begin(), V1.end(), V5.begin()))
    	{
    		cout << "V1 and V5 is equal" << endl;
    	}
    	return 0;
    }
    
    //result
    /*
    V1 and V2 is equal
    V1 and V3 is equal
    V1 and V4 is equal
    */
    

    is_permutation单纯的比较值的大小,但是需要注意的是序列1重复的必须在序列2中展现,也就是count sequence1[i] <= count sequence2[i]。

    展开全文
  • Permutation Recovery

    2019-12-17 21:57:05
    For each integer i (1 ), she asks the students to write down the number of integers greater than i that appears before i in the given permutation. This number is denoted ai. For example, if n = 8 and...
  • Perfect Permutation

    2017-08-13 15:32:48
    A permutation of 1..n {An} is called a Perfect Permutation if the sequence {|Ai − i|} is a permutation of 0..(n − 1). For example, {3, 2, 4, 1} is a perfect permutation for {2, 0, 1, 3} is a ...
  • LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Permutation 给出一个序列,求其下一个排列 STL中有std::next_permutation这个方法可以直接拿来用 也可以写一...

    LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++>

    LeetCode 31 Next Permutation

    给出一个序列,求其下一个排列
    STL中有std::next_permutation这个方法可以直接拿来用
    也可以写一个实现程序:

    1. 从右往左遍历序列,找到第一个nums[i-1]<num[i]的位置,记p = i-1
    2. 如果第一步没有找到,说明整个序列满足单调递减,也就是最大的排列,那么倒置序列,return即可。
    3. 再次从右往左遍历序列,找到第一个nums[i]>nums[p]的位置,std::swap(nums[i],nums[p])
    4. 此时从p位置开始到序列最右端一定满足单调递减,倒置这一部分,使其字典序最小,所得序列即为下一个排列。
    class Solution {
    public:
        void nextPermutation(std::vector<int>& nums) {
            int p = -1;
            for(int i = nums.size()-1; i>=0; i--) 
                if(i-1>=0 && nums[i]>nums[i-1]){
                    p = i-1;
                    break;
                }
            if(p==-1){
                std::reverse(nums.begin(),nums.end());
                return;
            }
            for(int i = nums.size()-1; i>=0; i--) 
                if(nums[i]>nums[p]){
                    std::swap(nums[i],nums[p]);
                    break;
                }
            std::reverse(nums.begin()+p+1,nums.end());
        }
    };

    LeetCode 60 Permutation Sequence

    求长度为n的序列(1~n)全排列的第k大排列
    如果不停调用std::next_permutation肯定会超时。
    利用康托展开,所有排列按照字典序排序后,按顺序编号,称为康托编码
    那么根据序列长度和编号求解序列,实际就是解码过程。
    编码解码详见 https://blog.csdn.net/synapse7/article/details/16901489

    class Solution {
    public:
        std::string getPermutation(int n, int k) {
            std::string ans;
            std::string seq0(n,'0');
            for(int i = 0; i<n; i++){ // seq0: 1~n minimal permutation
                seq0[i] += i+1;
            }
            int base = 1;
            for(int i = 1; i<n; i++) base *= i;
            k--;
            for(int i = 0; i<n-1; k %= base, base/=n-i-1, i++){
                auto pos = seq0.begin()+k/base;
                ans.push_back(*pos);
                seq0.erase(pos);
            }
            ans.push_back(seq0[0]);
            return ans;
        }
    };

    转载于:https://www.cnblogs.com/NeilThang/p/10343101.html

    展开全文
  • Next Permutation

    2020-04-16 22:42:37
    Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible o...

    Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.

    If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).

    The replacement must be in-place and use only constant extra memory.

    Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.

    1,2,31,3,2
    3,2,11,2,3
    1,1,51,5,1

    public class Solution {
        public void NextPermutation(int[] nums) {
            if (nums == null || nums.Length <= 1) return;
            else {
                int i = 0;
                while (i < nums.Length - 1 && nums[i] >= nums[i + 1]) {
                    i++;
                }
                if (i == nums.Length - 1) {
                    Array.Sort(nums);
                }
                else {                
                    List<int> a = new List<int>();
                    int j = nums.Length - 1;
                    int max = nums[j--];
                    a.Add(max);
                    while (j >= 0) {
                        if (max > nums[j]) {
                            int temp = max;
                            foreach(int t in a) {
                                if (t > nums[j] && t < temp) {
                                    temp = t;
                                }
                            }
                            a.Remove(temp);
                            max = temp;
                            a.Add(nums[j]);
                            nums[j] = max;
                            int[] b = a.ToArray();
                            Array.Sort(b);
                            for (int k = j + 1, m = 0; k < nums.Length; ++k, ++m) {
                                nums[k] = b[m];
                            }
                            break;
                        }
                        else {
                            a.Add(nums[j]);
                            max = nums[j];
                        }
                        --j;
                    }
                }
            }
        }
    }

    Runtime: 228 ms, faster than 97.45% of C# online submissions for Next Permutation.

    public class Solution {
        public void nextPermutation(int[] nums) {
            int i = nums.length - 2;
            while (i >= 0 && nums[i + 1] <= nums[i]) {
                i--;
            }
            if (i >= 0) {
                int j = nums.length - 1;
                while (j >= 0 && nums[j] <= nums[i]) {
                    j--;
                }
                swap(nums, i, j);
            }
            reverse(nums, i + 1);
        }
    
        private void reverse(int[] nums, int start) {
            int i = start, j = nums.length - 1;
            while (i < j) {
                swap(nums, i, j);
                i++;
                j--;
            }
        }
    
        private void swap(int[] nums, int i, int j) {
            int temp = nums[i];
            nums[i] = nums[j];
            nums[j] = temp;
        }
    }
    public class Solution {
        public void NextPermutation(int[] nums) {
            var i = nums.Length - 2;
            while (i >= 0 && nums[i] >= nums[i + 1])
            {
                i--;
            }
            
            if (i >= 0)
            {
                var j = 0;
                for (j = nums.Length - 1; j > i; j--)
                {
                    if (nums[j] > nums[i]) break;
                }
    
                var temp = nums[i];
                nums[i] = nums[j];
                nums[j] = temp;
            }
            
            var l = i + 1;
            var r = nums.Length - 1;
            while (l < r)
            {
                var temp = nums[l];
                nums[l] = nums[r];
                nums[r] = temp;
                    
                l++;
                r--;
            }
        }
    }

    展开全文
  • Permutation function

    2021-01-28 10:00:11
    1:next_permutation()函数,作用是输出所有比当前排列 排列大的排列(顺序为由小到大排),求原排列的下一个排列。 next_permutation(a,a+n)函数是对数组a中前n个元素进行全排列 #pragma GCC diagnostic error"-std...

    全排列函数:包含两个。
    1:next_permutation()函数,作用是输出所有比当前排列 排列大的排列(顺序为由小到大排),求原排列的下一个排列。
    next_permutation(a,a+n)函数是对数组a中前n个元素进行全排列

    方法1#pragma GCC diagnostic error"-std=c++11"
    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    int main(){
    	string str1;
    	cin>>str1;
    	while(next_permutation(str1.begin(),str1.end()))/将全排列的情况全排出来
    	cout<<str1<<endl;
       	return 0;
    }
    //注意:
    //1.while(m--){
    	 // next_permutation(a,a+n);/进行m次全排列
    	//}
    //2.#include<bits/stdc++.h>
     //using namespace std;
    // int main()
    //{
        //int a[3]={2,1,3};//预先存第三个排列 
        //prev_permutation(a,a+3);	//只是生成下一个排列 ,并不是全部.
    	//cout<<a[0]<<" "<<a[1]<<" "<<a[2]<<endl;//输出 1 3 2 
        //return 0;
    //}
    
    方法2#include<bits/stdc++.h>
    using namespace std;
    int main(){
    	char str[100];
    	cin>>str;
    	int len=strlen(str);
    	while(next_permutation(str,str+len)){
    		 cout<<str<<endl;
    	}
    	return 0;
    }
    
    

    当输入123时:
    在这里插入图片描述
    2.perv_permutation()函数,作用是输出比当前排列小的排列 (顺序:从大到小),求原排列的上一个排列。

    方法1#pragma GCC diagnostic error"-std=c++11"
    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int maxn=1e5+9;
    int main(){
    	string str;
    	cin>>str;
    	while(prev_permutation(str.begin(),str.end()))
    	  cout<<str<<endl;
    	return 0;
    }
    方法2#pragma GCC diagnostic error"-std=c++11"
    #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
    	char str[100];
    	cin>>str;
    	int len=strlen(str);
    	while(prev_permutation(str,str+len))
    	{
    		 cout<<str<<endl;
    	}
    	return 0;
    }
    
    

    当输入231时:
    在这里插入图片描述
    3.需要注意:
    如果要得到几个数的全排列,需要将数组按升序排列,因为对于next_permutation()函数,它只对比自己大的排列起作用,所以可以对输入的序列进行由小到大的排序,因为最后的总个数不包含第一个最小的那个序列,所以要在最后的总数上加1,才是这几个数进行全排列的结果。

    方法1#pragma GCC diagnostic error"-std=c++11"
    #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
    	char str[100];
    	int sum=0;
    	cin>>str;
    	 int len=strlen(str);
    	 sort(str,str+len); 
    	while(next_permutation(str,str+len))
    	{
    		 cout<<str<<endl;
    		 sum++;
    	}
    	cout<<sum<<endl;
    	return 0;
    }
    
    
    

    当输入231时计算机会自动将其从小到大排好序即无论输入多少都是从123开始的,即最小的数开始。
    在这里插入图片描述
    4.输出全排列:
    尽量用do while循环,因为第一个排列也要输出:

    方法1#include<bits/stdc++.h>
    using namespace std;
    int main()
    {
        int a[3]={1,2,3}; 
        do
        {
            cout<<a[0]<<" "<<a[1]<<" "<<a[2]<<endl;
        }while(next_permutation(a,a+3));
        return 0;
    }
    

    在这里插入图片描述
    5.补充去重写法:

    #include<iostream>
    #include<vector>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    int main()
    {
    	int num[100];
    	string s;
    	cin>>s;
    	int len=s.length();
    	for(int i=0;i<len;i++)
    	{
    		num[i]=s[i]-'0';
    	}
    	sort(num,num+len);
    	do{
    		for( int i=0;i<len;i++)
    		{
    			cout<<num[i];
    		}
    		cout<<endl;
    	}while(next_permutation(num,num+len));
    	
    	return 0;
    } 入代码片
    

    总结:
    函数模板:next_permutation(arr, arr+size);
    函数模板:prev_permutation(arr, arr+size);
    解释:arr为数组,size为数组长度。next_permutation(arr, arr+size);当有下一个较大值返回1,否则返回0,prev_permutation(arr, arr+size);当有上一个较小值返回1,否则返回0。例如:3 2 1,只有上一个较小值,没有下一个较大值。1 2 3只有下一个较大值,没有上一个较小值。2 1 3 的上一个较小值为 1 3 2,下一个较大值为 2 3 1.

    展开全文
  • Next Permutation Given a list of integers, which denote a permutation. Find the next permutation in ascending order. Notice The list may contains duplicate integers. Example F...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 14,425
精华内容 5,770
关键字:

permutation