三种方法生成子集

 1 #include 
 2 #define _for(i,a,b) for(int i = (a);i < (b);i ++)
 3 using namespace std;
 4 
 5 vectorint>> rnt;
 6 void print_subset1(vector<int> A,vector<int> B)
 7 {
 8     vector<int> tmp;
 9     _for(i,0,A.size())
10         tmp.push_back(B[A[i]]);
11     rnt.push_back(tmp);
12     
13     int s = A.size() ? A[A.size()-1]+1 : 0;
14     _for(i,s,B.size())
15     {
16         A.push_back(i);
17         print_subset1(A,B);
18         A.pop_back();
19     } 
20 }
21 
22 void print_subset2(vector<int> A,vector<int> B)
23 {
24     if(A.size()==B.size())
25     {
26         vector<int> tmp;
27         _for(i,0,A.size())
28             if(A[i]) tmp.push_back(B[i]);
29         rnt.push_back(tmp);
30         return ;
31     }
32     A.push_back(1); print_subset2(A,B);
33     A.pop_back(); A.push_back(0);print_subset2(A,B);
34 }
35 
36 void _print_subset3(vector<int> B,int s)
37 {
38     vector<int> tmp;
39     _for(i,0,B.size())
40         if(s&(1<<i)) tmp.push_back(B[i]);
41     rnt.push_back(tmp); 
42 }
43 
44 void print_subset3(vector<int> B)
45 {
46     _for(i,0,1<<B.size())
47         _print_subset3(B,i);
48 }
49 
50 void print_rnt()
51 {
52     _for(i,0,rnt.size())
53     {
54         _for(j,0,rnt[i].size())
55         {
56             cout << rnt[i][j] << " ";
57         }
58         cout << endl;
59     }
60     cout << endl;
61 }
62 int main()
63 {
64     vector<int> A;
65     vector<int> B {1,3,9};
66     
67     
68     cout << "Incremental Construction:" << endl; 
69     print_subset1(A,B);
70     print_rnt();
71     rnt.clear();
72     
73     
74     cout << "Bit Vector" << endl;
75     print_subset2(A,B);
76     print_rnt();
77     rnt.clear();
78     
79     
80     cout << "Binary System" << endl;
81     print_subset3(B);
82     print_rnt();
83     rnt.clear();
84     
85     
86     return 0;
87 }

 

转载于:https://www.cnblogs.com/Asurudo/p/10039963.html

你可能感兴趣的:(三种方法生成子集)