Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 不重复的单词组合_C#_String_Combinations_Words - Fatal编程技术网

C# 不重复的单词组合

C# 不重复的单词组合,c#,string,combinations,words,C#,String,Combinations,Words,我有10个字。如何获得5个单词的所有可能组合(n=10,k=5)。顺序不重要 例如:“A”、“B”、“C”,如果k=2(在本例中n=3),它将使用AB、BC和AC。也许您知道一些有用的代码或示例 顺便说一句,如果我说得不对的话,我很抱歉,因为我的英语不是很好。你要做的是获取集合的所有排列 以下是代码片段: static void Main(string[] args) { var list = new List<string> { "a", "b", "c", "d"

我有10个字。如何获得5个单词的所有可能组合
(n=10,k=5)
。顺序不重要

例如:
“A”、“B”、“C”,如果k=2(在本例中n=3)
,它将使用AB、BC和AC。也许您知道一些有用的代码或示例


顺便说一句,如果我说得不对的话,我很抱歉,因为我的英语不是很好。

你要做的是获取集合的所有排列

以下是代码片段:

static void Main(string[] args)
{
    var list = new List<string> { "a", "b", "c", "d", "e" };
    var result = GetPermutations(list, 3);
    foreach (var perm in result)
    {
        foreach (var c in perm)
        {
            Console.Write(c + " ");
        }
        Console.WriteLine();
    }
    Console.ReadKey();
}

static IEnumerable<IEnumerable<T>> GetPermutations<T>(IEnumerable<T> items, int count)
{
    int i = 0;
    foreach (var item in items)
    {
        if (count == 1)
            yield return new T[] { item };
        else
        {
            foreach (var result in GetPermutations(items.Skip(i + 1), count - 1))
                yield return new T[] { item }.Concat(result);
        }

        ++i;
    }
}

一个更实用的解决方案怎么样

var list = new List<string> { "a", "b", "c", "d", "e" };
GetAllCombinations(list).OrderBy(_ => _).ToList().ForEach(Console.WriteLine);


static IEnumerable<string> GetAllCombinations(IEnumerable<string> list)
{
    return list.SelectMany(mainItem => list.Where(otherItem => !otherItem.Equals(mainItem))
                              .Select(otherItem => mainItem + otherItem));
}

以下是我总结的内容:

static class LinqExtensions
{
    public static IEnumerable<IEnumerable<T>> CombinationsWithoutRepetition<T>(
        this IEnumerable<T> items,
        int ofLength)
    {
        return (ofLength == 1) ?
            items.Select(item => new[] { item }) :
            items.SelectMany((item, i) => items.Skip(i + 1)
                                               .CombinationsWithoutRepetition(ofLength - 1)
                                               .Select(result => new T[] { item }.Concat(result)));
    }

    public static IEnumerable<IEnumerable<T>> CombinationsWithoutRepetition<T>(
        this IEnumerable<T> items,
        int ofLength,
        int upToLength)
    {
        return Enumerable.Range(ofLength, Math.Max(0, upToLength - ofLength + 1))
                         .SelectMany(len => items.CombinationsWithoutRepetition(ofLength: len));
    }

}
产生:

a,b
a,c
a,d
b,c
b,d
c,d
a,b,c
a,b,d
a,c,d
b,c,d
a,b,c,d
请注意,这虽然简洁但效率低下,不应用于大型集合或内部循环。值得注意的是,短数组被多次重新创建并可以被记忆,而
IEnumerable
将被多次迭代,如果不小心,可能会导致意外的工作


此外,如果输入包含重复项,则输出也会重复。可以先使用
.Distinct().ToArray()
,或者使用另一种解决方案,其中包括等式检查,并且可能需要一个
IEqualityComparer
以获得通用性。

在数学上更加正确。您可以不重复地调用此组合(顺序不重要)(一个项目最多出现1次或更少)。“最多1次”是否意味着“或更少”?OP指定顺序无关紧要,因此代码结果中出现的“ab”和“ba”是相同的。欢迎使用堆栈溢出。虽然本准则可以回答该问题,但提供关于本准则为什么和/或如何回答该问题的附加上下文可提高其长期价值。亲切的问候。
public IActionResult Index() 
{
    var list = new List<string> { "a", "b", "c", "d", "e" };
    List<string> ret = GetAllCombinations(list).OrderBy(_ => _).ToList();

    return View();
}

static IEnumerable<string> GetAllCombinations(IEnumerable<string> list)
{
    return list.SelectMany((mainItem, mi) => list.Where((otherItem, oi) => mi < oi)
                              .Select(otherItem => mainItem + otherItem));
}
foreach (var c in new[] {"a","b","c","d"}.CombinationsWithoutRepetition(ofLength: 2, upToLength: 4))
{
    Console.WriteLine(string.Join(',', c));
}
a,b
a,c
a,d
b,c
b,d
c,d
a,b,c
a,b,d
a,c,d
b,c,d
a,b,c,d
public IActionResult Index() 
{
    var list = new List<string> { "a", "b", "c", "d", "e" };
    List<string> ret = GetAllCombinations(list).OrderBy(_ => _).ToList();

    return View();
}

static IEnumerable<string> GetAllCombinations(IEnumerable<string> list)
{
    return list.SelectMany((mainItem, mi) => list.Where((otherItem, oi) => mi < oi)
                              .Select(otherItem => mainItem + otherItem));
}
ab
ac
ad
ae
bc
bd
be
cd
ce
de