Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is an algorithm in Java that enumerates the possible sequences by mapping an integer to the sequence.</p> <pre><code>public class Main { private int[] counts = { 3, 2, 1 }; // 3 Symbols A, 2 Symbols B, 1 Symbol C private int n = sum(counts); public static void main(String[] args) { new Main().enumerate(); } private void enumerate() { int s = size(counts); for (int i = 0; i &lt; s; ++i) { String p = perm(i); System.out.printf("%4d -&gt; %s\n", i, p); } } // calculates the total number of symbols still to be placed private int sum(int[] counts) { int n = 0; for (int i = 0; i &lt; counts.length; i++) { n += counts[i]; } return n; } // calculates the number of different sequences with the symbol configuration in counts private int size(int[] counts) { int res = 1; int num = 0; for (int pos = 0; pos &lt; counts.length; pos++) { for (int den = 1; den &lt;= counts[pos]; den++) { res *= ++num; res /= den; } } return res; } // maps the sequence number to a sequence private String perm(int num) { int[] counts = this.counts.clone(); StringBuilder sb = new StringBuilder(n); for (int i = 0; i &lt; n; ++i) { int p = 0; for (;;) { while (counts[p] == 0) { p++; } counts[p]--; int c = size(counts); if (c &gt; num) { sb.append((char) ('A' + p)); break; } counts[p]++; num -= c; p++; } } return sb.toString(); } } </code></pre> <p>The mapping used by the algorithm is as follows. I use the example given in the question (3 x A, 2 x B, 1 x C) to illustrate it.</p> <p>There are 60 (=6!/3!/2!/1!) possible sequences in total, 30 (=5!/2!/2!/1!) of them have an <code>A</code> at the first place, 20 (=5!/3!/1!/1!) have a <code>B</code> at the first place, and 10 (=5!/3!/2!/0!) have a <code>C</code> at the first place.</p> <p>The numbers 0..29 are mapped to all sequences starting with an <code>A</code>, 30..49 are mapped to the sequences starting with <code>B</code>, and 50..59 are mapped to the sequences starting with <code>C</code>.</p> <p>The same process is repeated for the next place in the sequence, for example if we take the sequences starting with <code>B</code> we have now to map numbers 0 (=30-30) .. 19 (=49-30) to the sequences with configuration (3 x A, 1 x B, 1 x C)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload