+1  A: 

Actually your question is to list all subsets from a given set.

Considering the set {a,a,a,d,d,d,c,g,h,z,z}, your goal is to list all its unique subsets in order, except the empty set: {a} {a,a} {a,a,a} {a,a,a,d}

There is a quick way to list all subsets from a given set.

Let's take {ABC} as example:

{}     = 000
{C}    = 001
{B}    = 010
{BC}   = 011
{A}    = 100
{AC}   = 101
{AB}   = 110
{ABC}  = 111

See the pattern? Simply use an integer that grows from 0 to 2^n - 1. If the i'th digit of the integer is 1, fetch the i'th element from the set.

Note: Since in your example, there are duplicates in the string; therefore after generation you might need to remove duplicates.

Hope this can help you.

yinyueyouge
This is called the "power set". A search on Google for "c++ power set" is quite fruitful: http://www.google.com/search?q=c%2B%2B+power+set
Alex Reynolds
A: 

Well, it seems to me that one solution that is similar to yours, but doesn't match your output (see my comments on the question, though), is simply to iterate through the list of tails of the original string (e.g., for "abc", iterate through "abc", "bc" and "c"), and for each of those generate the list of prefixes ("abc", "ab", "a", then "bc", "b", then "c"). How does this compare to what you want?

Curt Sampson
+1  A: 

Following is a recursive algorithm to generate all subsequences.

/* in C -- I hope it will be intelligible */

#include <stdio.h>

static char input[] = "aaabbbccc";
static char output[sizeof input];

/* i is the current index in the input string
 * j is the current index in the output string
 */
static void printsubs(int i, int j) {
    /* print the current output string */
    output[j] = '\0';
    printf("%s\n", output);
    /* extend the output by each character from each remaining group and call ourselves recursively */
    while(input[i] != '\0') {
        output[j] = input[i];
        printsubs(i + 1, j + 1);
        /* find the next group of characters */
        do ++i;
        while(input[i] == input[i - 1]);
    }
}

int main(void) {
    printsubs(0, 0);
    return 0;
}

If your interest is merely in counting how many subsequences there are, you can do it much more efficiently. Simply count up how many of each letter there are, add 1 to each value, and multiply them together. In the above example, there are 3 a's, 3 b's, 3 c's, and 2 d's, for (3 + 1) * (3 + 1) * (3 + 1) * (2 + 1) = 192 subsequences. The reason this works is that you can choose between 0 and 3 a's, 0 and 3 b's, 0 and 3 c's, and 0 and 2 d's, and all of these choices are independent.

Dave
This does not take advantage of the s[i] <= s[i+1] constraint, that simplifies the problem a great deal.
Tom Leys
Yes it does. You didn't run it, did you?
Dave
I'll try this tonight - it seems incredibly short if it works!
20th Century Boy
I tested this - it works perfectly. In fact I think I'll accept it as the answer because it's so concise and more in the spirit of what I was thinking. Again, I would never have been able to come up with this myself, thank God for SO!
20th Century Boy
+5  A: 

An ammendement to Ryan Shaw's answer above:

Instead of counting in binary, count each digit in a base dependant on the number of each letter. For example:

a d c g h z
3 3 1 1 1 2

So count:

0 0 0 0 0 0
0 0 0 0 0 1
0 0 0 0 0 2
0 0 0 0 1 0
0 0 0 0 1 1 
0 0 0 0 1 2
0 0 0 1 0 0
...
0 0 0 1 1 2
0 0 1 0 0 0
...
0 0 1 1 1 2
0 1 0 0 0 0 
...
0 3 1 1 1 2
1 0 0 0 0 0
...
3 3 1 1 1 2

And you've enumerated all the possible subset, without duplicates. For any one of these outputting the string is simply a matter of looping through the digits and outputting as many of each letter as are specified.

1 2 0 0 1 1 => addhz
3 0 0 0 1 2 => aaahzz

And the code:

void GetCounts(const string &source, vector<char> &characters, vector<int> &counts)
{
    characters.clear();
    counts.clear();

    char currentChar = 0;
    for (string::const_iterator iSource = source.begin(); iSource != source.end(); ++iSource)
    {
     if (*iSource == currentChar)
      counts.back()++;
     else
     {
      characters.push_back(*iSource);
      counts.push_back(1);
      currentChar = *iSource;
     }
    }
}

bool Advance(vector<int> &current, const vector<int> &max)
{
    if (current.size() == 0)
     return false;

    current[0]++;
    for (size_t index = 0; index < current.size() - 1 && current[index] > max[index]; ++index)
    {
     current[index] = 0;
     current[index + 1]++;
    }
    if (current.back() > max.back())
     return false;
    return true;
}

string ToString(const vector<int> &current, const vector<char> &characters)
{
    string result;
    for (size_t index = 0; index < characters.size(); ++index)
     for (int i = 0; i < current[index]; ++i)
      result += characters[index];
    return result;
}

int main() { 
    vector<int> max;
    vector<char> characters;

    GetCounts("aaadddcghzz", characters, max);

    vector<int> current(characters.size(), 0);
    int index = 1;
    while (Advance(current, max))
    {
     cout << index++ << ":" << ToString(current, characters) << endl;
    }
}
Eclipse
Perfect solution. Now you need pseudocode
Tom Leys
This is all kinds of awesomeness. I could have thought about the problem for days (weeks? months?) and never come up with this. Did you make the code up on the fly or was it something you had already?
20th Century Boy
I've used things very similar to the Advance() function several times. It's really just an adder with carry, and with a variable base for each digit. It's a handy algorithm to have at your disposal.
Eclipse
A: 

i used this java code (http://www.merriampark.com/comb.htm) and came up with only 383. the code generates way too many duplicates so i had to throw a lot of them away. i ended up with only 383 (please see below). you probably want to look at the c++ code for next-combinatiom in the stl (but i could not find the source anywhere easily). the power set is probably the best approach (but you may have duplicates there also).

a
aa
aaa
aaac
aaacg
aaacgh
aaacghz
aaacghzz
aaacgz
aaacgzz
aaach
aaachz
aaachzz
aaacz
aaaczz
aaad
aaadc
aaadcg
aaadcgh
aaadcghz
aaadcghzz
aaadcgz
aaadcgzz
aaadch
aaadchz
aaadchzz
aaadcz
aaadczz
aaadd
aaaddc
aaaddcg
aaaddcgh
aaaddcghz
aaaddcghzz
aaaddcgz
aaaddcgzz
aaaddch
aaaddchz
aaaddchzz
aaaddcz
aaaddczz
aaaddd
aaadddc
aaadddcg
aaadddcgh
aaadddcghz
aaadddcghzz
aaadddcgz
aaadddcgzz
aaadddch
aaadddchz
aaadddchzz
aaadddcz
aaadddczz
aaadddg
aaadddgh
aaadddghz
aaadddghzz
aaadddgz
aaadddgzz
aaadddh
aaadddhz
aaadddhzz
aaadddz
aaadddzz
aaaddg
aaaddgh
aaaddghz
aaaddghzz
aaaddgz
aaaddgzz
aaaddh
aaaddhz
aaaddhzz
aaaddz
aaaddzz
aaadg
aaadgh
aaadghz
aaadghzz
aaadgz
aaadgzz
aaadh
aaadhz
aaadhzz
aaadz
aaadzz
aaag
aaagh
aaaghz
aaaghzz
aaagz
aaagzz
aaah
aaahz
aaahzz
aaaz
aaazz
aac
aacg
aacgh
aacghz
aacghzz
aacgz
aacgzz
aach
aachz
aachzz
aacz
aaczz
aad
aadc
aadcg
aadcgh
aadcghz
aadcghzz
aadcgz
aadcgzz
aadch
aadchz
aadchzz
aadcz
aadczz
aadd
aaddc
aaddcg
aaddcgh
aaddcghz
aaddcghzz
aaddcgz
aaddcgzz
aaddch
aaddchz
aaddchzz
aaddcz
aaddczz
aaddd
aadddc
aadddcg
aadddcgh
aadddcghz
aadddcghzz
aadddcgz
aadddcgzz
aadddch
aadddchz
aadddchzz
aadddcz
aadddczz
aadddg
aadddgh
aadddghz
aadddghzz
aadddgz
aadddgzz
aadddh
aadddhz
aadddhzz
aadddz
aadddzz
aaddg
aaddgh
aaddghz
aaddghzz
aaddgz
aaddgzz
aaddh
aaddhz
aaddhzz
aaddz
aaddzz
aadg
aadgh
aadghz
aadghzz
aadgz
aadgzz
aadh
aadhz
aadhzz
aadz
aadzz
aag
aagh
aaghz
aaghzz
aagz
aagzz
aah
aahz
aahzz
aaz
aazz
ac
acg
acgh
acghz
acghzz
acgz
acgzz
ach
achz
achzz
acz
aczz
ad
adc
adcg
adcgh
adcghz
adcghzz
adcgz
adcgzz
adch
adchz
adchzz
adcz
adczz
add
addc
addcg
addcgh
addcghz
addcghzz
addcgz
addcgzz
addch
addchz
addchzz
addcz
addczz
addd
adddc
adddcg
adddcgh
adddcghz
adddcghzz
adddcgz
adddcgzz
adddch
adddchz
adddchzz
adddcz
adddczz
adddg
adddgh
adddghz
adddghzz
adddgz
adddgzz
adddh
adddhz
adddhzz
adddz
adddzz
addg
addgh
addghz
addghzz
addgz
addgzz
addh
addhz
addhzz
addz
addzz
adg
adgh
adghz
adghzz
adgz
adgzz
adh
adhz
adhzz
adz
adzz
ag
agh
aghz
aghzz
agz
agzz
ah
ahz
ahzz
az
azz
c
cg
cgh
cghz
cghzz
cgz
cgzz
ch
chz
chzz
cz
czz
d
dc
dcg
dcgh
dcghz
dcghzz
dcgz
dcgzz
dch
dchz
dchzz
dcz
dczz
dd
ddc
ddcg
ddcgh
ddcghz
ddcghzz
ddcgz
ddcgzz
ddch
ddchz
ddchzz
ddcz
ddczz
ddd
dddc
dddcg
dddcgh
dddcghz
dddcghzz
dddcgz
dddcgzz
dddch
dddchz
dddchzz
dddcz
dddczz
dddg
dddgh
dddghz
dddghzz
dddgz
dddgzz
dddh
dddhz
dddhzz
dddz
dddzz
ddg
ddgh
ddghz
ddghzz
ddgz
ddgzz
ddh
ddhz
ddhzz
ddz
ddzz
dg
dgh
dghz
dghzz
dgz
dgzz
dh
dhz
dhzz
dz
dzz
g
gh
ghz
ghzz
gz
gzz
h
hz
hzz
z
zz
Ray Tayek
The 384th result is the blank string.
Eclipse