我已經編寫了代碼來實現我在字符串排列中找到的算法。我所擁有的是一個單詞列表(最多200個),我需要對列表中的5個級別進行排列。基本上將字符串單詞按五個進行分組並將其排列。我所擁有的前5個詞是否會產生排列並忽略其他的數組列表? 任何想法讚賞。上述排列出錯
Private Function permute(ByVal chunks As ArrayList, ByVal k As Long) As ArrayList
ReDim ItemUsed(k)
pno = 0
Permutate(k, 1)
Return chunks
End Function
Private Shared Sub Permutate(ByVal K As Long, ByVal pLevel As Long)
Dim i As Long, Perm As String
Perm = pString ' Save the current Perm
' for each value currently available
For i = 1 To K
If Not ItemUsed(i) Then
If pLevel = 1 Then
pString = chunks.Item(i)
'pString = inChars(i)
Else
pString = pString & chunks.Item(i)
'pString += inChars(i)
End If
If pLevel = K Then 'got next Perm
pno = pno + 1
SyncLock outfile
outfile.WriteLine(pno & " = " & pString & vbCrLf)
End SyncLock
outfile.Flush()
Exit Sub
End If
' Mark this item unavailable
ItemUsed(i) = True
' gen all Perms at next level
Permutate(K, pLevel + 1)
' Mark this item free again
ItemUsed(i) = False
' Restore the current Perm
pString = Perm
End If
Next
K是=〜5爲字在一次置換的數量,但是當我改變for循環到ArrayList大小我得到索引的誤差超出範圍
你是什麼意思「在5個級別」是什麼意思? – 2010-04-11 20:08:33
爲什麼你使用'SyncLock'?該代碼是否在多線程環境中使用?不建議!什麼是'pString',爲什麼它不是一個參數? – 2010-04-11 20:09:45
如果您可以爲您的功能提供樣本輸入和所需的輸出,它可能會幫助我們弄清楚您要做什麼。 – CoderDennis 2010-08-10 17:23:23