Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.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#_Algorithm_Recursion_String Concatenation_Yield Return - Fatal编程技术网

C# 如何使用递归以设置的顺序获取字符串的每个组合?

C# 如何使用递归以设置的顺序获取字符串的每个组合?,c#,algorithm,recursion,string-concatenation,yield-return,C#,Algorithm,Recursion,String Concatenation,Yield Return,这个问题与我先前的问题有关,我在这里提出: 我有几个类似的字符串列表,从几十个可能的列表中: 1: { "A", "B", "C" } 2: { "1", "2", "3" } 3: { "D", "E", "F" } 这三个列表仅作为示例,用户可以从几十个具有不同元素数量的类似列表中进行选择。例如,对于用户来说,这也是一个完全有效的选择: 25: { } // empty 4: { "%", "!", "$", "@" } 16: { "I", "a", "b", "Y" } 8: {

这个问题与我先前的问题有关,我在这里提出:

我有几个类似的字符串列表,从几十个可能的列表中:

1: { "A", "B", "C" }
2: { "1", "2", "3" }
3: { "D", "E", "F" }
这三个列表仅作为示例,用户可以从几十个具有不同元素数量的类似列表中进行选择。例如,对于用户来说,这也是一个完全有效的选择:

25: { } // empty
 4: { "%", "!", "$", "@" }
16: { "I", "a", "b", "Y" }
 8: { ")", "z", "!", "8" }
我想做的是在保持列表的“顺序”的同时,获得所有可能的字符串组合。换句话说,假设我们正在查看第一个列表,第一个组合将是
A1D
,然后是
A1E
,然后是
A1F
,然后是
B1D
,然后是
B1E
,等等。根据我之前提出的问题中提供的答案,我编写了这个有效的递归算法:

public void Tester()
{
    var collection = new List { list1, list2, list3 };
    var answer = GetStringCombinations(collection).ToList();

    answer.ForEach(Console.WriteLine);
}

private IEnumerable<string> GetStringCombinations(List<List<string>> collection)
{
    // if the collection is empty, return null to stop the recursion
    if (!collection.Any())
    {
        yield return null;
    }
    // recurse down the list, selecting one letter each time
    else
    {
        foreach (var letter in collection.First())
        {
            // get the collection sans the first item
            var nextCollection = collection.Skip(1).ToList();

            // construct the strings using yield return and recursion
            foreach (var tail in GetStringCombinations(nextCollection))
            {
                yield return letter + tail;
            }
        }
    }
}
public void Tester()
{
var collection=新列表{list1,list2,list3};
var answer=GetStringCombinations(collection.ToList();
应答.ForEach(控制台.写线);
}
私有IEnumerable GetStringCompositions(列表集合)
{
//如果集合为空,则返回null以停止递归
如果(!collection.Any())
{
收益返回空;
}
//沿列表向下递归,每次选择一个字母
其他的
{
foreach(collection.First()中的var字母)
{
//从第一项中获取集合
var nextCollection=collection.Skip(1.ToList();
//使用yield-return和递归构造字符串
foreach(getStringCompositions中的var尾部(下一个集合))
{
收益返回信+尾;
}
}
}
}

但是,此代码取决于
收益率
才能正常工作。如果不使用
yield return
关键字,您将如何实现此算法?例如,如果我要将代码移植到ColdFusion或Ruby(假设它们也没有类似的关键字)?

我没有对其进行测试,但应该可以工作

private List<string> GetStringCombinations(List<List<string>> collection)
{
List<string> ls = new List<string>();

// if the collection is empty, return null to stop the recursion
if (!collection.Any())
{
    return null;
}
// recurse down the list, selecting one letter each time
else
{
    foreach (var letter in collection.First())
    {
        // get the collection sans the first item
        var nextCollection = collection.Skip(1).ToList();

        // construct the strings using yield return and recursion
        foreach (var tail in GetStringCombinations(nextCollection))
        {
            ls.add(letter + tail);
        }
    }
}
return ls;
private List getstringcompositions(列表集合)
{
列表ls=新列表();
//如果集合为空,则返回null以停止递归
如果(!collection.Any())
{
返回null;
}
//沿列表向下递归,每次选择一个字母
其他的
{
foreach(collection.First()中的var字母)
{
//从第一项中获取集合
var nextCollection=collection.Skip(1.ToList();
//使用yield-return和递归构造字符串
foreach(getStringCompositions中的var尾部(下一个集合))
{
ls.添加(字母+尾部);
}
}
}
返回ls;

}

我没有测试过它,但应该可以工作

private List<string> GetStringCombinations(List<List<string>> collection)
{
List<string> ls = new List<string>();

// if the collection is empty, return null to stop the recursion
if (!collection.Any())
{
    return null;
}
// recurse down the list, selecting one letter each time
else
{
    foreach (var letter in collection.First())
    {
        // get the collection sans the first item
        var nextCollection = collection.Skip(1).ToList();

        // construct the strings using yield return and recursion
        foreach (var tail in GetStringCombinations(nextCollection))
        {
            ls.add(letter + tail);
        }
    }
}
return ls;
private List getstringcompositions(列表集合)
{
列表ls=新列表();
//如果集合为空,则返回null以停止递归
如果(!collection.Any())
{
返回null;
}
//沿列表向下递归,每次选择一个字母
其他的
{
foreach(collection.First()中的var字母)
{
//从第一项中获取集合
var nextCollection=collection.Skip(1.ToList();
//使用yield-return和递归构造字符串
foreach(getStringCompositions中的var尾部(下一个集合))
{
ls.添加(字母+尾部);
}
}
}
返回ls;
}

伪代码:

  Combinations(lists[1..n], start, prefix)
   1. If start = n + 1 then print(prefix)
   2. else then
   3.    for i = 1 to lists[start].length do
   4.       Combinations(lists, start + 1, 
                prefix.append(lists[start][i])
这样的办法应该行得通。为了获得最佳结果,请使用start=最低的数组索引和prefix=空字符串调用上述函数。通过一些调整,这将很好地工作

伪代码:

  Combinations(lists[1..n], start, prefix)
   1. If start = n + 1 then print(prefix)
   2. else then
   3.    for i = 1 to lists[start].length do
   4.       Combinations(lists, start + 1, 
                prefix.append(lists[start][i])

这样的办法应该行得通。为了获得最佳结果,请使用start=最低的数组索引和prefix=空字符串调用上述函数。通过一些调整,这将很好地工作

****拍额头****如此简单和明显,但我想不起来。谢谢大家!****拍额头****如此简单和明显,但我想不起来。非常感谢。