J'ai le code suivant pour générer des combinaisons de chaîne pour une petite liste et je voudrais l'adapter pour une grande liste de plus de 300 mots de chaîne. Peut-on suggérer comment modifier ce code ou utiliser une méthode différente.adapter le code de combinaison pour la plus grande liste
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
'
CHANGEMENTS //
Pour wordDistance = 1 Pour 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
Pourquoi posez-vous cette question? Vous rencontrez des problèmes de performances? Ne renvoie-t-il pas les résultats attendus? –
C'est un problème de performance. En fait, il a fonctionné si longtemps que j'ai dû arrêter le code car le temps cible est inférieur à une minute ou même 30 secondes si possible. Donc, par exemple, j'ai eu une liste de 335 mots de chaînes et il a fallu trop longtemps – vbNewbie
En plus de ma réponse ci-dessous, il pourrait aussi y avoir des problèmes avec votre algorithme, donc si mon avis ne vaut pas la peine de le réécrire en pseudo code et le repositionner comme une question agnostique de langue qui pourrait être vu par beaucoup plus de gens (le tag VB.Net ne semble pas être si populaire ici) –