← 回題庫

卷別

資料結構與演算法(A)

選擇

國立臺灣大學 2022 · 考試 · 第 11 題

考科演算法

(5 points) Consider a string of length nn, e.g., cbaabcba. We want to add the smallest number of characters to the front of the string to make it a palindrome, e.g., abcbaabcba. This problem can be solved by first concatenating the string with its inverse with a delimiter #, e.g. cbaabcba#abcbaabc. Then, run the pre-processing routine of the Knuth-Morris-Pratt algorithm on the new string s0s1⋯s2ns_0 s_1 \cdots s_{2n} of length (2n+1)(2n + 1) to get a failure function fs(j)f_s(j), which computes the length of the longest prefix of the string s0s1…sjs_0 s_1 \dots s_j that is also a suffix. After running the pre-processing algorithm, how can we calculate the smallest number of characters that should be added to the front of the original string to make a palindrome?

  • (A)
    n−fs(n−1)+1n - f_s(n-1) + 1
  • (B)
    n−fs(n)n - f_s(n)
  • (C)
    n−max⁡0≤j≤n−1(fs(j))n - \max_{0 \le j \le n-1} (f_s(j))
  • (D)
    n−max⁡n+1≤j≤2n(fs(j))n - \max_{n+1 \le j \le 2n} (f_s(j))
  • (E)
    none of the other choices

標準解析

AI 生成