演算法›Ch3 動態規劃
第 2 題/共 40 題
◀ AL 2/40
2. Dynamic Programming、LCS
#AL-03-002難Dynamic ProgrammingLCS

Let X[1…m]X[1\dots m] and Y[1…n]Y[1 \dots n] be two sequences. Let L[i][j]L[i][j] denote the length of the longest common subsequence (LCS) of the prefixes X[1…i]X[1 \dots i] and Y[1…j]Y[1 \dots j], and let C[i][j]C[i][j] denote the number of distinct LCSs (distinct sequences, not dynamic programming paths or alignments) of these prefixes. Let C[i][0]=C[0][j]=1C[i][0] = C[0][j] = 1, since the empty sequence is the unique LCS in these cases. Which of the following recurrences correctly computes C[i][j]C[i][j] without double counting identical LCS sequences?

Here, "without double counting" means that each distinct LCS sequence is counted exactly once, even if it can be obtained from multiple dynamic programming subproblems or alignments. In particular, when multiple subproblems yield LCSs of the same optimal length, any LCS sequence that appears in more than one subproblem must be counted only once.

(A) If X[i]=Y[j]X[i]=Y[j]: C[i][j]=C[i−1][j−1]C[i][j]=C[i-1][j-1]. Else if L[i−1][j]>L[i][j−1]L[i-1][j]>L[i][j-1]: C[i][j]=C[i−1][j]C[i][j]=C[i-1][j]. Else if L[i−1][j]<L[i][j−1]L[i-1][j]<L[i][j-1]: C[i][j]=C[i][j−1]C[i][j]=C[i][j-1]. Else: C[i][j]=C[i−1][j]+C[i][j−1]C[i][j]=C[i-1][j]+C[i][j-1].

(B) If X[i]=Y[j]X[i]=Y[j]: C[i][j]=C[i−1][j−1]C[i][j]=C[i-1][j-1]. Else if L[i−1][j]>L[i][j−1]L[i-1][j]>L[i][j-1]: C[i][j]=C[i−1][j]C[i][j]=C[i-1][j]. Else if L[i−1][j]<L[i][j−1]L[i-1][j]<L[i][j-1]: C[i][j]=C[i][j−1]C[i][j]=C[i][j-1]. Else: C[i][j]=C[i−1][j]+C[i][j−1]−C[i−1][j−1]C[i][j]=C[i-1][j]+C[i][j-1]-C[i-1][j-1].

(C) If X[i]=Y[j]X[i]=Y[j]: C[i][j]=C[i−1][j]+C[i][j−1]C[i][j]=C[i-1][j]+C[i][j-1]. Else: C[i][j]=C[i−1][j−1]C[i][j]=C[i-1][j-1].

(D) If X[i]=Y[j]X[i]=Y[j]: C[i][j]=1C[i][j]=1. Else: C[i][j]=C[i−1][j]+C[i−1][j−1]C[i][j]=C[i-1][j]+C[i-1][j-1].

(E) If X[i]=Y[j]X[i]=Y[j]: C[i][j]=C[i−1][j−1]C[i][j]=C[i-1][j-1]. Else: C[i][j]=max⁡{C[i−1][j],C[i][j−1]}C[i][j]=\max\{C[i-1][j], C[i][j-1]\}.

📄 台大115
跳轉到第題
▤完整推導請見《WH 資工筆記 · 演算法》Ch3 動態規劃
本章題號 · 1–20 / 40