Algorithm exercise: child set of permutation and combination

Source: Internet
Author: User

Algorithm exercise: child set of permutation and combination
Problem description

Input a sequence containing different numbers and output all its child sets (including empty sets ). Requirements: 1) Ordered Arrangement of elements in the Set; 2) the output result does not include repeated sets.

 

Example

Input sequence {3, 1, 2}

Output: {}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}

 

Problem Analysis

You can use the first method of permutation and combination to solve the problem: return the problem by installment. During initialization, The result set contains an empty set. When a series is scanned, the original set is retained, and the current element is inserted into the existing collection to form a new set. For details, see the GetSubSetsAmortized function in the code below.

You can also use the second method: use recursion in the for loop. Note: 1) retain any intermediate results, instead of traversing the complete series to save the results. 2) After using the sum of the numbers of the series, you need to roll back, that is, the backtracking method, to restore to the status before use. For details, see the GetSubSetsRecursive function in the code below.

 

Scaling Problems

If the sequence contains repeated elements and the output results do not contain repeated sets, what should I do? To avoid repeated output combinations, add the first identical element to the combination. For details, see the GetSubSetsRecursiveEx function in the code below.

 

Code Section

 

# Include
 
  
# Include
  
   
Using namespace std; typedef vector
   
    
IntArray; typedef vector
    
     
> ResultSet; ResultSet gResultSet; // result set // void GetSubSetsAmortized (const IntArray & mSrcArray) using the installment return method {// Add the empty set gResultSet. push_back (IntArray (); for (int I = 0; I <mSrcArray. size (); ++ I) {int nCount = gResultSet. size (); for (int j = 0; j <nCount; ++ j) {IntArray mSubSet = gResultSet [j]; mSubSet. push_back (mSrcArray [I]); gResultSet. push_back (mSubSet) ;}}// call void GetSubSetsRecursive (const IntArray & mSrcArray, IntArray & mDstArrayTemp, int iStart) {if (iStart> mSrcArray. size () return; gResultSet. push_back (mDstArrayTemp); // retain any intermediate results for (int I = iStart; I <mSrcArray. size (); ++ I) {// Add the mDstArrayTemp element. push_back (mSrcArray [I]); GetSubSetsRecursive (mSrcArray, mDstArrayTemp, I + 1); // remove the element mDstArrayTemp. pop_back () ;}}// call void GetSubSetsRecursiveEx recursively in the for loop (const IntArray & mSrcArray, IntArray & mDstArrayTemp, int iStart) {if (iStart> mSrcArray. size () return; gResultSet. push_back (mDstArrayTemp); // retain any intermediate results for (int I = iStart; I <mSrcArray. size (); ++ I) {// avoid duplicate result sets. Add if (I! = IStart & mSrcArray [I] = mSrcArray [I-1]) continue; // Add the element mDstArrayTemp. push_back (mSrcArray [I]); GetSubSetsRecursiveEx (mSrcArray, mDstArrayTemp, I + 1); // remove the element mDstArrayTemp. pop_back () ;}}// print the result set void OutSubSets () {for (ResultSet: iterator it = gResultSet. begin (); it! = GResultSet. end (); ++ it) {if (it-> empty () {cout <empty set <endl; continue;} for (IntArray :: iterator itTemp = it-> begin (); itTemp! = It-> end (); ++ itTemp) {cout <* itTemp <;} cout <endl;} cout <-------------------------------------------- <endl ;} int main () {IntArray mSrcArray; int nTemp; while (true) {mSrcArray. clear (); while (cin> nTemp) {if (nTemp = 0) break; mSrcArray. push_back (nTemp);} // sort (mSrcArray. begin (), mSrcArray. end (); gResultSet. clear (); // GetSubSetsAmortized (mSrcArray); IntArray mDstArrayTemp; GetSubSetsRecursiveEx (mSrcArray, mDstArrayTemp, 0); // print the result set OutSubSets ();} return 0 ;}
    
   
  
 


 

 

Series of articles:
1. This series of articles [algorithm exercises] are only a record of my learning process and self-motivation. They do not mean any preaching. It is my pleasure to give readers some knowledge and insights.
2. This series of articles is my understanding of some of the experiences written by Chen Dongfeng's book "coming into silicon valley, programmer interview secrets". Most of the opinions in this article come from this book!
3. There are many mistakes and shortcomings in the article. You are welcome to criticize and correct them. Thank you.

 

 

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.