C# C语言中k个单词n个重复的组合算法

C# C语言中k个单词n个重复的组合算法,c#,C#,我需要一个组合算法,从用户那里获取2个信息: 字符串列表 重复计数 我想让用户列表的组合重复计数。像这样, Apple,Bear,Cat,Dog 3 结果: Apple Bear Cat Dog Apple Bear Bear Cat Cat Dog Apple Cat Bear Dog Apple Dog Bear Cat Dog Apple Bear Cat Apple Bear Dog Apple Cat Dog Apple Bear Cat Dog Apple Bear Apple

我需要一个组合算法,从用户那里获取2个信息:

字符串列表 重复计数 我想让用户列表的组合重复计数。像这样,

Apple,Bear,Cat,Dog
3
结果:

Apple
Bear
Cat
Dog
Apple Bear
Bear Cat
Cat Dog
Apple Cat
Bear Dog
Apple Dog
Bear Cat Dog
Apple Bear Cat
Apple Bear Dog
Apple Cat Dog
Apple
Bear
Cat
Dog
Apple Bear
Apple Cat
Apple Dog
Bear Cat
Bear Dog
Cat Dog
Apple Bear Cat
Apple Bear Dog
Apple Cat Dog
Bear Cat Dog
我不想要:

Apple Apple or Apple Dog, Dog Apple etc.
如何编写此代码?我不知道从哪里开始。

我推荐这个项目:。值得一读,这些类的效率至少比任何LINQ方法都要高:

例如:

IEnumerable<IEnumerable<T>> GetAllCombinations<T>(IList<T> itemList, int countRepetition)
{
    for (int lowerIndex = 1; lowerIndex <= countRepetition; lowerIndex++)
    {
        var combinations = new Combinations<T>(itemList, lowerIndex, GenerateOption.WithoutRepetition);
        foreach (IList<T> combination in combinations)
            yield return combination;
    }
}

您可以通过观察组合中任何项的存在或不存在可以用二进制数中的单个位来表示来解决此问题。要表示项目,您将使用每个项目一位

因此,通过从1到2^N-1进行计数,可以生成一组N项的所有可能组合。然后,对于数字中设置的每个位,在源数组中的相应索引处包含该项

如果对每个组合中的最大项数也有限制,则可以在选择该数字的项数之前检查其中设置的1位数,如果设置的位数过多,则跳过该位数

在一个数字中设置的位数称为its,有许多快速的方法来计算它

将所有这些整合到一个可编译的控制台应用程序中:

using System;
using System.Collections.Generic;
using System.Linq;

namespace Demo
{
    public class Program
    {
        static void Main()
        {
            List<string> sList = new List<string> { "Apple", "Bear", "Cat", "Dog" };

            var combinations = Combinations(sList, 3);

            foreach (var comb in combinations)
                Console.WriteLine(string.Join(", ", comb));

            // If you must have the combinations in order of number of items, you would have to sort it by count.
            // This will be EXTREMELY SLOW for large sets of data!

            Console.WriteLine();
            combinations = Combinations(sList, 3).OrderBy(comb => comb.Count);

            foreach (var comb in combinations)
                Console.WriteLine(string.Join(", ", comb));
        }

        /// <summary>Returns all the combinations of 'items' taken at most 'max' at a time.</summary>

        public static IEnumerable<List<T>> Combinations<T>(List<T> items, int max)
        {
            uint n = 1u << items.Count;

            for (uint i = 1; i < n; ++i)
            {
                int c = numBitsSet(i);

                if (c <= max)
                    yield return choose(items, c, i, n);
            }
        }

        static List<T> choose<T>(List<T> items, int count, uint mask, uint max)
        {
            var result = new List<T>(count);

            for (uint bit = 1, i = 0; bit < max; bit <<= 1, ++i)
                if ((mask & bit) != 0)
                    result.Add(items[(int)i]);

            return result;
        }

        static int numBitsSet(uint i)
        {
            i = i - ((i >> 1) & 0x55555555);
            i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
            return (int)(((i + (i >> 4)) & 0x0F0F0F0Fu) * 0x01010101u) >> 24;
        }
    }
}

欢迎来到StackOverflow。您需要向我们展示到目前为止您已经尝试了什么以及哪里出了问题,不幸的是,这不是一个代码编写服务!我想你的示例结果缺少苹果、猫、狗。是的,马修你是对的,我忘了写苹果猫狗结果,我会修复它。您好,它工作正常。组合是undefined@fubo:当然,这不是.NET framework中的类。你需要从我提供的链接下载它。它是测试项目的一部分,您可以在本文顶部下载。但本文对所使用的算法进行了讨论和解释。文件夹组合包含所有类。好的,谢谢澄清。
using System;
using System.Collections.Generic;
using System.Linq;

namespace Demo
{
    public class Program
    {
        static void Main()
        {
            List<string> sList = new List<string> { "Apple", "Bear", "Cat", "Dog" };

            var combinations = Combinations(sList, 3);

            foreach (var comb in combinations)
                Console.WriteLine(string.Join(", ", comb));

            // If you must have the combinations in order of number of items, you would have to sort it by count.
            // This will be EXTREMELY SLOW for large sets of data!

            Console.WriteLine();
            combinations = Combinations(sList, 3).OrderBy(comb => comb.Count);

            foreach (var comb in combinations)
                Console.WriteLine(string.Join(", ", comb));
        }

        /// <summary>Returns all the combinations of 'items' taken at most 'max' at a time.</summary>

        public static IEnumerable<List<T>> Combinations<T>(List<T> items, int max)
        {
            uint n = 1u << items.Count;

            for (uint i = 1; i < n; ++i)
            {
                int c = numBitsSet(i);

                if (c <= max)
                    yield return choose(items, c, i, n);
            }
        }

        static List<T> choose<T>(List<T> items, int count, uint mask, uint max)
        {
            var result = new List<T>(count);

            for (uint bit = 1, i = 0; bit < max; bit <<= 1, ++i)
                if ((mask & bit) != 0)
                    result.Add(items[(int)i]);

            return result;
        }

        static int numBitsSet(uint i)
        {
            i = i - ((i >> 1) & 0x55555555);
            i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
            return (int)(((i + (i >> 4)) & 0x0F0F0F0Fu) * 0x01010101u) >> 24;
        }
    }
}