Arrangement of n objects (Permutation with duplicates)
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 th开发者_StackOverflow中文版e 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
PhilippeIf 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.
精彩评论