【问题标题】:Permutations & combination generating algorithm排列组合生成算法
【发布时间】:2012-10-22 11:30:17
【问题描述】:

我想写一个算法来打印给定“”对的所有可能组合,我试图开发一种算法来解决这个问题,但我认为这是不正确的,因为我确实意识到了这个问题与排列 [nPr] 相关,假设给定输入为 5,它应该创建 120 个组合(5P5=120),但我的代码只生成 81 个。

In my code have tried to generate all possible combinations by placing every element at every place one by one, but now I am little confused about how correct this approach is?

事情很可能无法掌握“制作子集/组合/排列”的真正概念(尽管理论上我知道它们是什么以及如何计算它们)

我不是在寻找一个完整的最终“勺式代码”,而是可以解释我“我应该做什么”的东西,我可以从中提取步骤,理解概念并开发自己的。

If possible something extending or tweaking my current coding to achieve the right result would be easier for me to understand.

void permute()
{
    string str=”<><><>”;
    char buck=' ';
for(int a=0;a<str.length()-1;a++)
    {
        for(int b=0;b<str.length()-1;b++){
            cout<<str<<endl;
            buck=str[b];
            str[b]=str[b+1];
            str[b+1]=buck;
        }
    }
}

我一直在努力了解我应该做什么,但我仍在苦苦挣扎,任何帮助或指导都会非常有帮助。 谢谢你


From 'all combinations' i mean printing out all the possible ways given set of characters can be arranged, lets say for 2 pairs '&lt;&gt;&lt;&gt;' it should be like: &lt;&gt;&lt;&gt;,&gt;&lt;&lt;&gt;,&gt;&lt;&lt;&gt;,&gt;&lt;&gt;&lt;,&lt;&lt;&gt;&gt;,&gt;&gt;&lt;&lt; ... ... ...

【问题讨论】:

  • 您可能想看看cs.sunysb.edu/~algorith/files/generating-permutations.shtml 以及“递归生成排列”。
  • 你能举例说明 n=2 吗?我不确定 的所有组合到底是什么意思
  • 排列和组合是不同的东西,需要不同的算法。你应该明确你想要哪一个。
  • &gt;&lt;&gt;&lt;&gt;&lt; 是有效的解决方案吗?如果不是 - 你有Cn(n/2) 的可能性,其中Cn 表示Catalan number
  • @Maven:那么您要求的是标准排列。那里有很多细节。您的编辑澄清了它与加泰罗尼亚语编号无关。

标签: c++ algorithm set permutation combinations


【解决方案1】:

C++ 提供bool std::next_permutation(Iterator first, Iterator last) 将 (first, last) 的内容修改为序列中的下一个排列,如果有更多排列则返回 true,如果这是最后一个排列则返回 false。列表需要先排序(使用std::sort(Iterator first, Iterator last)),排序后的列表形成第一个排列。

您可以使用str.begin()str.end() 与这些算法进行交互。

注意:因为您的数据集包含重复项,并非所有排列都是可能的(有些可能与其他条目重复)。那就是:

string : permutations
-------:-------------
abcd   : 24
<><>   : 6
abcdef : 720
<><><> : 20

如果您真的想要所有排列(包括重复),您可以拥有一个 int indices = { 0, 1, 2, 3, 4, 5 }; 数组,在该数组上运行排列,然后为每个排列打印 str[indices[0]]str[indices[5]]

这可以让您深入了解您的算法以及出了什么问题。也就是说,它可以作为比较您的算法的参考。

【讨论】:

    【解决方案2】:

    根据我的测试,它有 42 个解决方案:

        function placeBrackets(n)
        {
            var placeBracketsRecur = function(prefix, remainingOpen, remainingClosed)
            {
                if(remainingClosed == 0)
                {
                    document.write(prefix + "<br/>");
                    return;
                }
    
                if(remainingOpen > 0)
                {
                    placeBracketsRecur(prefix + "(",  remainingOpen - 1, remainingClosed);
                }
    
                if(remainingOpen < remainingClosed)
                {
                    placeBracketsRecur(prefix + ")",  remainingOpen, remainingClosed - 1);
                }
            }
    
            placeBracketsRecur("", n, n);
        }
    

    ///输出

    ((((()))))
    (((()())))
    (((())()))
    (((()))())
    (((())))()
    ((()(())))
    ((()()()))
    ((()())())
    ((()()))()
    ((())(()))
    ((())()())
    ((())())()
    ((()))(())
    ((()))()()
    (()((())))
    (()(()()))
    (()(())())
    (()(()))()
    (()()(()))
    (()()()())
    (()()())()
    (()())(())
    (()())()()
    (())((()))
    (())(()())
    (())(())()
    (())()(())
    (())()()()
    ()(((())))
    ()((()()))
    ()((())())
    ()((()))()
    ()(()(()))
    ()(()()())
    ()(()())()
    ()(())(())
    ()(())()()
    ()()((()))
    ()()(()())
    ()()(())()
    ()()()(())
    ()()()()()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-05
      • 1970-01-01
      • 1970-01-01
      • 2011-06-18
      • 2012-10-24
      • 1970-01-01
      • 2013-11-28
      • 1970-01-01
      相关资源
      最近更新 更多