115 年 國立中央大學資訊工程學系碩士班《資料結構與演算法》

📄 試題原卷 免費註冊後即可對照原始考卷 PDF免費註冊

第 1 題

  1. Which of the following statements about Quicksort are true?
    (A) In the worst case, Quicksort runs in O(n2)O(n^2) time.
    (B) Choosing the median as the pivot guarantees that Quicksort will run in O(nlog⁡n)O(n\log n) time.
    (C) Standard in-place Quicksort is a stable sorting algorithm.
    (D) The expected running time of randomized Quicksort is O(nlog⁡n)O(n\log n).
    (E) Quicksort can be implemented using only O(log⁡n)O(\log n) extra space on average.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

Quicksort 的效率取決於 pivot 把陣列切得多平均:每次切成兩半時遞迴深度為 log⁡n\log n,最差情況每次只切掉一個元素,深度變成 nn。另外要分清楚「時間」與「額外空間」(遞迴堆疊),以及排序的穩定性(相等鍵值的相對順序是否保留)。

解題方法

用遞迴式判斷時間:

T(n)=T(n−1)+O(n)=O(n2)(最差:每次切成 0 與 n−1)T(n)=T(n-1)+O(n)=O(n^2)\qquad\text{(最差:每次切成 }0\text{ 與 }n-1\text{)} T(n)=2T(n/2)+O(n)=O(nlog⁡n)(每次切在中間)T(n)=2T(n/2)+O(n)=O(n\log n)\qquad\text{(每次切在中間)}

選項分析

  • (A) 正確:已排序或反序的輸入、且固定取首元素為 pivot 時,每次只切掉一個元素,總比較次數約 n2/2n^2/2,為 O(n2)O(n^2)。
  • (B) 正確:若每次都以「真正的中位數」當 pivot(可用 median of medians 在 O(n)O(n) 內找到),兩邊各剩約 n/2n/2,遞迴式為 T(n)=2T(n/2)+O(n)T(n)=2T(n/2)+O(n),最差情況也是 O(nlog⁡n)O(n\log n)。
  • (C) 錯誤:in-place 分割會把元素跨距離交換,相等鍵值的相對順序可能被打亂,所以標準 Quicksort 不穩定。
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 2 題

  1. Which of the following statements about amortized analysis are true?
    (A) Amortized analysis provides an upper bound on the average cost per operation over any sequence of operations, not assuming any probability distribution.
    (B) If the amortized cost of an operation is O(1)O(1), then every individual operation must also run in O(1)O(1) time.
    (C) The potential method assigns a potential value to the data structure state, which is used to account for future expensive operations.
    (D) In the accounting method, some operations may be charged more than their actual cost to pay for later operations.
    (E) Amortized analysis and average-case analysis are equivalent concepts.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

攤銷分析(amortized analysis)是對「任意一串操作」求總成本的上界,再平均到每個操作上;它是最差情況下的保證,不涉及機率。三種常見方法:聚合法(aggregate)、記帳法(accounting)、位能法(potential)。

解題方法

以動態陣列(滿了就容量加倍)為例:單次 push 偶爾要搬移 nn 個元素,花 O(n)O(n);但 nn 次 push 的總成本不超過 3n3n,所以攤銷成本為 O(1)O(1)。這個例子同時說明了 (B)、(C)、(D) 的對錯。

選項分析

  • (A) 正確:攤銷成本保證「任何長度為 mm 的操作序列,總成本 ≤\le 攤銷成本總和」,對任意序列都成立,不假設輸入分布。
  • (B) 錯誤:攤銷 O(1)O(1) 只保證平均起來是常數,個別操作仍可能很貴(如動態陣列擴容那一次是 O(n)O(n))。
  • (C) 正確:位能法定義位能函數 Φ(Di)\Phi(D_i),攤銷成本 c^i=ci+Φ(Di)−Φ(Di−1)\hat c_i=c_i+\Phi(D_i)-\Phi(D_{i-1});便宜的操作累積位能,用來支付之後昂貴的操作。
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 3 題

  1. Let dp[i][j]dp[i][j] represent the length of the Longest Common Subsequence of strings X[1…i]X[1\ldots i] and Y[1…j]Y[1\ldots j]. Which of the following statements are correct?
    (A) If X[i]==Y[j]X[i]==Y[j], then dp[i][j]=dp[i−1][j−1]+1dp[i][j]=dp[i-1][j-1]+1.
    (B) If X[i] ! ⁣=Y[j]X[i]\,!\!=Y[j], then dp[i][j]=dp[i−1][j−1]dp[i][j]=dp[i-1][j-1].
    (C) The time complexity of the standard DP solution for LCS is O(n×m)O(n\times m), where mm and nn are the lengths of the two input sequences.
    (D) The DP table can be optimized to use only two rows without affecting correctness.
    (E) LCS can always be solved using a greedy strategy by matching characters as early as possible.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

最長共同子序列(LCS)的標準遞迴關係:

dp[i][j]={0,i=0 或 j=0dp[i−1][j−1]+1,X[i]=Y[j]max⁡(dp[i−1][j], dp[i][j−1]),X[i]≠Y[j]dp[i][j]= \begin{cases} 0, & i=0 \text{ 或 } j=0\\ dp[i-1][j-1]+1, & X[i]=Y[j]\\ \max\bigl(dp[i-1][j],\ dp[i][j-1]\bigr), & X[i]\ne Y[j] \end{cases}

解題方法

表格大小為 (m+1)×(n+1)(m+1)\times(n+1),每格只看左、上、左上三格,常數時間算完。計算第 ii 列時只用到第 ii 列與第 i−1i-1 列。

選項分析

  • (A) 正確:最後一個字元相同時,一定可以把它放進 LCS,長度為去掉兩字元後的 LCS 再加 1。
  • (B) 錯誤:字元不同時應取 max⁡(dp[i−1][j],dp[i][j−1])\max(dp[i-1][j],dp[i][j-1])。只取 dp[i−1][j−1]dp[i-1][j-1] 會漏掉「X[i]X[i] 與 YY 前面某字元配對」的情況。反例:X=ABX=\text{AB}、Y=BY=\text{B} 時正確值為 1,但依 (B) 會得到 dp[1][0]=0dp[1][0]=0。
  • (C) 正確:共 m×nm\times n 格、每格 O(1)O(1),時間為 O(mn)O(mn)。
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 4 題

  1. Which of the following statements about greedy algorithms are true?
    (A) A greedy algorithm is correct only if the problem satisfies both the greedy-choice property and optimal substructure.
    (B) The fractional knapsack problem can be optimally solved using a greedy strategy, whereas the 0/1 knapsack problem cannot.
    (C) Dijkstra's shortest-path algorithm fails to produce correct results if the graph contains edges with negative weights.
    (D) Every problem that can be solved using dynamic programming can also be solved optimally using a greedy algorithm.
    (E) Prim's and Kruskal's algorithms are greedy algorithms for finding a minimum spanning tree.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

貪婪法每一步都做「當下看起來最好」的選擇且不回頭。它能得到最佳解,需要兩個性質:

  1. 貪婪選擇性質(greedy-choice property):存在一個最佳解包含當下的貪婪選擇。
  2. 最佳子結構(optimal substructure):做完選擇後,剩下的子問題的最佳解加上這個選擇,就是原問題的最佳解。

動態規劃只需要最佳子結構(加上重疊子問題),所以適用範圍比貪婪法大。

選項分析

  • (A) 正確:這是教科書(CLRS)對貪婪法正確性的標準說法:兩個性質都要成立,貪婪選擇才能一路推到全域最佳解。
  • (B) 正確:分數背包依「價值/重量」由高到低裝,可證明最佳;0/1 背包不能切割,貪婪會失敗。例:容量 50,物品 (w,v)=(10,60),(20,100),(30,120)(w,v)=(10,60),(20,100),(30,120),依單位價值貪婪得 160,最佳解是後兩者 220。
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 5 題

  1. Which of the following statements are necessarily true?
    (A) If a polynomial-time algorithm exists for the optimization version of an NP-complete problem, then the corresponding decision version is also solvable in polynomial time.
    (B) If a language LL is NP-hard and L∈coNPL\in\text{coNP}, then NP=coNP\text{NP}=\text{coNP}.
    (C) There exists an NP-complete problem whose complement is also NP-complete.
    (D) If problem AA is NP-complete and A≤pBA\le_p B, then BB is NP-complete.
    (E) If an NP-complete problem admits a polynomial-time approximation scheme (PTAS), then P=NP\text{P}=\text{NP}.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

  • NP-hard:所有 NP 問題都能在多項式時間歸約到它;NP-complete=NP-hard 且屬於 NP。
  • 複雜度類別對多項式歸約「向下封閉」:若 A≤pBA\le_p B 且 B∈CB\in\mathcal{C}(C\mathcal{C} 為 P、NP 或 coNP),則 A∈CA\in\mathcal{C}。
  • 題目問「necessarily true」:在目前已知的理論下一定成立才算對,依賴未解猜想(如 NP≠coNP\text{NP}\ne\text{coNP})的敘述不算。

選項分析

  • (A) 正確:決策版問的是「是否存在成本 ≤k\le k 的解」,只要用最佳化演算法求出最佳值再和 kk 比較即可,多一次比較仍是多項式時間。
  • (B) 正確:LL 是 NP-hard,所以任何 A∈NPA\in\text{NP} 都有 A≤pLA\le_p L;又 L∈coNPL\in\text{coNP} 且 coNP 對歸約封閉,得 A∈coNPA\in\text{coNP},即 NP⊆coNP\text{NP}\subseteq\text{coNP}。取補集得 coNP⊆NP\text{coNP}\subseteq\text{NP},故 NP=coNP\text{NP}=\text{coNP}。
  • (C) 錯誤:若某 NP-complete 問題 AA 的補集 Aˉ\bar A 也是 NP-complete,則 Aˉ∈NP\bar A\in\text{NP},即 A∈coNPA\in\text{coNP},由 (B) 的推理得 NP=coNP\text{NP}=\text{coNP}。這是未解問題,所以這個敘述不是「必然成立」。
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 6 題

  1. Let f(n)f(n) and g(n)g(n) be non-negative functions defined for all sufficiently large integer nn. Select one or more correct statements.
    (A) If f(n)=O(g(n))f(n)=O(g(n)), then g(n)=Ω(f(n))g(n)=\Omega(f(n)).
    (B) If f(n)=Θ(g(n))f(n)=\Theta(g(n)), then f(n)=O(g(n))f(n)=O(g(n)).
    (C) n=o(nlog⁡n)n=o(n\log n).
    (D) If f(n)=O(g(n))f(n)=O(g(n)) and g(n)=O(f(n))g(n)=O(f(n)), then f(n)=Θ(g(n))f(n)=\Theta(g(n)).
    (E) n2+200000002n=Θ(n)n^2+20000000^2n=\Theta(n).

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

f=O(g)  ⟺  ∃c>0,n0: f(n)≤c g(n)  ∀n≥n0f=O(g)\iff \exists c>0,n_0:\ f(n)\le c\,g(n)\ \ \forall n\ge n_0 f=Ω(g)  ⟺  g=O(f),f=Θ(g)  ⟺  f=O(g) 且 f=Ω(g)f=\Omega(g)\iff g=O(f),\qquad f=\Theta(g)\iff f=O(g)\text{ 且 }f=\Omega(g)

小寫 oo 表示嚴格較小:f=o(g)  ⟺  lim⁡n→∞f(n)/g(n)=0f=o(g)\iff \lim_{n\to\infty} f(n)/g(n)=0。

選項分析

  • (A) 正確:f(n)≤c g(n)f(n)\le c\,g(n) 等價於 g(n)≥1cf(n)g(n)\ge \frac1c f(n),正是 g=Ω(f)g=\Omega(f) 的定義(轉置對稱性)。
  • (B) 正確:Θ\Theta 同時包含上界與下界,取出上界那一半就是 OO。
  • (C) 正確:lim⁡n→∞nnlog⁡n=lim⁡n→∞1log⁡n=0\displaystyle\lim_{n\to\infty}\frac{n}{n\log n}=\lim_{n\to\infty}\frac{1}{\log n}=0,所以 n=o(nlog⁡n)n=o(n\log n)。
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 7 題

  1. Consider lower bounds for algorithmic problems in the comparison-based model. Select one or more correct statements.
    (A) Any comparison-based algorithm for sorting nn distinct elements has a worst-case lower bound of Ω(nlog⁡n)\Omega(n\log n).
    (B) The Ω(nlog⁡n)\Omega(n\log n) lower bound for sorting applies to all sorting algorithms, including counting sort and radix sort.
    (C) The decision-tree model is commonly used to prove lower bounds for comparison-based sorting.
    (D) The information-theoretic argument for comparison-based sorting relies on the fact that there are n!n! possible input permutations.
    (E) A lower bound of Ω(n)\Omega(n) for a problem implies that no algorithm can solve the problem faster than linear time in the worst case, regardless of the computational model.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

比較式排序可以畫成一棵決策樹:每個內部節點是一次比較,每個葉節點對應一種輸出排列。nn 個相異元素有 n!n! 種排列,所以至少要有 n!n! 片葉子;高度 hh 的二元樹最多 2h2^h 片葉子:

2h≥n! ⇒ h≥log⁡2n!=Θ(nlog⁡n)2^h\ge n!\ \Rightarrow\ h\ge\log_2 n!=\Theta(n\log n)

選項分析

  • (A) 正確:由上式,任何比較式排序的最差比較次數至少是 log⁡2n!=Ω(nlog⁡n)\log_2 n!=\Omega(n\log n)。
  • (B) 錯誤:這個下界只對「只靠比較取得資訊」的演算法成立。counting sort、radix sort 直接用鍵值當索引,不屬於比較模型,在鍵值範圍有限時可做到 O(n)O(n)(如 counting sort 為 O(n+k)O(n+k))。
  • (C) 正確:決策樹模型正是證明比較式排序下界的標準工具。
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 8 題

  1. Consider the single-source or all-pairs shortest path problem on a given graph, where edge weights may be positive, zero, or negative. Select one or more correct statements.
    (A) Dijkstra's shortest path algorithm may fail when negative-weight edges exist.
    (B) Bellman–Ford algorithm can detect the existence of negative-weight cycles.
    (C) If a graph contains a negative-weight cycle reachable from the source, then the shortest path to some nodes is not well-defined.
    (D) Floyd–Warshall algorithm can be used to detect negative-weight cycles.
    (E) The all-pairs shortest path problem can be solved in the O(V×E)O(V\times E) time complexity, where VV is the number of nodes and EE is the number of edges.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

演算法問題允許負邊時間
Dijkstra單源否O((V+E)log⁡V)O((V+E)\log V)
Bellman–Ford單源是,可偵測負環O(VE)O(VE)
Floyd–Warshall全點對是,可偵測負環O(V3)O(V^3)
Johnson全點對是O(VE+V2log⁡V)O(VE+V^2\log V)

選項分析

  • (A) 正確:Dijkstra 一旦把節點標為完成就不再更新,負邊可能讓之後找到更短的路,因此「可能」出錯。
  • (B) 正確:做完 V−1V-1 輪鬆弛後再做一輪,若仍有邊能被鬆弛,就代表存在從源點可達的負環。
  • (C) 正確:繞負環一圈總長就減少,可以無限繞下去,經過該環可到達的節點最短距離為 −∞-\infty,最短路徑沒有定義。
  • (D) 正確:Floyd–Warshall 結束後,若某個 d[i][i]<0d[i][i]<0,代表 ii 在一個負環上。
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 9 題

  1. Consider the branch-and-bound algorithm and the A* algorithm used for solving optimization problems. Select one or more correct statements.
    (A) The branch-and-bound algorithm systematically explores a search tree while using bounds to prune suboptimal branches.
    (B) The A* algorithm can be viewed as a special case of the branch-and-bound algorithm.
    (C) If the heuristic function used in the A* algorithm never overestimates the true remaining cost to reach the goal, then the A* algorithm is guaranteed to find an optimal solution.
    (D) The A* algorithm does not guarantee a lower worst-case time complexity than an exhaustive search of the entire solution space.
    (E) The branch-and-bound algorithm does not guarantee polynomial-time performance in the worst case.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

  • Branch-and-bound:展開解空間樹,對每個節點估計可能達到的最佳值(界限);若界限已經比目前最佳解差,就剪掉整棵子樹。
  • A*:以 f(n)=g(n)+h(n)f(n)=g(n)+h(n) 決定展開順序,gg 是已花成本,hh 是到目標的估計成本。ff 本身就是一個下界,所以 A* 可視為「以最佳優先順序展開」的 branch-and-bound。

選項分析

  • (A) 正確:這正是 branch-and-bound 的定義:系統性地分支,並用界限剪掉不可能更好的分支。
  • (B) 正確:A* 用 g+hg+h 當下界、每次展開下界最小的節點,第一次展開到目標時即為最佳解;這就是 best-first branch-and-bound 的一種特例(演算法教科書常這樣歸類)。
  • (C) 正確:hh 不高估(admissible)時,A* 保證找到最佳解(圖搜尋版若不重新開啟節點,還需要 hh 滿足一致性 consistent;一般教科書的敘述即為本選項)。
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 10 題

  1. Consider the prune-and-search algorithm using the median of medians for finding the kk-th smallest element in an unsorted array AA of nn distinct elements, where 1≤k≤n1\le k\le n, as described below:

Algorithm Select(AA, kk):
Input: An array AA of nn elements and an integer kk, 1≤k≤∣A∣=n1\le k\le |A|=n
Output: The kk-th smallest element in AA
Step 1: If ∣A∣≤5|A|\le 5, then sort array AA (with any method, e.g., insertion sort) and return the element with rank kk (i.e., the kk-th element in the sorted array).
Step 2: Divide the array AA into groups of five elements each (ignore the last group if it has fewer than five elements).
Step 3: Find the median of each group.
Step 4: Recursively compute the median of the found medians, denoted by mm.
Step 5: Partition the original array into the following three sets:
L={x∈A∣x<m}L=\{x\in A\mid x<m\}
E={m}E=\{m\}
R={x∈A∣x>m}R=\{x\in A\mid x>m\}
Step 6: If k=∣L∣+1k=|L|+1 then return mm
Else if k≤∣L∣k\le|L| then return Select(LL, kk)
Else return Select(RR, k−∣L∣−1k-|L|-1).

Select one or more correct statements.
(A) After grouping elements into blocks of five and choosing the median of medians as the pivot denoted mm, at least 3n/103n/10 elements are guaranteed to be less than or equal to the pivot.
(B) The recurrence T(n)=T(n/5)+T(7n/10)+O(n)T(n)=T(n/5)+T(7n/10)+O(n) implies that the algorithm runs in O(nlog⁡n)O(n\log n) time.
(C) Using groups of size 3 instead of 5 still guarantees linear worst-case running time.
(D) The correctness of the pruning step relies on the fact that the pivot is an approximate median of the entire array.
(E) The median-finding problem is a special case of the kk-th smallest selection problem, where k=⌈n/2⌉k=\lceil n/2\rceil, and both can be solved in worst-case linear time.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

Median of medians 選出的 mm 保證「夠接近中位數」:約 n/5n/5 個組中位數裡有一半 ≤m\le m,每個這樣的組又有 3 個元素 ≤\le 該組中位數,所以

∣{x≤m}∣ ≥ 3⋅12⋅n5=3n10|\{x\le m\}|\ \ge\ 3\cdot\frac12\cdot\frac n5=\frac{3n}{10}

(嚴格計算需扣掉常數,CLRS 寫成 3n/10−63n/10-6),對稱地也至少有約 3n/103n/10 個元素 ≥m\ge m。因此遞迴最多處理約 7n/107n/10 個元素:

T(n)≤T(n/5)+T(7n/10)+O(n)T(n)\le T(n/5)+T(7n/10)+O(n)

解題方法

這類遞迴式看子問題比例和:15+710=910<1\frac15+\frac7{10}=\frac9{10}<1,每一層的總工作量以 910\frac9{10} 的等比遞減,總和為 O(n)O(n)。

選項分析

  • (A) 正確:如上推導,至少約 3n/103n/10 個元素 ≤m\le m(這是本演算法的關鍵引理,教科書以 3n/103n/10 表示,精確下界差一個常數)。
  • (B) 錯誤:比例和 9/10<19/10<1,遞迴式解出來是 Θ(n)\Theta(n),演算法是線性時間。本選項把結論說成 O(nlog⁡n)O(n\log n),沒有抓到這個遞迴式真正推出的線性時間結論。
  • (C) 錯誤:組大小改成 3 時,保證只剩約 n/3n/3 個元素被排除,遞迴式變成 T(n)=T(n/3)+T(2n/3)+O(n)T(n)=T(n/3)+T(2n/3)+O(n),比例和恰為 1,解為 Θ(nlog⁡n)\Theta(n\log n),不再是線性。
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 11 題

  1. Given a weighted directed graph G (V, E) below.

🖼️【此處有附圖,請對照原卷】

Which of the following statement(s) about G is (are) true?
(A) ACEGFBD is a possible topological order of the network if G is an activity-on-vertex (AOV) network.
(B) ABECGFD is a possible topological order of the network if G is an AOV network.
(C) Suppose G is an AOE network. If each task takes exactly one day, then ABCDGF is a critical path.
(D) Suppose G is an AOE network. If each activity takes one day except the activity on the directed edge (A, E), which takes three days, then AEGF is a critical path.

🖼️ 本題含圖表,以下為原卷對應頁面:
原卷第 5 頁

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

依原卷附圖,有向邊為:

A→B, A→C, A→E, B→C, B→D, C→D, C→G, D→G, E→F, E→G, G→FA\to B,\ A\to C,\ A\to E,\ B\to C,\ B\to D,\ C\to D,\ C\to G,\ D\to G,\ E\to F,\ E\to G,\ G\to F
  • 拓撲順序(AOV):每條邊 u→vu\to v 都必須 uu 排在 vv 前面。
  • 關鍵路徑(AOE):邊代表活動,從起點到終點最長的路徑就是關鍵路徑,長度等於完工的最短時間;可能有多條。

解題方法

拓撲順序逐條邊檢查是否「反向」。關鍵路徑則列出 AA 到 FF 的所有路徑並計算長度:

路徑邊數(D) 的長度(A→EA\to E 為 3 天)
A E FA\,E\,F24
A E G FA\,E\,G\,F35
A C G FA\,C\,G\,F33
A B C G FA\,B\,C\,G\,F44
A C D G FA\,C\,D\,G\,F44
A B D G FA\,B\,D\,G\,F44
A B C D G FA\,B\,C\,D\,G\,F55

選項分析

  • (A) 錯誤:ACEGFBD 中 C 排在 B 前面,但有邊 B→CB\to C,違反拓撲順序(D 排在 G 之後也違反 D→GD\to G)。
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 12 題

  1. Given an empty 11-bucket hash table with hash function h(k)=k%11h(k)=k\%11. Below is the resulting hash table after inserting 8 items.
BucketValue
0
1
224
357
437
527
616
717
815
938
10

Which of the following statements is/are true?
(A) 27 37 16 17 24 15 57 38 is a possible input sequence when quadratic probing is adopted.
(B) 37 27 16 24 15 57 38 17 is a possible input sequence when quadratic probing is adopted.
(C) 37 27 16 24 57 17 15 38 is a possible input sequence when liner probing is adopted.
(D) 27 16 37 38 57 15 17 24 is a possible input sequence when liner probing is adopted.

🖼️ 本題含圖表,以下為原卷對應頁面:
原卷第 5 頁

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

各鍵值的雜湊位置 h(k)=k mod 11h(k)=k\bmod 11:

kk2457371527163817
h(k)h(k)22445556
  • 線性探測:依序試 h, h+1, h+2,…h,\ h+1,\ h+2,\dots
  • 二次探測:依序試 h, h+12, h+22, h+32,…h,\ h+1^2,\ h+2^2,\ h+3^2,\dots(即 h,h+1,h+4,h+9,…h,h+1,h+4,h+9,\dots,皆  mod 11\bmod 11)

目標表格:2:24, 3:57, 4:37, 5:27, 6:16, 7:17, 8:15, 9:382{:}24,\ 3{:}57,\ 4{:}37,\ 5{:}27,\ 6{:}16,\ 7{:}17,\ 8{:}15,\ 9{:}38。

解題方法

依各選項的順序逐一插入,比對最後結果是否與表格相同。

選項分析

  • (A) 正確(二次探測):27→5;37→4;16:5 滿 → 5+1=65+1=6;17:6 滿 → 6+1=76+1=7;24→2;15:4、5 滿 → 4+4=84+4=8;57:2 滿 → 2+1=32+1=3;38:5、6 滿 → 5+4=95+4=9。結果與表格完全相同。
  • (B) 正確(二次探測):37→4;27→5;16:5 滿 → 6;24→2;15:4、5 滿 → 8;57:2 滿 → 3;38:5、6 滿 → 9;17:6 滿 → 7。結果與表格相同。
  • (C) 正確(線性探測):37→4;27→5;16→6;24→2;57:2 滿 → 3;17:6 滿 → 7;
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 13 題

  1. Given a weighted undirected graph G (V, E) below. Which of the following statement(s) about the minimum spanning tree (MST) of G is (are) true?

🖼️【此處有附圖,請對照原卷】

(A) If the MST is constructed by Kruskal's algorithm, edge (E, G) is the 6-th edge added to the MST.
(B) If the MST is constructed by Kruskal's algorithm, the MST is a binary tree.
(C) If the MST is constructed by Prim's algorithm and starting from vertex C, edge (E, G) is the 5-th edge added to the MST.
(D) If the MST is constructed by Prim's algorithm and starting from vertex D, there is an edge connecting vertex A and vertex B.

🖼️ 本題含圖表,以下為原卷對應頁面:
原卷第 6 頁

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

依原卷附圖,各邊權重為:

(B,C)=5, (D,G)=6, (E,G)=7, (F,G)=8, (A,C)=9, (B,D)=10,(A,B)=11, (A,E)=14, (E,F)=15, (C,D)=16, (C,G)=20\begin{aligned} &(B,C)=5,\ (D,G)=6,\ (E,G)=7,\ (F,G)=8,\ (A,C)=9,\ (B,D)=10,\\ &(A,B)=11,\ (A,E)=14,\ (E,F)=15,\ (C,D)=16,\ (C,G)=20 \end{aligned}

所有權重都相異,所以 MST 唯一;Kruskal 與 Prim 得到同一棵樹,只是加邊順序不同。

解題方法

Kruskal(由小到大,不成環就加入):

  1. (B,C)=5(B,C)=5
  2. (D,G)=6(D,G)=6
  3. (E,G)=7(E,G)=7
  4. (F,G)=8(F,G)=8
  5. (A,C)=9(A,C)=9
  6. (B,D)=10(B,D)=10:連接 {A,B,C}\{A,B,C\} 與 {D,E,F,G}\{D,E,F,G\},7 個點已有 6 條邊,完成。

MST ={BC, DG, EG, FG, AC, BD}=\{BC,\ DG,\ EG,\ FG,\ AC,\ BD\},總權重 45。

Prim 從 C 開始(每次加入連接樹內外的最小邊):

  1. (C,B)=5(C,B)=5
  2. (C,A)=9(C,A)=9
  3. (B,D)=10(B,D)=10
  4. (D,G)=6(D,G)=6
  5. (G,E)=7(G,E)=7
  6. (G,F)=8(G,F)=8

選項分析

  • (A) 錯誤:(E,G)(E,G) 是 Kruskal 加入的第 3 條邊,第 6 條是 (B,D)(B,D)。
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 14 題

  1. Given list [9,7,15,6,11,4,22,119][9, 7, 15, 6, 11, 4, 22, 119]. Which of the following statements is (are) true?
    (A) The resulting sequence of the first phase of Quick Sort when the first element, i.e., 9, is chosen as pivot is 6,7,4,9,11,15,22,1196, 7, 4, 9, 11, 15, 22, 119.
    (B) The resulting sequence of the first phase of Quick Sort when the second element, i.e., 7, is chosen as pivot is 4,6,7,15,11,9,22,1194, 6, 7, 15, 11, 9, 22, 119.
    (C) At the end of the second pass of LSD Radix sort, the first element of the resulting chain is 11.
    (D) At the end of the third pass of LSD Radix sort, the 7-th element of the resulting chain is 22.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

  • Quick Sort 一次分割(Hoare 式,教科書常用版本):pivot 放在最左邊,ii 從左往右找第一個大於 pivot 的元素、jj 從右往左找第一個小於 pivot 的元素,i<ji<j 就交換;兩者交錯時,把 pivot 與 a[j]a[j] 交換。
  • LSD Radix sort:從個位數開始依序以每一位數做穩定分配(bucket 0–9),再依 bucket 順序串回鏈結。

解題方法與選項分析

  • (A) 正確:pivot=9。

    • ii 停在 15、jj 停在 4,交換:9,7,4,6,11,15,22,1199,7,4,6,11,15,22,119
    • ii 停在 11、jj 停在 6,此時 i>ji>j,pivot 與 6 交換:6,7,4,9,11,15,22,1196,7,4,9,11,15,22,119

    與選項相同。

  • (B) 錯誤:pivot=7,先與第一個元素交換:7,9,15,6,11,4,22,1197,9,15,6,11,4,22,119。

    • ii 停在 9、jj 停在 4,交換:7,4,15,6,11,9,22,1197,4,15,6,11,9,22,119
    • ii 停在 15、jj 停在 6,交換:7,4,6,15,11,9,22,1197,4,6,15,11,9,22,119
    • ii 停在 15、jj 停在 6,交錯,pivot 與 6 交換:6,4,7,15,11,9,22,1196,4,7,15,11,9,22,119

    正確結果開頭是 6,46,4,選項寫成 4,64,6,不符。

  • (C) 錯誤:

    • 第 1 趟(個位數):1:[11]、2:[22]、4:[4]、5:[15]、6:[6]、7:[7]、9:[9, 119] →
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 15 題

  1. Consider a red-black tree with level order 52, 33, 80, 100. Which of the following statements is (are) true?
    (A) node 33 is red.
    (B) After inserting 65 and 55, node 100 in the resulting red-black tree is black.
    (C) After inserting 65, 55, and 60, node 60 in the resulting red-black tree is black.
    (D) After inserting 65, 55, and 60, the level order traversal of the resulting red-black tree is 52, 33, 80, 60, 100, 55, 65.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

紅黑樹規則:根為黑、紅節點的子節點必為黑、任一節點到其下所有 NIL 的路徑黑節點數相同。插入新節點塗紅,若父節點也是紅色:

  • 叔節點紅 → 父、叔改黑,祖父改紅,往上繼續檢查。
  • 叔節點黑 → 旋轉(LL/RR 單旋、LR/RL 雙旋)並重新著色。

解題方法

初始樹:52 為根,33 為左子,80 為右子,100 為 80 的右子。

判斷顏色:若 33 為紅,路徑 52→33→NIL52\to33\to\text{NIL} 只有 1 個黑節點,則 80 也必須是紅色,100 就必須是黑色,路徑 52→80→100→NIL52\to80\to100\to\text{NIL} 會有 2 個黑節點,矛盾。所以 33、80 為黑,100 為紅。

插入 65:52→8052\to80 的左子,紅色;父節點 80 為黑,不需調整。

插入 55:成為 65 的左子,紅色。父節點 65 紅、叔節點 100 紅 → 65、100 改黑,80 改紅;80 的父節點 52 為黑,結束。

52(B) → 33(B), 80(R);80→65(B), 100(B);65→55(R)52(\text{B})\ \to\ 33(\text{B}),\ 80(\text{R});\quad 80\to 65(\text{B}),\ 100(\text{B});\quad 65\to 55(\text{R})

插入 60:成為 55 的右子,紅色。父節點 55 紅、叔節點(65 的右子 NIL)黑,屬於 LR 情況:先對 55 左旋、再對 65 右旋,60 成為該子樹的根並塗黑,55、65 塗紅:

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 16 題

  1. Which of the following statements are correct?
    (A) The postfix of (a+b)/(c−d)+e(a+b)/(c-d)+e is ab+cd−/+eab+cd-/+e.
    (B) The postfix of infix expression b+a∗c/db+a*c/d is bac∗d/+bac*d/+.
    (C) The infix of the postfix expression ab+c∗d/ef−+ab+c*d/ef-+ is (((a+b)∗c)/d)+(e−f)(((a+b)*c)/d)+(e-f).
    (D) The prefix of the postfix expression abc+d∗/abc+d*/ is /a∗+bcd/a*+bcd.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

  • 中序轉後序:依優先順序(∗*、// 高於 ++、−-,同級由左至右結合)加上完整括號,再把每個運算子移到對應右括號之後。
  • 後序轉中序/前序:由左往右掃描,遇運算元就推入堆疊;遇運算子就彈出兩個(先彈出的是右運算元),組合後推回。

選項分析

  • (A) 錯誤:完整括號為 (((a+b)/(c−d))+e)\bigl(((a+b)/(c-d))+e\bigr)。(a+b)→ab+(a+b)\to ab+,(c−d)→cd−(c-d)\to cd-,相除得 ab+cd−/ab+cd-/,再加 ee:
ab+cd−/e+ab+cd-/e+

選項寫成 ab+cd−/+eab+cd-/+e,把 ++ 放在 ee 前面,順序錯誤(而且掃到 ++ 時堆疊只有一個運算元,根本不是合法的後序式)。

  • (B) 正確:∗* 與 // 同級、左結合,b+a∗c/d=b+((a∗c)/d)b+a*c/d=b+((a*c)/d)。a∗c→ac∗a*c\to ac*,再除以 d→ac∗d/d\to ac*d/,最後加 bb:bac∗d/+bac*d/+,與選項相同。
  • (C) 正確:堆疊掃描 ab+c∗d/ef−+ab+c*d/ef-+:
    1. a,b,+⇒(a+b)a,b,+\Rightarrow (a+b)
    2. c,∗⇒((a+b)∗c)c,*\Rightarrow ((a+b)*c)
    3. d,/⇒(((a+b)∗c)/d)d,/\Rightarrow (((a+b)*c)/d)
    4. e,f,−⇒(e−f)e,f,-\Rightarrow (e-f)
    5. +⇒(((a+b)∗c)/d)+(e−f)+\Rightarrow (((a+b)*c)/d)+(e-f)
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 17 題

  1. Consider the code fragment below.
container.push('I');
container.pop();
container.push('L');
container.push('O');
container.push('V');
container.pop();
container.push('E');
container.push('N');
container.pop();
container.push('C');
container.push('U');
container.pop();

Which of the following statements is (are) true?
(A) If the "container" is an empty standard stack. The sequence of elements inside the "container" from its top to the bottom is LOEC.
(B) If the "container" is an empty standard queue. The sequence of elements inside the "container" from its front to its rear is ENCU.
(C) If the "container" is an empty min heap, the level order traversal of the heap is NVOU.
(D) If the "container" is an empty max heap, the level order traversal of the heap is NLEC.

🖼️ 本題含圖表,以下為原卷對應頁面:
原卷第 7 頁

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

同一串 push/pop 套在四種容器上:stack(後進先出)、queue(先進先出)、min heap(pop 取最小)、max heap(pop 取最大)。字母大小依字母順序:C<E<I<L<N<O<U<VC<E<I<L<N<O<U<V。Heap 以陣列表示,level order 就是陣列順序;push 放到最後再往上調(sift-up),pop 把最後一個元素移到根再往下調(sift-down)。

選項分析

  • (A) 錯誤(stack):I 進出 → [L, O, V] → pop V → [L, O] → push E、N → pop N → [L, O, E] → push C、U → pop U → [L, O, E, C](由底到頂)。由頂到底是 CEOL;LOEC 是由底到頂的順序。
  • (B) 正確(queue):I 進出 → [L, O, V] → pop L → [O, V] → push E、N → pop O → [V, E, N] → push C、U → pop V → [E, N, C, U]。由前到後是 ENCU。
  • (C) 錯誤(min heap):
    1. push L、O、V:[L, O, V]
    2. pop(取 L):V 移到根,與較小子節點 O 交換 → [O, V]
    3. push E:E 上調與 O 交換 → [E, V, O]
    4. push N:N 與父節點 V 交換 → [E, N, O, V]
    5. pop(取 E):V 移到根,與較小子節點 N 交換 → [N, V, O]
    6. push C:C 與 V、再與 N 交換 → [C, N, O, V]
    7. push U:父節點 N 較小,不動 → [C, N, O, V, U]
    8. pop(取 C):U 移到根,與較小子節點 N 交換;U 的子節點 V 較大,停止 → [N, U, O, V]
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 18 題

  1. Given a binary tree with inorder traversal, ILOVENCU, and postorder traversal, LIVNUCEO. Which of the following statements is (are) true?
    (A) The fifth letter in the level-order traversal is V.
    (B) The height of the binary tree is 5.
    (C) C is a leaf node.
    (D) The six letter in the level-order traversal is U.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

後序的最後一個是根;在中序中找到根,左邊是左子樹、右邊是右子樹,再依子樹大小切分後序,遞迴重建。

解題方法

  1. 後序最後為 O → 根是 O。中序 IL‾ O VENCU‾\underline{IL}\ O\ \underline{VENCU}:左子樹 {I,L}\{I,L\}、右子樹 {V,E,N,C,U}\{V,E,N,C,U\}。
  2. 後序對應切分:左子樹 LILI、右子樹 VNUCEVNUCE。
  3. 左子樹:後序 LILI 的根為 I;中序 ILIL 中 L 在 I 右邊 → L 是 I 的右子。
  4. 右子樹:後序 VNUCEVNUCE 的根為 E;中序 V E NCUV\ E\ NCU → 左子 V,右子樹 {N,C,U}\{N,C,U\}。
  5. 右子樹 {N,C,U}\{N,C,U\}:後序 NUCNUC 的根為 C;中序 N C UN\ C\ U → 左子 N、右子 U。

重建結果:

          O
        /   \
       I     E
        \   / \
         L V   C
              / \
             N   U
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 19 題

  1. There are nn online courses labeled from 1 to nn. Each course ii is described by courses[i]=[durationi, lastDayi]\text{courses}[i]=[\text{duration}_i,\ \text{lastDay}_i], where the course must be taken continuously for durationi\text{duration}_i days and completed no later than lastDayi\text{lastDay}_i. You start on day 1 and may not take more than one course at a time. The goal is to determine the maximum number of courses that can be taken. Which of the following statements is/are true?
    (A) Time complexity is O(nlog⁡n)O(n\log n).
    (B) Let lastest start time of course i=lastDayi−durationii=\text{lastDay}_i-\text{duration}_i. The course with the minimum latest start time should be selected first.
    (C) A min-heap-based greedy approach provides a suitable solution.
    (D) The course with the earliest lastDay should be selected first.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

這是排程問題「Course Schedule III」:在截止期限限制下,最大化完成的課程數。標準解法是「依截止日排序+max-heap 反悔式貪婪」:

  1. 依 lastDay\text{lastDay} 由小到大排序。
  2. 依序嘗試每門課:先選上它(time+=duration\text{time} \mathrel{+}= \text{duration}),把 duration 放進 max-heap。
  3. 若 time>lastDay\text{time}>\text{lastDay},就從 heap 移除 duration 最大的那門課(time−=該 duration\text{time} \mathrel{-}= \text{該 duration})。
  4. 最後 heap 的大小就是答案。

解題方法

正確性的直覺:依截止日處理,才能用 Exchange argument 證明「已選的課照截止日順序上一定可行」;超時就丟掉最長的課,課程數不變,但剩下的總時間最小,對之後最有利。

courses.sort(key=lambda c: c[1])        # 依 lastDay 排序
heap, time = [], 0                      # 以負值模擬 max-heap
for d, last in courses:
    time += d
    heapq.heappush(heap, -d)
    if time > last:
        time += heapq.heappop(heap)     # 移除最長的課(加回負值)
return len(heap)

排序 O(nlog⁡n)O(n\log n),每門課最多一次 push、一次 pop,各 O(log⁡n)O(\log n),總時間 O(nlog⁡n)O(n\log n),空間 O(n)O(n)。

選項分析

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 20 題

  1. Consider structurally unique binary trees with nn nodes. For example, when n=3n=3, there are 5 structurally unique binary trees.

🖼️【此處有附圖,請對照原卷】

Which of the following statements is (are) true?
(A) The space complexity of determining structurally unique nn-node binary trees is O(n)O(n).
(B) The time complexity of determining structurally unique nn-node binary trees is O(n2)O(n^2).
(C) There are 429 structurally unique binary trees when n=7n=7.
(D) There are 4862 structurally unique binary trees when n=9n=9.

🖼️ 本題含圖表,以下為原卷對應頁面:
原卷第 9 頁

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

nn 個節點的相異二元樹個數是 Catalan 數 CnC_n。選一個節點當根,左子樹 ii 個節點、右子樹 n−1−in-1-i 個節點:

C0=1,Cn=∑i=0n−1Ci Cn−1−i=1n+1(2nn)C_0=1,\qquad C_n=\sum_{i=0}^{n-1}C_i\,C_{n-1-i}=\frac{1}{n+1}\binom{2n}{n}

解題方法

用 DP 由小到大填表:

nn0123456789
CnC_n1125144213242914304862

驗算:C7=18(147)=34328=429C_7=\frac{1}{8}\binom{14}{7}=\frac{3432}{8}=429,C9=110(189)=4862010=4862C_9=\frac{1}{10}\binom{18}{9}=\frac{48620}{10}=4862。

選項分析

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

其他考古題