hessam wrote:
> There is a problem with that algorithm. It may print the same string
> more than once:
> e.g. for input "abb" and K = 2, it will print:
>
> ab
> ab
>
> I recommend storing the strings in a trie.
The OP asked for _all_ subsequences. He said nothing about uniqueness.
But I didn't write to quibble over problem definition. If you do more
algebra on the explicit stack code I posted, you can eliminate some
stack thrashing and also obtain a nicer-looking result.
void print_all_subsequences(char *s, int is, char *r, char ir)
{
int iss[1000], irs = ir, i = 0;
for (;;) {
if (ir < 0) {
printf("\n%s", r);
if (i <= 0)
break;
is = iss[--i];
ir = ++irs;
}
else {
r[ir--] = s[is--];
if (is > ir) {
--irs;
iss[i++] = is;
}
}
}
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/algogeeks
-~----------~----~----~----~------~----~------~--~---