2012-01-02 67 views
0

所有组合我有这样的代码:获取无重复

 Dim combinations As New List(Of String) 

     Dim word As String = "abc" 

     For c1 = 0 To word.Length - 1 
      combinations.Add(word(c1)) 
      For c2 = 0 To word.Length - 1 
       If c2 <> c1 Then 
        combinations.Add(word(c1) & word(c2)) 
        For c3 = 0 To word.Length - 1 
         If c3 <> c2 And c3 <> c1 Then 
          combinations.Add(word(c1) & word(c2) & word(c3)) 
         End If 
        Next 
       End If 
      Next 
     Next 

输出:

a, ab, abc, ac, acb, b, ba, bac, bc, bca, c, ca, cab, cb, cba 

如何使一个函数,它会做同样的事情无限字长?

+3

问题:家庭作业?建议:谷歌“vb.net permutation”:) – paulsm4 2012-01-02 23:51:24

+0

有一个非常有效的方法来做出所有组合的时候,有一个限制(足够高,可以肯定)的字母数,例如32或64. – dasblinkenlight 2012-01-02 23:55:17

+0

@ paulsm4我不知道这个combinatorics类型是如何被调用的。这不是通常的排列或组合类型。 – Cobold 2012-01-02 23:57:56

回答

0

我不是vb.net编码器,但看到这是一个有趣的练习。在这里我的答案在伪代码:

array A = ('a','b','c') //add all the unique letters to an array 
integer COUNT = length of A 
array P = A //initialize P (the final answer) as A 
array L = A //initialize L as A to start 
for j=2 to COUNT { 
    array N =() //new empty array 
    foreach i in L { //loop through all the elements of L 
    foreach m in A { //loop through all the elements of A 
     if (i does not contain m) { 
      push (i + m) into P //push the concatenation of i & m into array P 
      push (i + m) into N //do the same thing for array N the next loop through 
     } 
    } 
    } 
    L = N 
} 
change P to a string or whatever you want the output to be.... 
+0

我继续做了它在JavaScript中:http://jsbin.com/ixugoz – shaun5 2012-01-04 19:51:33

+0

Firefox和/或JavaScript突破10 ... 10个字母有9864100排列(没有重复的字母和可变的字长) – shaun5 2012-01-04 21:00:46

1

Backtracking是一个很好的方法来做到这一点。基本上,您可以随时建立各种输入的图形,添加下一个可用的输入,直到用完为止,然后备份并删除一个,然后替换为另一个。这里的another explanation