【问题标题】:How to find the permutations of a string? [closed]如何找到字符串的排列? [关闭]
【发布时间】:2011-03-10 11:31:44
【问题描述】:

我怎样才能找到特定字符串的组合......

ABCD - "",A,B,C,AB,AC,AD,BC,BD,CD,ABC,ACD,ABD,ABCD,...

C 编程语言....

谢谢..

【问题讨论】:

  • 这不是排列,这是所有可能的子集......
  • 尝试搜索此站点。
  • 当我问一个问题时,我就像在编程一样考虑它。我希望它清晰,没有拼写错误,格式正确。看到这样的问题让我哭笑不得:(

标签: algorithm data-structures subset


【解决方案1】:

在集合中插入您需要的字母并调用 findSubset 。

Set findSubsets(Set A)
{
     if A is the empty set
            return the empty set

     // The first element of our set is A[0]
     // (A - A[0] is the set A without the first element)
     Set B := findSubsets(A - A[0])

     // Set B is now all the subsets of A without the first element
     // We now find all the subsets of A containing the first element by finding
     // tacking on the first element to the sets inside B
     Set C = {}
     For each set D in B
            add {A[0], D} to C

    return B added to C
}

【讨论】:

    【解决方案2】:

    在 python 中,标准库 ìtertools 应该会有所帮助。看看combinations() 函数,也许其他函数取决于你想要的exacly。您想允许重复吗?顺序重要吗?

    【讨论】:

    • 需要编程..理解逻辑...
    • 首先,您需要更准确地表达您的问题。术语 permutationscombinations 被定义为不同的术语,然后,您的描述指向 subsets...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-03
    • 2013-07-08
    • 1970-01-01
    • 2016-04-30
    • 2021-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多