n 个对象的排列(重复排列)

发布于 2024-09-25 14:06:22 字数 778 浏览 3 评论 0原文

我正在寻找“排列”算法的快速实现(重复排列)。 给定 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# 或 Pascal 编写代码)

提前致谢
菲利普

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尛丟丟 2024-10-02 14:06:22

如果您可以使用 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;
}

编辑:输出为:

AAAABCC 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

对于任何不会使用 C++ 的人,Mark Nelson 写了一个 文章 可能会有所帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文