Let and be two sequences. Let denote the length of the longest common subsequence (LCS) of the prefixes and , and let denote the number of distinct LCSs (distinct sequences, not dynamic programming paths or alignments) of these prefixes. Let , since the empty sequence is the unique LCS in these cases. Which of the following recurrences correctly computes 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 : . Else if : . Else if : . Else: .
(B) If : . Else if : . Else if : . Else: .
(C) If : . Else: .
(D) If : . Else: .
(E) If : . Else: .
參考答案與解析
答案 (B)。
- (A) 錯:兩邊長度相同時直接相加,會把兩邊共有的 LCS 算兩次。
- (C)(D) 錯:把字元相同與不同的情形寫反,或直接寫死成 1。
- (E) 錯:取最大值會漏掉只出現在另一邊的 LCS。
- (B) 用排容原理:當 且 時, 等於兩邊 LCS 集合的聯集大小。同時是兩邊 LCS 的序列,一定是 與 的公共子序列,所以兩邊的交集就是 的 LCS 集合,扣掉 即可避免重複。
- 時,長度為 的 LCS 一定以這個字元結尾,所以 。
補充:(B) 只有在 時才正確。若 , 的 LCS 比較短,兩邊沒有共有的 LCS,不應該扣除。例:、,LCS 長度為 1,相異的 LCS 有 a、b 兩個,但 (B) 算出 1。完整正確的寫法是在長度相同的情形使用
五個選項中只有 (B) 處理了重複計數,出題者預期的答案是 (B)。