At position , compare A[i-1] and B[j-1]. If they match: . Found a common character, extend by 1. If they don't match: . Skip a character from A or B, keep the better result. The final answer is where and are the lengths.
This formula tells you how to compute dp[i][j] from earlier dp values. Getting this right is the core of DP (dynamic programming).