使组合代码适用于更大的列表

时间:2010-04-12 14:49:57

标签: 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(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

2 个答案:

答案 0 :(得分:1)

您的代码中一个显而易见的事情是使用ReDim Preserve。这可能是一个非常缓慢的操作,因为我认为每次更改大小时它都会将整个数组复制到一个新数组中,并且由于你在内部循环中这样做,我认为这可能是一个重要的问题。

最简单的修复方法是停止使用这些类型的数组,而是使用List和它的Add方法。

答案 1 :(得分:1)

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

  • 给出一个字符串列表,
  • 返回列表中n项的所有可能组合
  • 其中n = 2到列表长度

例如,在5个字符串的列表中,您需要2个字符串,3个字符串,4个字符串和5个字符串的所有组合。

如果这是对您的问题的准确陈述,则需要指出一个明显的问题。您将生成的项目数量为2 ^(列表长度)。这意味着无论如何,尝试生成300个项目的所有组合永远不会很快。此外,对于除了最小的列表之外的任何列表,您将需要懒惰地生成项目,否则您将耗尽内存。

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