Anonymous user
Last letter-first letter: Difference between revisions
Improved second D version
(Small fix in second D program) |
(Improved second D version) |
||
Line 91:
rufflet trapinch haxorus seaking girafarig gabite exeggcute
emolga audino</pre>
Runtime: about 1.23 seconds, dmd compiler.
This is the same program, with some low-level optimizations:
Line 97:
std.typecons, std.exception;
struct Ref {
Tuple!(string[], int) findLongestChain(in string[] items) {▼
char lastChar,
}
}
}▼
}
▲Tuple!(string[], int) findLongestChain(in string[] items) {
foreach (item; items)
enforce(item.length > 1);
▲ int nSolutions;
▲ Ref[] refs, longestPathRefs;
foreach (ushort i, item; items)
refs ~= Ref(i, item[0], item[$-1]);
▲ /// tally statistics
▲ void search(in size_t currLen) {
▲ if (currLen == longestPathRefs.length) {
▲ nSolutions++;
▲ } else if (currLen > longestPathRefs.length) {
▲ nSolutions = 1;
▲ longestPathRefs = refs[0 .. currLen].dup;
▲ }
▲ // recursive search
▲ const dchar lastChar = refs[currLen - 1].lastChar;
▲ foreach (i; currLen .. refs.length)
▲ if (refs[i].firstChar == lastChar) {
▲ const aux = refs[currLen];
▲ refs[currLen] = refs[i];
▲ refs[i] = aux;
▲ search(currLen + 1);
▲ refs[i] = refs[currLen];
▲ refs[currLen] = aux;
// try each item as possible start
Line 171 ⟶ 172:
writeln(" ", sol_nsol[0][i .. min(i+7, $)].join(" "));
}</lang>
Runtime: about 0.
=={{header|Go}}==
|