/* This program reads strings on standard input and treats them as elements of a set. * It then prints all subsets of that set. */ #include #include #include #include #include #include #include #include #include using namespace std; int main() { vector items; string s; size_t j; int k; int len; char buf[100]; /* Get the input strings and error check. */ while (cin >> s) items.push_back(s); if (items.size() > 30) { cerr << "Sorry, not generating more than 2^30 subsets\n"; exit(1); } /* Calculate the number of characters in the hex for the numbers that we're going to use to represent the subsets. */ snprintf(buf, 100, "%x", (1 << items.size()) - 1); s = buf; len = s.size(); /* Enumerate the subsets. */ for (k = 0; k < (1 << items.size()); k++) { printf("0x%0*x ", len, k); // Print the subset in hex. /* Print the subset in bits (smallest first). */ for (j = 0; j < items.size(); j++) { printf("%c", (k & (1 << j)) ? '1' : '0'); } /* Print the strings that are in the subset. */ for (j = 0; j < items.size(); j++) { if (k & (1 << j)) printf(" %s", items[j].c_str()); } printf("\n"); } exit(0); }