打印&QUOT所有可能的组合; X - QUOT;量与&QUOT字符; X - QUOT;字符串的长度(强力) [英] Print all the possible combinations of "X" amount of characters with "X" string length (Brute Force)

查看:168
本文介绍了打印&QUOT所有可能的组合; X - QUOT;量与&QUOT字符; X - QUOT;字符串的长度(强力)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个字组合生成器,我的意思是要打印的X与X字符串的长度,

I'm trying to write a word combination generator, I mean to print all the possible combinations of "X" amount of characters with "X" string length,

首先我必须说,我看到了一个问题,在计算器究竟这个问题,这有很多字发电机的答案做exaclty这(对不同的语言),但请不要纪念这个为重复或不要评论我的问题只是参考我联系,因为我已经C $的这个问题CS测试了所有的C#和VBNET $,真的没有工作100%符合预期,看到我需要的组合:

First of all I need to say I saw a question in StackOverFlow about exactly this problem which have a lot of answers of word generators to do exaclty this (on different languages), but please, don't mark this as duplicate or don't comment my question only to reference-me that link 'cause I've tested ALL the C# and VBNET codes of that question and really none works 100% as expected, see the combinations I need:

例如,如果我有A,B和C字,我想使这些字符的所有组合中的字符串3的长度则是这种结果,我预计:

For example, If I have "a","b" and "c" characters, and I want to make all combinations of those characters in a string of "3" length then this is the result I expect:

' 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

(排序并不重要,以后我可以排序。)

(The sort don't matter, I can sort it later.)

......但到目前为止,那预期的结果这是我能得到:

...But so far of that expected result this is what I could get:

'a
'aa
'aaa
'b
'bb
'bbb
'c
'cc
'ccc

我在两种语言(Ruby和批次)做到了这一点很久以前,但使用嵌套福尔(很多福尔的每一起为追加只有一个字母到另一个用于输出),当然,如果我试图做到这一点在VBNET是避免了很多福尔的起诉,并做一个更好的表现方式。

I have done this time ago in two languages (Ruby and Batch) but using nested Fors (A lot of Fors together each For appending only one letter to the other For output), of course If I'm trying to do this in VBNET is to avoid the suing of a lot of Fors, and to do it in a better performance way.

在code下面你可以看到,而不是写他们的物理磁盘I如何尝试分配所有组合在RAM存储器(阵列),所以我想用一个逻辑写这篇code方法和更好的性能的方法,那么我要保存所有组合中的记忆第一次和多数民众赞成的原因也就是为什么我不希望使用大量维权的(表现)。

In the code below you can see how I try to allocate all the combinations at RAM memory (an array) instead of writing they to the physical disk, so I want to write this code using a logic method and in a better performance way, then I want to save all the combinations "in the memory" first and thats the reason too why I don't want to use a lot of fors (the performance).

这是我最后一次尝试,一切都在评论-行解释说:

This was my last try, all is explained in comment-lines:

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

我precise帮助。

I precise help.

推荐答案

使用LINQ由 @pengyang 一个美妙的解决方案,适用于IEnumerables:

A wonderful solution using Linq by @pengyang that works with 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

这篇关于打印&QUOT所有可能的组合; X - QUOT;量与&QUOT字符; X - QUOT;字符串的长度(强力)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆