To merge two arrays into the lexicographically largest result:
At each step, compare the remaining suffixes. Pick from whichever array has the larger suffix.
function merge(a, b)
result := []
i := 0
j := 0
while i < len(a) or j < len(b)
if suffix a[i:] > suffix b[j:] then
result.append(a[i])
i := i + 1
else
result.append(b[j])
j := j + 1
return result
Compare suffixes, not just current elements. beats even though both start with .