我正在使用位移来生成给定数字字符串的幂集。我怎样才能将它限制在一定的长度,比如 4,从而通过不找到不需要长度的子序列来提高执行时间。
例如:如果给定的数字字符串是10292
,则只需要以下子序列:1029、102、109、029、0292 等(仅限数字 4、3、2、1)。
以下是我的代码:
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
}