I was using bit-shift to generate a powerset of a given numeric string. How can I restrict it to a certain length, say 4, and thus improve the execution time by not finding the subsequences of undesired length.
For ex: if given numeric string is 10292
, then only following subsequences are needed: 1029, 102, 109, 029, 0292, etc (only with digits 4,3,2,1).
Following is my code:
scanf("%s", &str); //read numeric string
int n = strlen(str); //find size of string
// loop to find subsequences or powerset
for ( i = 1; i < ( 1 << n ); ++i ) {
string subseq;
for ( j = 0; j < n; ++j ) {
if ( i & ( 1 << j ) ) {
subseq+=str[j];
}
}
cout << subseq << endl; //print the subsequence
}