Complete the code to declare the 2D array for dynamic programming.
int dp[[1]][100];
The dp array size should be one more than the string length to include the empty substring case.
Complete the code to initialize the first row of dp with zeros.
for (int j = 0; j <= m; j++) dp[0][j] = [1];
The first row represents empty prefix of s1, so LCS length is zero for all prefixes of s2.
Fix the error in the condition to check if characters match.
if (s1[i - 1] [1] s2[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1;
We add 1 to dp[i-1][j-1] only if characters at current positions are equal.
Fill in the blank to choose the maximum LCS length from previous states.
else dp[i][j] = (dp[i - 1][j] [1] dp[i][j - 1]) ? dp[i - 1][j] : dp[i][j - 1];
We take the maximum of dp[i-1][j] and dp[i][j-1] to find the longest subsequence so far.
Fill all three blanks to complete the loop for filling dp table.
for (int i = 1; i [1] n; i++) { for (int j = 1; j [2] m; j++) { if (s1[i - 1] == s2[j - 1]) dp[i][j] = dp[i - 1][j - 1] + [3]; else dp[i][j] = (dp[i - 1][j] > dp[i][j - 1]) ? dp[i - 1][j] : dp[i][j - 1]; } }
The loops run from 1 to length inclusive, and we add 1 when characters match.