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
Merge pull request #3917 from dimagi/lcs-merge

fix exponential recursion in detail screen config