Reworking algorithm for longest common subsequence