计算串的所有可能的组合,与转弯组合

2023-09-11 05:46:58 作者:骄纵

我想允许用户在文本框中输入文字,并有计划产生的一切可能的组合,除了用最少的3个字符的6和最大我不需要没用的话像'作为','A','我','要'等塞满了我的数组。我还可以检查每个组合对一个字典,以确保它是一个真正的词。

I'm trying to allow a user to enter text in a textbox, and have the program generate all possible combinations of it, except with a minimum of 3 characters and maximum of 6. I don't need useless words like 'as', 'a', 'i', 'to', etc cluttering up my array. I'll also be checking each combination against a dictionary to make sure it's a real word.

我的字典完成(苦心产生,这里有一个链接到它的回报(警告:巨大的加载时间(对我来说)!)

I have the dictionary complete (painstakingly generated, here's a link to it in return (WARNING: gigantic load time (for me)!)

不管怎么说,如果用户输入ABCDEF(排名不分先后),我怎么可能产生,例如:

Anyways, if the user enters 'ABCDEF' (in no particular order), how could I generate, for example:

'ABC'
'BAC'
'CAB'
...
'ABD'
'ABE'
'ABF'

等等各种可能的组合,无论什么样的顺序?据我所知,有这些组合中的一个荒谬的数额,但只需要计算一次,所以我不是太担心。

etc... EVERY possible combination, no matter what order? I understand that there are a ridiculous amount of these combinations, but it only needs to be calculated once, so I'm not too worried about that.

我发现code样本递归找到的组合(不排列,我不需要那些)(ABCDEF,ABCDFE ... ACDBFE等)。他们没有做什么,我需要的,我没有在哪里,甚至开始与该项目丝毫线索。

I've found code samples to recursively find combinations (not permutations, I don't need those) of just the fixed-width string (ABCDEF, ABCDFE ... ACDBFE, etc). They don't do what I need, and I haven't the slightest clue about where to even start with this project.

这是不是功课,它开始了作为的成长接管我的生命在这样一个简单的问题,我个人的项目......我简直不敢相信我能不知道这一点!

This isn't homework, it started out as a personal project of mine that's grown to take over my life over such a simple problem... I can't believe I can't figure this out!

推荐答案

这听起来我像你描述的发电机组

It sounds to me like you're describing the Power Set

下面是一个实现我已经躺在附近我个人的库:

Here's an implementation I had lying around my personal library:

// Helper method to count set bits in an integer
public static int CountBits(int n)
{
    int count = 0;
    while (n != 0)
    {
        count++;
        n &= (n - 1);
    }
    return count;
}


public static IEnumerable<IEnumerable<T>> PowerSet<T>(
    IEnumerable<T> src, 
    int minSetSize = 0, 
    int maxSetSize = int.MaxValue)
{
    // we want fast random access to the source, so we'll
    // need to ToArray() it
    var cached = src.ToArray();
    var setSize = Math.Pow(2, cached.Length);
    for(int i=0; i < setSize; i++)
    {
        var subSetSize = CountBits(i);
        if(subSetSize < minSetSize || 
           subSetSize > maxSetSize)
        {
            continue;
        }
        T[] set = new T[subSetSize];

        var temp = i;
        var srcIdx = 0;
        var dstIdx = 0;
        while(temp > 0)
        {
            if((temp & 0x01) == 1)
            {
                set[dstIdx++] = cached[srcIdx];
            }
            temp >>= 1;
            srcIdx++;            
        }
        yield return set;
    }
    yield break;
}

和快速测试台:

void Main()
{
    var src = "ABCDEF";
    var combos = PowerSet(src, 3, 6);

    // hairy joins for great prettiness
    Console.WriteLine(
        string.Join(" , ", 
            combos.Select(subset => 
                string.Concat("[", 
                    string.Join(",", subset) , "]")))
    );
}

输出:

[A,B,C] , [A,B,D] , [A,C,D] , [B,C,D] , [A,B,C,D] , [A,B,E] , [A,C,E] , [B,C,E] , [A,B,C,E] , 
[A,D,E] , [B,D,E] , [A,B,D,E] , [C,D,E] , [A,C,D,E] , [B,C,D,E] , [A,B,C,D,E] , [A,B,F] , 
[A,C,F] , [B,C,F] , [A,B,C,F] , [A,D,F] , [B,D,F] , [A,B,D,F] , [C,D,F] , [A,C,D,F] , 
[B,C,D,F] , [A,B,C,D,F] , [A,E,F] , [B,E,F] , [A,B,E,F] , [C,E,F] , [A,C,E,F] , [B,C,E,F] , 
[A,B,C,E,F] , [D,E,F] , [A,D,E,F] , [B,D,E,F] , [A,B,D,E,F] , [C,D,E,F] , [A,C,D,E,F] , 
[B,C,D,E,F] , [A,B,C,D,E,F]