Description
LINK: https://leetcode.com/problems/longest-palindromic-substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example 1:1
2
3Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.
Example 2:1
2Input: "cbbd"
Output: "bb"
Solution
1 | static auto _ = []() |
Discussion
神奇的马拉车算法(Manacher’s Algorithm),找回文子串只需要O(n)的时间复杂度。
算法介绍:https://articles.leetcode.com/longest-palindromic-substring-part-ii/
基本思路是,把字符串扩展,两两之间插入一个特殊字符,例如’#’。
接着用一个数组P来记录以每个点为中心的回文串最长的长度。
这样的好处是,不管回文串长度是奇数还是偶数,都可以很自然地包含在内。
而且,假设中心为i,从i-d到i+d为最长的回文串,那么d刚好就是原字符串中该位置的回文子串长度。
得到数组P的算法流程:1
2
3
4
5
6
7
8
9
10init L = 0, R = 0, C = 0;
if i > R,
then expand L and R, update C = i
else if P[ i’ ] < R – i,
then P[ i ] ← P[ i’ ]
else
P[ i ] >= P[ i’ ]. (Which we have to expand past the right edge (R) to find P[ i ].
update C = i
另外,string的初始化,可以用1
string t(10,'^');
表示初始化为10个'^'。