我正在寻找一个快速实现的“排列”算法(与重复排列)。
给定n个对象(数量a中的a,数量b中的b,…),生成所有可能的组合。
示例:

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"

(如果可能,用C、C或帕斯卡编码)
提前谢谢
菲利普

最佳答案

如果你可以使用C++,它已经在标准库中提供:

#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;
}

编辑:输出为:
aaabcc aaacbc aaaccb aabacc aabcca aacabc aacacb aacbac aacbca
aaccab aaccba abaacc abacc abaca abcaac abcaca abccaa acaabc acacab
阿卡巴阿卡巴阿卡巴阿卡巴阿卡巴阿卡巴阿卡巴阿卡巴阿卡巴阿卡巴
baacc baacc baaca bacca bacca bacca bacca bcaac bacca bccaa
中国民航总局
卡卡巴卡巴卡巴卡巴卡巴卡巴卡巴卡巴卡巴卡巴卡巴卡巴卡巴卡巴卡巴卡巴卡巴卡巴卡巴卡巴卡巴卡巴卡巴卡巴
对于任何不能使用C++的人来说,Mark Nelson在几年前在C/C++用户期刊上写了一篇article,这可能是有帮助的。

10-06 10:40