演算法›Ch6 KMP 字串比對
第 2 題/共 3 題
◀ AL 2/3
2. KMP、Palindrome、Failure Function
#AL-06-002難KMPPalindromeFailure Function

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_0s_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_0s_1\ldots s_j that is also a suffix. After running the pre-processing routine, how can we calculate the smallest number of characters that should be added to the front of the original string to make it a palindrome?

📄 台大111
跳轉到第題
▤完整推導請見《WH 資工筆記 · 演算法》Ch6 KMP 字串比對
本章題號 · 1–3 / 3