Calculate both the length and LCS using DP.