0

単語の組み合わせを生成したいと思います。たとえば、次のリストがある場合:{cat、dog、horse、ape、hen、mouse}結果は、n(n-1)/ 2 cat dog horse ape hen mouse(cat dog)(dog horse)(馬の猿)(猿の鶏)(鶏のマウス)(猫の犬の馬)(犬の馬の猿)(馬の猿の鶏)など

これが理にかなっていることを願っています...私が見つけたものはすべて順列を含んでいます

私が持っているリストは500の長さになるでしょう

4

2 に答える 2

3

これを試して!:

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

最初の関数は、2番目の関数を呼び出す方法を示すものにすぎません。500リストを取得する方法によって異なります。動物の名前にコピーして貼り付けるか、単語を含むファイルを読み込むことができます。1行に収まらない場合は、次のことを試すことができます。

    Dim myAnimals As New StringBulder 
    myAnimals.Append("dog cat ... animal49 animal50") 
    myAnimals.Append(" ") 
    myAnimals.Append("animal51 ... animal99") 
    myAnimals.Append(" ") 
    myAnimals.Append("animal100 ... animal150") 

それから

Dim myAnimalCombinations As String() = BuildCombinations(myAnimals.ToString)
于 2010-04-12T04:43:04.473 に答える
1

リストがarr={猫、犬、馬、類人猿、鶏、マウス}であるとすると、次のことができます。

for i = 0; i < arr.size; i++)
  for j = i; j < arr.size; j++)
    print i,j;

アイデアは基本的にです-各アイテムについて、リスト上の他のすべてのアイテムとペアにします。ただし、重複(1、2、2、1など)を回避するために、毎回最初から内部ループを開始するのではなく、外部ループの現在のインデックスから開始します。

于 2010-04-12T04:38:44.550 に答える