【问题标题】:How to get all combinations of a set of sets?如何获得一组集合的所有组合?
【发布时间】:2017-06-22 20:13:37
【问题描述】:

我有一组字符串。

我需要找到所有可能的字符串组合。

有什么最好的方法来解决这个问题吗?

语言是 C#,但我不是在寻找具体的实现,只是解决问题的一般方法。

【问题讨论】:

  • 查看power sets
  • 每组中的一个字符串中的每个组合或任意数量组中任意数量字符串中的每个组合?请发布一个示例。
  • 在任意数量的集合中的任意数量的字符串中。
  • 谢谢 Henrik - 这似乎就是我要找的东西 ;-)
  • @ThomasCook:没问题。不妨看看 Guava 的 PowerSet 类。

标签: algorithm set combinatorics


【解决方案1】:

将字符串放入 Listn 中,然后创建将生成元素随机组合的方法。类似的东西:

How to make a combination of strings in C#?

编辑: 将字符串List的所有List合并成一个长字符串List。

    List<List<String>> sets = new List<List<String>>();
    List<String> allProducts = new List<String>();
    List<String> set1 = new List<String>() { "one", "two", "three" };
    List<String> set2 = new List<String>() { "111", "222", "333" };

    sets.Add(set1);
    sets.Add(set2);

    foreach (var set in sets)
    {
        allProducts.AddRange(set);
    }

然后像上面的条目一样对 allProducts 进行操作。

【讨论】:

  • 是的,但我正在处理一组集合,如下所示: List> 我需要找到所有组合
  • 你能举个例子吗?
  • 示例:var toCombine= new List&lt;List&lt;string&gt;&gt; { new List&lt;string&gt;() { "ABC", "DEF" }, new List&lt;string&gt;() { "39", "21" }, new List&lt;string&gt;() { "Foo", "Bar" } }; List&lt;string&gt; GetAllCombinations(List&lt;List&lt;string&gt;&gt; of) { // Return a set of strings where each string in the set is a combination of the input set of sets such that all possible combinations are returned... }
  • 我不确定如何格式化注释中的代码,所以它都是内联的,抱歉
  • 好的,您可以使用我上面使用的代码创建一个字符串列表,然后应用如何在 C# 中组合字符串?
猜你喜欢
  • 2012-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-04
  • 1970-01-01
  • 1970-01-01
  • 2018-05-08
相关资源
最近更新 更多