Building the similarity matrix
The longest common subsequence (LCS) problem asks: given two strings, what is the longest sequence of characters that appears in both (not necessarily contiguous)? DP builds a 2D table where entry (i, j) holds the length of the LCS of the first i characters of string A and the first j characters of string B. If characters match, the entry is 1 plus the diagonal entry (from i-1, j-1). If they do not match, it is the maximum of the left or top entry (from i-1 or j-1).
Recovering the actual subsequence
The table alone gives the LCS length. To find the actual characters, you traceback from the bottom-right corner: if characters matched at an entry, include that character and move diagonally; otherwise, move toward the larger neighbor. This reconstructs the sequence. LCS is the foundation of diff algorithms (comparing file versions), version control, and sequence alignment in bioinformatics. The O(m*n) DP solution is practical for strings up to thousands of characters.