Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,13 @@
|
|||
import std.stdio, std.array;
|
||||
|
||||
T[] lcs(T)(in T[] a, in T[] b) pure nothrow @safe {
|
||||
if (a.empty || b.empty) return null;
|
||||
if (a[0] == b[0])
|
||||
return a[0] ~ lcs(a[1 .. $], b[1 .. $]);
|
||||
const longest = (T[] x, T[] y) => x.length > y.length ? x : y;
|
||||
return longest(lcs(a, b[1 .. $]), lcs(a[1 .. $], b));
|
||||
}
|
||||
|
||||
void main() {
|
||||
lcs("thisisatest", "testing123testing").writeln;
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
import std.stdio, std.algorithm, std.traits;
|
||||
|
||||
T[] lcs(T)(in T[] a, in T[] b) pure /*nothrow*/ {
|
||||
auto L = new uint[][](a.length + 1, b.length + 1);
|
||||
|
||||
foreach (immutable i; 0 .. a.length)
|
||||
foreach (immutable j; 0 .. b.length)
|
||||
L[i + 1][j + 1] = (a[i] == b[j]) ? (1 + L[i][j]) :
|
||||
max(L[i + 1][j], L[i][j + 1]);
|
||||
|
||||
Unqual!T[] result;
|
||||
for (auto i = a.length, j = b.length; i > 0 && j > 0; ) {
|
||||
if (a[i - 1] == b[j - 1]) {
|
||||
result ~= a[i - 1];
|
||||
i--;
|
||||
j--;
|
||||
} else
|
||||
if (L[i][j - 1] < L[i - 1][j])
|
||||
i--;
|
||||
else
|
||||
j--;
|
||||
}
|
||||
|
||||
result.reverse(); // Not nothrow.
|
||||
return result;
|
||||
}
|
||||
|
||||
void main() {
|
||||
lcs("thisisatest", "testing123testing").writeln;
|
||||
}
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
import std.stdio, std.algorithm, std.range, std.array, std.string, std.typecons;
|
||||
|
||||
uint[] lensLCS(R)(R xs, R ys) pure nothrow @safe {
|
||||
auto prev = new typeof(return)(1 + ys.length);
|
||||
auto curr = new typeof(return)(1 + ys.length);
|
||||
|
||||
foreach (immutable x; xs) {
|
||||
swap(curr, prev);
|
||||
size_t i = 0;
|
||||
foreach (immutable y; ys) {
|
||||
curr[i + 1] = (x == y) ? prev[i] + 1 : max(curr[i], prev[i + 1]);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
return curr;
|
||||
}
|
||||
|
||||
void calculateLCS(T)(in T[] xs, in T[] ys, bool[] xs_in_lcs,
|
||||
in size_t idx=0) pure nothrow @safe {
|
||||
immutable nx = xs.length;
|
||||
immutable ny = ys.length;
|
||||
|
||||
if (nx == 0)
|
||||
return;
|
||||
|
||||
if (nx == 1) {
|
||||
if (ys.canFind(xs[0]))
|
||||
xs_in_lcs[idx] = true;
|
||||
} else {
|
||||
immutable mid = nx / 2;
|
||||
const xb = xs[0.. mid];
|
||||
const xe = xs[mid .. $];
|
||||
immutable ll_b = lensLCS(xb, ys);
|
||||
|
||||
const ll_e = lensLCS(xe.retro, ys.retro); // retro is slow with dmd.
|
||||
|
||||
//immutable k = iota(ny + 1)
|
||||
// .reduce!(max!(j => ll_b[j] + ll_e[ny - j]));
|
||||
immutable k = iota(ny + 1)
|
||||
.minPos!((i, j) => tuple(ll_b[i] + ll_e[ny - i]) >
|
||||
tuple(ll_b[j] + ll_e[ny - j]))[0];
|
||||
|
||||
calculateLCS(xb, ys[0 .. k], xs_in_lcs, idx);
|
||||
calculateLCS(xe, ys[k .. $], xs_in_lcs, idx + mid);
|
||||
}
|
||||
}
|
||||
|
||||
const(T)[] lcs(T)(in T[] xs, in T[] ys) pure /*nothrow*/ @safe {
|
||||
auto xs_in_lcs = new bool[xs.length];
|
||||
calculateLCS(xs, ys, xs_in_lcs);
|
||||
return zip(xs, xs_in_lcs).filter!q{ a[1] }.map!q{ a[0] }.array; // Not nothrow.
|
||||
}
|
||||
|
||||
string lcsString(in string s1, in string s2) pure /*nothrow*/ @safe {
|
||||
return lcs(s1.representation, s2.representation).assumeUTF;
|
||||
}
|
||||
|
||||
void main() {
|
||||
lcsString("thisisatest", "testing123testing").writeln;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue