views:

66

answers:

1

Hi everybody,

I am looking for a fast implementation of the "arrangement" algorithm (permutation with duplicates). Given N objects (A in quantity a, B in quantity b, ...), generate all the possible combinations.
Exemple:

Arrangement("AAA", "B", "CC") would return :   
"AAABCC" "AABACC" "AABCAC" "AABCCA" "ABAACC" "ABACAC" "ABACCA" "ABCAAC"   
"ABCACA" "ABCCAA" "BAAACC" "BAACAC" "BAACCA" "BACAAC" "BACACA" "BACCAA"   
"BCAAAC" "BCAACA" "BCACAA" "BCCAAA" "AAACBC" "AACABC" "AACBAC" "AACBCA"   
"ACAABC" "ACABAC" "ACABCA" "ACBAAC" "ACBACA" "ACBCAA" "CAAABC" "CAABAC"   
"CAABCA" "CABAAC" "CABACA" "CABCAA" "CBAAAC" "CBAACA" "CBACAA" "CBCAAA"   
"AAACCB" "AACACB" "AACCAB" "AACCBA" "ACAACB" "ACACAB" "ACACBA" "ACCAAB"   
"ACCABA" "ACCBAA" "CAAACB" "CAACAB" "CAACBA" "CACAAB" "CACABA" "CACBAA"   
"CCAAAB" "CCAABA" "CCABAA" "CCBAAA"  

(Code in C, C# or Pascal if possible)

Thanks in advance
Philippe

+6  A: 

If you can use C++, it's already provided in the standard library:

#include <algorithm>
#include <string>
#include <iostream>

int main() { 
    std::string a("AAABCC");

    do {
        std::cout << a << "\t";
    } while (std::next_permutation(a.begin(), a.end()));
    return 0;
}

Edit: the output from this is:

AAABCC AAACBC AAACCB AABACC AABCAC AABCCA AACABC AACACB AACBAC AACBCA AACCAB AACCBA ABAACC ABACAC ABACCA ABCAAC ABCACA ABCCAA ACAABC ACAACB ACABAC ACABCA ACACAB ACACBA ACBAAC ACBACA ACBCAA ACCAAB ACCABA ACCBAA BAAACC BAACAC BAACCA BACAAC BACACA BACCAA BCAAAC BCAACA BCACAA BCCAAA CAAABC CAAACB CAABAC CAABCA CAACAB CAACBA CABAAC CABACA CABCAA CACAAB CACABA CACBAA CBAAAC CBAACA CBACAA CBCAAA CCAAAB CCAABA CCABAA CCBAAA

For anybody who can't use C++, Mark Nelson wrote an article in the C/C++ User's Journal a few years ago that might be helpful.

Jerry Coffin
Unfortunatly, I can't use C++ library ...
PhilippeC
@PhilippeC: perhaps the link I've added to the answer will be helpful.
Jerry Coffin
Hi Jerry: thanks for the link. I had this function in my tools but never realized it worked with duplicates ...
PhilippeC