Output all child sets of a set

Source: Internet
Author: User

# Include <iostream>
Using namespace STD;

Const int n= 1000;

Void selsubstr (INT flag [], int count)
{
For (INT I = 0; I <count; I ++)
{
Flag [I] = Flag [I] ^ 1;
// Stop if the value is 0 after the current element is reversed. In this way, there are 2 ^ n-1 cases, and there are no duplicates.
If (! Flag [I])
Break;
}
}

Void print (INT flag [], int count, const char * STR, char * substr)
{
Int J = 0;
For (INT I = 0; I <count; I ++)
{
If (flag [I]) // place the character to be output in substr
Substr [J ++] = STR [I];
}
Substr [J] = '/0 ';
Cout <substr <Endl;
}

Int main ()
{
Char * STR = new char [N]; // original string
Char * substr = new char [N]; // temporary string storage for the subset
Cout <"input a string :";
Cin. Getline (STR, N );
Int COUNT = strlen (STR );

int * flag = new int [count];
// when the default start is set, all elements are not displayed.
for (INT I = 0; I flag [I] = 0;
cout <"as follow is substr:" int substrcount = 0; // count the number of subsets
int q = 2 <(count-1 ); // calculate the number of cycles
for (INT I = 0; I {< br> selsubstr (flag, count ); // obtain the substring (subset)
Print (flag, Count, STR, substr); // display the substring (subset)
substrcount ++;
}< br> cout <"Total number of the subset (include empty-set) is:" return 0;
}

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.