我正在尝试编写一个单词组合生成器,我的意思是用“X”字符串长度打印所有可能的“X”字符组合,
首先,我需要说,我在StackOverFlow中看到了一个关于这个问题的问题,这个问题有许多单词生成器的答案可以做到这一点(在不同的语言上),但是请不要将其标记为重复或者不要评论我的问题只是为了引用我的链接'因为我已经测试了该问题的所有C#和VBNET代码,并且实际上没有按预期工作100%,请参阅我需要的组合:
例如,如果我有“a”,“b”和“c”字符,并且我想将这些字符的所有组合以“3”长度的字符串组成,那么这就是我期望的结果:
' Expected result, 27 combinations:
'
' aaa
' aab
' aac
'
' aba
' abb
' abc
'
' aca
' acb
' acc
'
' baa
' bab
' bac
'
' bba
' bbb
' bbc
'
' bca
' bcb
' bcc
'
' caa
' cab
' cac
'
' cba
' cbb
' cbc
'
' cca
' ccb
' ccc
(排序无关紧要,我可以稍后再分类。)
...但到目前为止,预期的结果是我能得到的:
'a
'aa
'aaa
'b
'bb
'bbb
'c
'cc
'ccc
我这次用两种语言(Ruby和Batch)完成了但是使用嵌套的Fors(每个Fors一起用于将另一个字母附加到另一个用于输出),当然如果我正在尝试这样做在VBNET中是为了避免起诉很多Fors,并以更好的性能方式来做。
在下面的代码中,您可以看到我如何尝试在RAM内存(数组)中分配所有组合,而不是将它们写入物理磁盘,因此我想使用逻辑方法编写此代码并获得更好的性能方式,然后我想首先保存“在内存中”的所有组合,这也是为什么我不想使用很多fors(性能)的原因。
这是我的最后一次尝试,所有内容都在注释行中解释:
Public Class Form1
Dim characters As Char() ' Default value: {"a","b","c"}
Dim StringLength As Int64 ' Default value: 3
Dim TotalCombinations As Int64 ' Default value: 27
Dim strarray(99999999) As String ' Default size: "99.999.999" million of combinations in memory (I need to confirm this from an expert).
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim index As Int64 = 0
For column As Int64 = 0 To TotalCombinations - 1 ' For 0 to 26
For Each character As Char In characters ' Characters = {"a","b","c"}
If column < index Then
index = 0 ' I reset index value 'cause... just experimenting things.
Try
strarray(index) += characters(index)
RichTextBox1.Text += strarray(index) & ControlChars.NewLine
Catch
End Try
Else
Try
strarray(index) += characters(index)
RichTextBox1.Text += strarray(index) & ControlChars.NewLine
Catch
End Try
End If
Next
index += 1
Next
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
characters = sender.text.ToCharArray ' Result: {"a","b","c"}
Calculate_Combinations() ' Result: 27
End Sub
Private Sub NumericUpDown1_ValueChanged(sender As Object, e As EventArgs) Handles NumericUpDown1.ValueChanged
StringLength = sender.value ' Result: 3
Calculate_Combinations() ' Result: 27
End Sub
Private Sub Calculate_Combinations()
Try
TotalCombinations = ((characters.LongLength * StringLength) * StringLength) ' Result: 27
Label1.Text = ((characters.LongLength * StringLength) * StringLength) & " number of combinations." ' Result: 27
Catch : End Try
End Sub
End Class
我精确的帮助。
答案 0 :(得分:2)
使用Linq by @pengyang的一个很棒的解决方案,适用于IEnumerables:
Private Shared Function GetCombinations(Of T)(list As IEnumerable(Of T), length As Integer) As IEnumerable(Of IEnumerable(Of T))
If length = 1 Then
Return list.[Select](Function(x) New T() {x})
End If
Return GetCombinations(list, length - 1).SelectMany(Function(x) list, Function(t1, t2) t1.Concat(New T() {t2}))
End Function
然后:
Dim result = GetCombinations("abc", 3)
LinqPad的屏幕截图:http://i.imgur.com/Xgjg9bz.png
答案 1 :(得分:0)
三个嵌套循环
Dim foo As String = "abc"
For Each c1 As Char In foo
For Each c2 As Char In foo
For Each c3 As Char In foo
Debug.WriteLine(String.Join("", c1, c2, c3))
Next
Next
Next