RosettaCodeData/Task/Longest-common-subsequence/D/longest-common-subsequence-1.d

14 lines
391 B
D
Raw Permalink Normal View History

2013-06-05 21:47:54 +00:00
import std.stdio, std.array;
2013-04-10 21:29:02 -07:00
2015-02-20 00:35:01 -05:00
T[] lcs(T)(in T[] a, in T[] b) pure nothrow @safe {
2013-06-05 21:47:54 +00:00
if (a.empty || b.empty) return null;
2013-04-10 21:29:02 -07:00
if (a[0] == b[0])
return a[0] ~ lcs(a[1 .. $], b[1 .. $]);
2013-06-05 21:47:54 +00:00
const longest = (T[] x, T[] y) => x.length > y.length ? x : y;
return longest(lcs(a, b[1 .. $]), lcs(a[1 .. $], b));
2013-04-10 21:29:02 -07:00
}
void main() {
2013-06-05 21:47:54 +00:00
lcs("thisisatest", "testing123testing").writeln;
2013-04-10 21:29:02 -07:00
}