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

Q:宿題?提案:Google "vb.net permutation" :) – paulsm4

+0

文字数(例えば、32または64)に制限がある場合は、すべての組み合わせを作成する非常に効率的な方法があります。 – dasblinkenlight

+0

@ paulsm4私はこのコンビナトリアルタイプがどのように呼び出されるのか分かりません。それは通常の並べ替えや組み合わせの種類ではありません。 – 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 ... 10文字9864100 Permutations(文字を繰り返したり、可変長ではありません) – shaun5

1

Backtrackingこれを行うには良い方法です。基本的には、さまざまな入力のグラフを作成し、使い果たされるまで次の利用可能なものを追加してから、バックアップして削除し、別のものに置き換えます。ここにはanother explanationがあります。

関連する問題