Recursive permutation and combination (no repetition and repetition)

Source: Internet
Author: User
Tags repetition

Given an array of n in size, all outputs are arranged in full order.

1. No repeated elements in the array: place the elements in the array in sequence in the output array from the beginning to the end. If the elements are completely output, recursively change the element position, until all outputs are arranged in full order. Recursion uses a tag array to record whether an element has been placed.

2. There are repeated elements in the array: This is similar to 1. The difference is that the number of times an element is recorded with a tag array, which is reduced by one each time it is placed.

The code for Question 1 is as follows:

 

Void PermRecur (vector <int> & ivec, vector <int> & use, vector <int> & res, int k) {// If the subscript k is equal to size, it indicates that all elements have been placed. if (k = ivec. size () {for (int I = 0; I <k; ++ I) cout <res [I] <''; cout <endl; return ;} for (int I = 0; I <ivec. size (); ++ I) {// if (use [I] = 0) {// after an element is placed, it is marked as 1use [I] = 1; res [k] = ivec [I]; PermRecur (ivec, use, res, k + 1); // After recursion, mark the element as 0use [I] = 0 ;}} void Perm (vector <int> & ivec) {int size = ivec. size (); assert (size> 0); // the flag of all elements is initialized to 0 vector <int> use (size); vector <int> res (size ); permRecur (ivec, use, res, 0);} void main () {int a [] = {1, 2, 3}; vector <int> ivec (, a + 3); Perm (ivec );}
# Include <iostream> # include <vector> # include <assert. h> using namespace std; void PermRecur (vector <int> & num, vector <int> & use, vector <int> & res, int k) {// if the subscript k is equal to size, all elements have been placed if (k = res. size () {for (int I = 0; I <k; ++ I) cout <res [I] <''; cout <endl; return ;} for (int I = 0; I <num. size (); ++ I) {// if the value is greater than 0, the remaining elements are not placed if (use [I]> 0) {// The number of times the element is placed minus 1 -- use [I]; res [k] = num [I]; PermRecur (num, use, res, K + 1); // After recursion, add the number of times of this element to 1 + + use [I] ;}} void Perm (vector <int> & ivec) {assert (! Ivec. empty (); int size = ivec. size (); vector <int> res (size); vector <int> use; vector <int> num; // counts the number of occurrences of each element based on ivec, all de-duplicated elements are stored in num for use. push_back (1); num. push_back (ivec [0]); for (int I = 1; I <size; ++ I) {int j = 0; for (; j <I; ++ j) {if (ivec [I] = ivec [j]) {++ use [j]; break ;}} if (j = I) {use. push_back (1); num. push_back (ivec [I]) ;}} PermRecur (num, use, res, 0);} void main () {int a [] = {1, 2, 2 }; vector <int> ivec (a, a + 3); Perm (ivec );}

 

Returns all subsets of an array of n. Sub-problem: Select m elements from n elements and call them cyclically.

1. No repeated elements in the array: place the elements in the array in sequence in the output array, and output the elements when the number of elements reaches m. Then recursion is performed to exchange the remaining elements and m elements.

2. There are repeated elements in the array: similar to the full arrangement, repeat and calculate the number of elements first, and then perform recursion.

The code for Question 1 is as follows:

 

# Include <iostream> # include <vector> # include <assert. h> using namespace std; void CombRecur (vector <int> & ivec, vector <int> & res, int m, int begin, int k) {// output result if (k = m) {for (int I = 0; I <res. size (); ++ I) cout <res [I] <''; cout <endl; return ;}for (int I = begin; I <ivec. size (); ++ I) {res [k] = ivec [I]; CombRecur (ivec, res, m, I + 1, k + 1 );}} void Comb (vector <int> & ivec) {assert (! Ivec. empty (); for (int I = 1; I <= ivec. size (); ++ I) {vector <int> res (I); CombRecur (ivec, res, I, 0, 0) ;}} void main () {int a [] = {1, 2, 3}; vector <int> ivec (a, a + 3); Comb (ivec );}
# Include <iostream> # include <vector> # include <assert. h> using namespace std; void CombRecur (vector <int> & num, vector <int> & use, vector <int> & res, int m, int begin, int k) {// if k is equal to m, it indicates that if (k = m) {for (int I = 0; I <res. size (); ++ I) cout <res [I] <''; cout <endl; return ;}for (int I = begin; I <num. size (); ++ I) {// if the number of elements exceeds 0, select if (use [I]> 0) {// After selection, the number of elements is reduced by 1 -- use [I]; res [k] = num [I]; // beg is recursive here In is still I, because the I-th element may repeat CombRecur (num, use, res, m, I, k + 1); ++ use [I] ;}} void Comb (vector <int> & ivec) {assert (! Ivec. empty (); int size = ivec. size (); vector <int> num; vector <int> use; // counts the number of occurrences of each element based on ivec, and stores all de-duplicated elements in num. push_back (ivec [0]); use. push_back (1); for (int I = 1; I <size; ++ I) {int j = 0; for (; j <I; ++ j) {if (ivec [I] = ivec [j]) {++ use [j]; break ;}} if (j = I) {use. push_back (1); num. push_back (ivec [I]);} // from 1 ~ N loop call to obtain the size of 1 ~ All subsets of n for (int I = 1; I <= size; ++ I) {vector <int> res (I); CombRecur (num, use, res, I, 0, 0) ;}} void main () {int a [] = {1, 2, 2}; vector <int> ivec (a, a + 3 ); comb (ivec );}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.