← 回題庫

卷別

資料結構與演算法

選擇

國立臺灣大學 2026 · 考試 · 第 22 題

考科演算法

(4 points) 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−1]+C[i][j−1]C[i][j]=C[i-1][j-1]+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][j−1]C[i][j]=C[i-1][j]+C[i][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\big(C[i-1][j],\,C[i][j-1]\big).

標準解析

AI 已複查