Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,45 @@
import std.stdio, std.ascii, std.algorithm, core.memory, permutations2;
/** Uses greedy algorithm of adding another char (or two, or three, ...)
until an unseen perm is formed in the last n chars. */
string superpermutation(in uint n) nothrow
in {
assert(n > 0 && n < uppercase.length);
} out(result) {
// It's a superpermutation.
assert(uppercase[0 .. n].dup.permutations.all!(p => result.canFind(p)));
} body {
string result = uppercase[0 .. n];
bool[const char[]] toFind;
GC.disable;
foreach (const perm; result.dup.permutations)
toFind[perm] = true;
GC.enable;
toFind.remove(result);
auto trialPerm = new char[n];
auto auxAdd = new char[n];
while (toFind.length) {
MIDDLE: foreach (immutable skip; 1 .. n) {
auxAdd[0 .. skip] = result[$ - n .. $ - n + skip];
foreach (const trialAdd; auxAdd[0 .. skip].permutations!false) {
trialPerm[0 .. n - skip] = result[$ + skip - n .. $];
trialPerm[n - skip .. $] = trialAdd[];
if (trialPerm in toFind) {
result ~= trialAdd;
toFind.remove(trialPerm);
break MIDDLE;
}
}
}
}
return result;
}
void main() {
foreach (immutable n; 1 .. 8)
n.superpermutation.length.writeln;
}

View file

@ -0,0 +1,50 @@
import std.stdio, std.range, std.algorithm, std.ascii;
enum uint nMax = 12;
__gshared char[] superperm;
__gshared uint pos;
__gshared uint[nMax] count;
/// factSum(n) = 1! + 2! + ... + n!
uint factSum(in uint n) pure nothrow @nogc @safe {
return iota(1, n + 1).map!(m => reduce!q{ a * b }(1u, iota(1, m + 1))).sum;
}
bool r(in uint n) nothrow @nogc {
if (!n)
return false;
immutable c = superperm[pos - n];
if (!--count[n]) {
count[n] = n;
if (!r(n - 1))
return false;
}
superperm[pos++] = c;
return true;
}
void superPerm(in uint n) nothrow {
static immutable chars = digits ~ uppercase;
static assert(chars.length >= nMax);
pos = n;
superperm.length = factSum(n);
foreach (immutable i; 0 .. n + 1)
count[i] = i;
foreach (immutable i; 1 .. n + 1)
superperm[i - 1] = chars[i];
while (r(n)) {}
}
void main() {
foreach (immutable n; 0 .. nMax) {
superPerm(n);
writef("superPerm(%2d) len = %d", n, superperm.length);
// Use -version=doPrint to see the string itself.
version (doPrint) write(": ", superperm);
writeln;
}
}