2012-01-02 4 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

질문 : 숙제? 제안 : Google "vb.net permutation": – paulsm4

+0

글자 수 (예 : 32, 64)에 제한이있을 때 모든 조합을 만드는 매우 효율적인 방법이 있습니다. – dasblinkenlight

+0

@ paulsm4이 combinatorics 유형이 어떻게 호출되는지 모르겠습니다. 그것은 보통의 순열 또는 조합 유형이 아닙니다. – Cobold

답변

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

+0

Firefox 및/또는 javascript는 10 이상을 깨뜨린다. 9864100 Permutations (반복되는 문자 및 가변 단어 길이 제외) – shaun5

1

Backtracking이 작업을 수행하는 좋은 방법입니다. 기본적으로, 다양한 입력에 대한 그래프를 작성하면서, 사용 가능한 다음 항목을 추가 한 다음, 그것을 모두 백업하고 제거하고 다른 항목으로 대체 할 때까지 그래프를 작성합니다. 여기에 another explanation이 있습니다.