包含所有可能组合的字符串列表的功率集

时间:2019-06-26 07:28:46

标签: c# combinations powerset

我在list中有一组数据,例如

  • john

  • 詹姆斯

  • 史密斯

  • j

  • s

我需要所有可能的组合和订单,例如

  • j's James

  • s j James

  • 史密斯j詹姆斯

  • j james

  • s j

列表大小不是预先设置的,而是基于用户输入的数据

我在这里查看了几种解决方案,并且接近获得所需的东西,例如,我得到了回报

  • j s james

  • s james

  • 史密斯j詹姆斯

但是我永远不会得到以詹姆斯开头的结果

以下链接中提供的一些解决方案 Listing all permutations of a string/integer Creating a power set of a Sequence All Possible Combinations of a list of Values

public static IList<IList<T>> PowerSet<T>(IList<T> list)
{       
    int n = 1 << list.Count;
    IList<IList<T>> powerset = new List<IList<T>>();
    for (int i = 0; i < n; ++i)
    {
        IList<T> set = new List<T>();
        for (int bits = i, j = 0; bits != 0; bits >>= 1, ++j)
        {
            if ((bits & 1) != 0)
                set.Add(list[j]);
        }
        powerset.Add(set);
    }
    return powerset;
}

在上面的代码上抛出list可以得到我能得到的最接近的代码,但不会返回所有可能的组合

我从这里得到了这段代码,但是如果我能找到原始代码,那么我就无法找到上面的代码

0 个答案:

没有答案