# "Algorithmic Competition Primer Classic" 7.3 Subset Generation "incremental construction" "bit vector Method" "Binary method"

Source: Internet
Author: User

7.3.1 Incremental Construction method

Idea: Select one element at a time to put it in the collection. My own understanding of recursion is not enough, although there is no explicit recursive stop condition, but if you can not continue to add elements, it will not continue to recursion, and then my headache is backtracking.

`#include <stdio.h>intnum[4],n;voidAintNint*a,intans) {     for(inti =0; i < ans; i + +)//Print Current elementprintf"%d", A[i]); printf ("\ n"); ints = ans?a[ans-1]+1:0;//determines the minimum possible value of the current element     for(inti = s; I < n; i + +) {A[ans]=i; A (N,a,ans+1);//Recursive constructed subset    }     return;}intMain () {n=3; A (N,num,0); return 0;}`

7.3.2-bit vector method

Idea: Constructs a bit vector a[i], if a[i]=1, if and only if I is in subset A of the set.

`#include <stdio.h>intnum[4],n;voidPrint_subset (intNint*a,intans) {    if(ans = =N) { for(inti =0; i < ans; i + +)//Print the current collection            if(A[i]) printf ("%d", i); printf ("\ n"); return; } A[ans]=1;//Select cur elementPrint_subset (n,a,ans+1); A[ans]=0;//do not select the first CUR elementPrint_subset (n,a,ans+1); return;}intMain () {n=3; Print_subset (N,num,0); return 0;}`

7.3.3 Binary method

`#include <stdio.h>intn =3;voidPrint_subset (intNintans) {     for(inti =0; I < n; i + +)//print a subset of {0,1,2,3..n-1} ans        if(ans& (1<<i)) printf ("%d", i); printf ("\ n"); return;}intMain () { for(inti =0; I < (1&LT;&LT;N); i + +)//enumerate the encodings corresponding to each subsetPrint_subset (n,i); return 0;}`

"Algorithmic Competition Primer Classic" 7.3 Subset Generation "incremental construction" "bit vector Method" "Binary method"

Related Keywords:

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.