生成组合,通过组合相邻字符。

huangapple go评论65阅读模式
英文:

Generate combinations by combining adjacent characters

问题

我有一个程序,应该生成所有可能的相邻字符连接的组合。

例如:

输入 = [a, b, c]
输出 = [a, b, c], [ab, c], [a, bc]

输入 = [a, b, c, d]
输出 = [a, b, c, d], [ab, c, d], [a, bc, d], [a, b, cd], [ab, cd]

输入 = [a, b, c, d, e]
输出 = [a, b, c, d, e], [ab, c, d, e], [a, bc, d, e], [a, b, cd, e], [a, b, c, de], [ab, cd, e], [a, bc, de], **[ab, c, de]**
我的程序输出缺少最后一个

基本上,我们只允许组合两个相邻字符。

我写了下面的程序。

public class Program
{
    public static void Main(string[] args)
    {
        List<string> cases = new List<string> {"a b c", "a b c d", "a b c d e"};
        for (int c = 0; c < cases.Count; c++)
        {
            var result = F(cases[c]);
            Console.WriteLine(cases[c]);
            result.ForEach(Console.WriteLine);
            Console.WriteLine("---------------------------");
        }
    }

    public static List<string> F(string searchTerm)
    {
        List<string> result = new List<string>();
        var terms = searchTerm.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList();
        if (terms.Count == 1)
            return new List<string> { searchTerm };

        for (int x = 1; x <= 2; x++)
        {
            for (int i = 0; i < terms.Count - 1; i++)
            {
                if (x == 1)
                {
                    int j = i;
                    var joinedWord = terms[j] + terms[j + 1];
                    result.Add(searchTerm.Replace($"{terms[j]} {terms[j + 1]}", joinedWord));
                }

                if (x == 2)
                {
                    int j = i;
                    if (j + 3 < terms.Count)
                    {
                        var firstJoinedWord = terms[j] + terms[j + 1];
                        var secondJoinedWord = terms[j + 2] + terms[j + 3];
                        result.Add(searchTerm.Replace($"{terms[j]} {terms[j + 1]} {terms[j + 2]} {terms[j + 3]}", firstJoinedWord + " " + secondJoinedWord));
                    }
                }
            }
        }

        return result;
    }
}

以下是输出。

生成组合,通过组合相邻字符。

我不知道是否需要使用递归/动态规划来解决这个问题,因为可能会有N种组合。任何帮助将不胜感激。谢谢。

英文:

I have a program that should generate combinations of concatenation of all possible adjacent characters.

For example

Input = [a,b,c]
Output = [a,b,c], [ab,c], [a,bc]

Input = [a,b,c,d]
Output = [a,b,c,d], [ab,c,d], [a,bc,d], [a,b,cd], [ab,cd]

Input = [a,b,c,d,e]
Output = [a,b,c,d,e], [ab,c,d,e], [a,bc,d,e], [a,b,cd,e], [a,b,c,de], [ab,cd,e], [a,bc,de], **[ab,c,de]**
Last one missing from my program output

Basically we are only allowed to combine two adjacent characters.

I have written the program below.

public class Program
{
    public static void Main(string[] args)
    {
        List&lt;string&gt; cases = new List&lt;string&gt; {&quot;a b c&quot;, &quot;a b c d&quot;, &quot;a b c d e&quot;};
        for (int c = 0; c &lt; cases.Count; c++)
        {
            var result = F(cases[c]);
            Console.WriteLine(cases[c]);
            result.ForEach(Console.WriteLine);
            Console.WriteLine(&quot;---------------------------&quot;);
        }
    }

    public static List&lt;string&gt; F(string searchTerm)
    {
        List&lt;string&gt; result = new List&lt;string&gt;();
        var terms = searchTerm.Split(new[] { &#39; &#39; }, StringSplitOptions.RemoveEmptyEntries).ToList();
        if (terms.Count == 1)
            return new List&lt;string&gt; { searchTerm };

        for (int x = 1; x &lt;= 2; x++)
        {
            for (int i = 0; i &lt; terms.Count - 1; i++)
            {
                if (x == 1)
                {
                    int j = i;
                    var joinedWord = terms[j] + terms[j + 1];
                    result.Add(searchTerm.Replace($&quot;{terms[j]} {terms[j + 1]}&quot;, joinedWord));
                }

                if (x == 2)
                {
                    int j = i;
                    if (j + 3 &lt; terms.Count)
                    {
                        var firstJoinedWord = terms[j] + terms[j + 1];
                        var secondJoinedWord = terms[j + 2] + terms[j + 3];
                        result.Add(searchTerm.Replace($&quot;{terms[j]} {terms[j + 1]} {terms[j + 2]} {terms[j + 3]}&quot;, firstJoinedWord + &quot; &quot; + secondJoinedWord));
                    }
                }
            }
        }
        

        return result;
    }
}

And here is the output.

生成组合,通过组合相邻字符。

I don't know if we need to use Recursion/Dynamic Programming to solve this? because there can be N number of combinations. Any help will be appreciated. Thanks.

答案1

得分: 1

以下是翻译好的代码部分:

arr = ["a", "b", "c", "d", "e"]

possibilities = []

def generate_combinations(combination, index):
    global arr
    if index == len(arr):
        possibilities.append(list(combination))
    else:
        # Either we can concatenate with our adjacent
        if index < len(arr) - 1:
            generate_combinations(combination + [arr[index] + arr[index+1]], index+2)
        # Or we don't
        generate_combinations(combination + [arr[index]], index+1)

generate_combinations([], 0)

for p in possibilities:
    print(p)

输出的测试案例结果:

['ab', 'cd', 'e']
['ab', 'c', 'de']
['ab', 'c', 'd', 'e']
['a', 'bc', 'de']
['a', 'bc', 'd', 'e']
['a', 'b', 'cd', 'e']
['a', 'b', 'c', 'de']
['a', 'b', 'c', 'd', 'e']
[Finished in 40ms]
英文:

Check out this code

arr = [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;, &quot;e&quot;]


possibilities = []

def generate_combinations(combination, index):
    global arr
    if index == len(arr):
        possibilities.append(list(combination))
    else:
        # Either we can concatenate with our adjacent
        if index &lt; len(arr) - 1:
            generate_combinations(combination + [arr[index] + arr[index+1]], index+2)
        # Or we don&#39;t
        generate_combinations(combination + [arr[index]], index+1)

generate_combinations([], 0)


for p in possibilities:
    print(p)

Output for your test case:

[&#39;ab&#39;, &#39;cd&#39;, &#39;e&#39;]
[&#39;ab&#39;, &#39;c&#39;, &#39;de&#39;]
[&#39;ab&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;]
[&#39;a&#39;, &#39;bc&#39;, &#39;de&#39;]
[&#39;a&#39;, &#39;bc&#39;, &#39;d&#39;, &#39;e&#39;]
[&#39;a&#39;, &#39;b&#39;, &#39;cd&#39;, &#39;e&#39;]
[&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;de&#39;]
[&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;, &#39;e&#39;]
[Finished in 40ms]

Let me know if the solution isn't understandable

答案2

得分: 0

以下是您要的中文翻译:

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

class Program
{
    public static IEnumerable<List<string>> Mates(List<string> t)
    {
        if (t.Count == 0)
        {
            yield return new List<string> {};
            yield break;
        }
    
        if (t.Count == 1)
        {
            yield return t;
            yield break;
        }
        
        foreach (var m in Mates(t.GetRange(1, t.Count - 1)))
            yield return new List<string> { t[0] }
                .Concat(m)
                .ToList();
        
        foreach (var m in Mates(t.GetRange(2, t.Count - 2)))
            yield return new List<string> { Mate(t[0], t[1]) }
                .Concat(m)
                .ToList();
    }
    
    public static string Mate(string a, string b)
    {
        return a + b;
    }
    
    public static void Main(string[] args)
    {
        var input = new List<string> { "a", "b", "c", "d", "e" };
        
        foreach (var m in Mates(input))
            Console.WriteLine(string.Join(",", m));
    }
}

希望这个翻译对您有所帮助。

英文:

Here's a quick JavaScript version that you can run in your browser to verify the result -

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function *mates(t) {
  if (t.length == 0) return yield []
  if (t.length == 1) return yield t
  for (const m of mates(t.slice(1)))
    yield [t[0], ...m]
  for (const m of mates(t.slice(2)))
    yield [mate(t[0], t[1]), ...m]
}

function mate(a,b) {
  return a + b
}

for (const m of mates([&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;, &quot;e&quot;]))
  console.log(m.join(&quot;,&quot;))

<!-- language: lang-css -->

.as-console-wrapper { min-height: 100%; top: 0; }

<!-- end snippet -->

a,b,c,d,e
a,b,c,de
a,b,cd,e
a,bc,d,e
a,bc,de
ab,c,d,e
ab,c,de
ab,cd,e

We can easily convert that to a C# program -

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

class Program
{
    public static IEnumerable&lt;List&lt;string&gt;&gt; Mates(List&lt;string&gt; t)
    {
        if (t.Count == 0)
        {
            yield return new List&lt;string&gt; {};
            yield break;
        }
    
        if (t.Count == 1)
        {
            yield return t;
            yield break;
        }
        
        foreach (var m in Mates(t.GetRange(1, t.Count - 1)))
            yield return new List&lt;string&gt; { t[0] }
                .Concat(m)
                .ToList();
        
        foreach (var m in Mates(t.GetRange(2, t.Count - 2)))
            yield return new List&lt;string&gt; { Mate(t[0], t[1]) }
                .Concat(m)
                .ToList();
    }
    
    public static string Mate(string a, string b)
    {
        return a + b;
    }
    
    public static void Main(string[] args)
    {
        var input = new List&lt;string&gt; { &quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;, &quot;e&quot; };
        
        foreach (var m in Mates(input))
            Console.WriteLine(string.Join(&quot;,&quot;, m));
    }
}
a,b,c,d,e
a,b,c,de
a,b,cd,e
a,bc,d,e
a,bc,de
ab,c,d,e
ab,c,de
ab,cd,e

huangapple
  • 本文由 发表于 2023年6月1日 19:45:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76381541.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定