Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.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
Vb.net 调整组合代码以适应更大的列表_Vb.net_String_Combinations - Fatal编程技术网

Vb.net 调整组合代码以适应更大的列表

Vb.net 调整组合代码以适应更大的列表,vb.net,string,combinations,Vb.net,String,Combinations,我有下面的代码来为一个小列表生成字符串组合,并希望将其用于一个包含300多个字符串单词的大列表。有人能建议如何修改此代码或使用其他方法吗 Public Class combinations Public Shared Sub main() Dim myAnimals As String = "cat dog horse ape hen mouse" Dim myAnimalCombinations As String() = BuildCombinations(myAn

我有下面的代码来为一个小列表生成字符串组合,并希望将其用于一个包含300多个字符串单词的大列表。有人能建议如何修改此代码或使用其他方法吗

Public Class combinations



Public Shared Sub main()

    Dim myAnimals As String = "cat dog horse ape hen mouse"

    Dim myAnimalCombinations As String() = BuildCombinations(myAnimals)

    For Each combination As String In myAnimalCombinations
        ''//Look on the Output Tab for the results! 
        Console.WriteLine("(" & combination & ")")
    Next combination

    Console.ReadLine()

End Sub



Public Shared Function BuildCombinations(ByVal inputString As String) As String()

    ''//Separate the sentence into useable words. 
    Dim wordsArray As String() = inputString.Split(" ".ToCharArray)

    ''//A plase to store the results as we build them 
    Dim returnArray() As String = New String() {""}

    ''//The 'combination level' that we're up to 
    Dim wordDistance As Integer = 1

    ''//Go through all the combination levels... 
    For wordDistance = 1 To wordsArray.GetUpperBound(0)

        ''//Go through all the words at this combination level... 
        For wordIndex As Integer = 0 To wordsArray.GetUpperBound(0) - wordDistance

            ''//Get the first word of this combination level 
            Dim combination As New System.Text.StringBuilder(wordsArray(wordIndex))

            ''//And all all the remaining words a this combination level 
            For combinationIndex As Integer = 1 To wordDistance

                combination.Append(" " & wordsArray(wordIndex + combinationIndex))

            Next combinationIndex

            ''//Add this combination to the results 
            returnArray(returnArray.GetUpperBound(0)) = combination.ToString

            ''//Add a new row to the results, ready for the next combination 
            ReDim Preserve returnArray(returnArray.GetUpperBound(0) + 1)

        Next wordIndex

    Next wordDistance

    ''//Get rid of the last, blank row. 
    ReDim Preserve returnArray(returnArray.GetUpperBound(0) - 1)

    ''//Return combinations to the calling method. 
    Return returnArray

End Function

End Class
"

变化//

对于wordDistance=1到inputList.Count.ToString/2

        Dim count = inputList.Count.ToString

        'Go through all the words at this combination level... 
        For wordIndex As Integer = 0 To inputList.Count.ToString - wordDistance

            'Get the first word of this combination level 
            combination.Add(inputList.Item(wordIndex))
            'And all all the remaining words a this combination level 
            For combinationIndex As Integer = 1 To wordDistance
                combination.Add(" " & inputList.Item(wordIndex + combinationIndex))
            Next combinationIndex

            'Add this combination to the results 

            If Not wordsList.Contains(combination) Then
                wordsList.Add(combination.ToString)
            End If

            'Add a new row to the results, ready for the next combination 
            'ReDim Preserve returnArray(returnArray.GetUpperBound(0) + 1)

        Next wordIndex

    Next wordDistance

代码中一个明显的特点是使用了ReDim Preserve。这可能是一个相当慢的操作,因为我认为每次改变大小时,它都会将整个数组复制到一个新数组中,而且由于您在循环中执行此操作,我认为这可能是一个重要的问题


解决这个问题的最简单方法是停止使用这些类型的数组,而是将List与Add方法结合使用。

我想确保我了解您首先要做的事情。你的问题似乎是:

  • 给定一个字符串列表
  • 返回列表中n个项目的所有可能组合
  • 其中n=2表示列表的长度
例如,在5个字符串的列表中,您可能需要2个字符串、3个字符串、4个字符串和5个字符串的所有组合

如果这是对你问题的准确陈述,那么有一个突出的问题需要指出。您将生成的项目数为2^(列表长度)。这意味着,无论发生什么情况,尝试生成300个项目的所有组合都不会很快。此外,除了最微小的列表之外,任何列表都需要惰性地生成项,否则将耗尽内存


如果您不想要所有长度的所有组合,您可能需要澄清您的问题,以便更好地陈述您所期望的目标。

您为什么要问这个问题?您是否遇到性能问题?它不会返回预期的结果吗?这是一个性能问题。事实上,它运行了很长时间,我不得不停止代码,因为目标时间不到一分钟,如果可能的话,甚至不到30秒。例如,我有335个字符串单词的列表,除了下面的答案,它只需要太长时间,您的算法可能也会有问题,所以如果我的建议无助于它可能值得重写为伪代码,并将其重新发布为一个语言无关的问题,这可能会被更多的人看到(VB.Net标记在这里似乎并不流行)感谢您的回复。我删除了redim进程,并使用ArrayList而不是字符串来减少一些时间,但我承认您的观点,即大型列表将花费大量时间。我稍微更改了代码,但仍然遵循上面的算法,它工作正常,但我仍然希望限制组合的大小和数量。对于例如,如果我为猫、狗、母鸡、老鼠的列表生成如下组合列表:{cat、dog、hen、mouse、cat dog、dog hen、hen mouse、cat dog hen、dog hen mouse、cat dog hen mouse}::每个组合中的项目数=每个组合的4个长度=例如dog hen=7,包括空格