Avoid measuring the shared prefix of the same substrings twice

This improves the algorithm by an order of magnitude with respect to speed,
while making no difference to the result.

Signed-off-by: Morten Hustveit <morten.hustveit@gmail.com>
Add support for counting substrings by document