string combinations
I would like to generate a combination of words. For example if I had the following list: {cat, dog, horse, ape, h开发者_如何学编程en, mouse} then the result would be n(n-1)/2 cat dog horse ape hen mouse (cat dog) (dog horse) (horse ape) (ape hen) (hen mouse) (cat dog horse) (dog horse ape) (horse ape hen) etc
Hope this makes sense...everything I found involves permutations
The list I have would be a 500 long
Try this! :
Public Sub test()
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
End Sub
Public 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
The first function is just something that shows you how to call the second function. It really depends on how you get your 500 list - you can copy and paste it over the animal names, or you can load a file with the words in it. If it doesn't fit on one line you can try:
Dim myAnimals As New StringBulder
myAnimals.Append("dog cat ... animal49 animal50")
myAnimals.Append(" ")
myAnimals.Append("animal51 ... animal99")
myAnimals.Append(" ")
myAnimals.Append("animal100 ... animal150")
etc.
then
Dim myAnimalCombinations As String() = BuildCombinations(myAnimals.ToString)
Say your list is arr = {cat, dog, horse, ape, hen, mouse} Then you can do:
for i = 0; i < arr.size; i++)
for j = i; j < arr.size; j++)
print i,j;
The idea is basically - for each item, pair it with every other item on the list. However, to avoid the duplication (e.g. 1,2 and 2,1) you don't start the internal loop from the beginning every time but from your current index of the outer loop.
精彩评论