fix exponential recursion in detail screen config

by memoizing `lcsMerge`. Also:

- simplify it to return the merged result rather than an internal data
  structure
- do not keep track of the whole longest submatch, just its length, for
  simplicity

fixes http://manage.dimagi.com/default.asp?125416
https://github.com/dimagi/commcare-hq/pull/3930