modified lcs naive memoisation implementation