|
3 | 3 |
|
4 | 4 | 给定一个字符串S,找出S中最长的回文子字符串。 |
5 | 5 |
|
6 | | -Note: |
7 | | -This is Part II of the article: Longest Palindromic Substring. Here, we describe an algorithm (Manacher’s algorithm) which finds the longest palindromic substring in linear time. Please read Part I for more background information. |
8 | 6 | 注意: |
9 | | -这是最长回文子字符串 http://leetcode.com/2011/11/longest-palindromic-substring-part-i.html 的第二部。 |
| 7 | +这是最长回文子字符串 http://leetcode.com/2011/11/longest-palindromic-substring-part-i.html 的第二部。这里,我们要描述一个算法(Manacher算法),可以在线性时间内找出最长回文子字符串。请阅读第一部 http://leetcode.com/2011/11/longest-palindromic-substring-part-i.html 了解更多的内幕(傲骄了)。 |
| 8 | + |
| 9 | +Manacher |
| 10 | + |
10 | 11 |
|
11 | | -In my previous post we discussed a total of four different methods, among them there’s a pretty simple algorithm with O(N2) run time and constant space complexity. Here, we discuss an algorithm that runs in O(N) time and O(N) space, also known as Manacher’s algorithm. |
12 | 12 |
|
13 | 13 | Hint: |
14 | 14 | Think how you would improve over the simpler O(N2) approach. Consider the worst case scenarios. The worst case scenarios are the inputs with multiple palindromes overlapping each other. For example, the inputs: “aaaaaaaaa” and “cabcbabcbabcba”. In fact, we could take advantage of the palindrome’s symmetric property and avoid some of the unnecessary computations. |
@@ -62,56 +62,7 @@ The final part is to determine when should we move the position of C together wi |
62 | 62 | If the palindrome centered at i does expand past R, we update C to i, (the center of this new palindrome), and extend R to the new palindrome’s right edge. |
63 | 63 | In each step, there are two possibilities. If P[ i ] ≤ R – i, we set P[ i ] to P[ i' ] which takes exactly one step. Otherwise we attempt to change the palindrome’s center to i by expanding it starting at the right edge, R. Extending R (the inner while loop) takes at most a total of N steps, and positioning and testing each centers take a total of N steps too. Therefore, this algorithm guarantees to finish in at most 2*N steps, giving a linear time solution. |
64 | 64 |
|
65 | | - |
66 | | -1 |
67 | | -2 |
68 | | -3 |
69 | | -4 |
70 | | -5 |
71 | | -6 |
72 | | -7 |
73 | | -8 |
74 | | -9 |
75 | | -10 |
76 | | -11 |
77 | | -12 |
78 | | -13 |
79 | | -14 |
80 | | -15 |
81 | | -16 |
82 | | -17 |
83 | | -18 |
84 | | -19 |
85 | | -20 |
86 | | -21 |
87 | | -22 |
88 | | -23 |
89 | | -24 |
90 | | -25 |
91 | | -26 |
92 | | -27 |
93 | | -28 |
94 | | -29 |
95 | | -30 |
96 | | -31 |
97 | | -32 |
98 | | -33 |
99 | | -34 |
100 | | -35 |
101 | | -36 |
102 | | -37 |
103 | | -38 |
104 | | -39 |
105 | | -40 |
106 | | -41 |
107 | | -42 |
108 | | -43 |
109 | | -44 |
110 | | -45 |
111 | | -46 |
112 | | -47 |
113 | | -48 |
114 | | -49 |
| 65 | +``` |
115 | 66 | // Transform S into T. |
116 | 67 | // For example, S = "abba", T = "^#a#b#b#a#$". |
117 | 68 | // ^ and $ signs are sentinels appended to each end to avoid bounds checking |
@@ -161,6 +112,8 @@ string longestPalindrome(string s) { |
161 | 112 | |
162 | 113 | return s.substr((centerIndex - 1 - maxLen)/2, maxLen); |
163 | 114 | } |
| 115 | +``` |
| 116 | + |
164 | 117 | Note: |
165 | 118 | This algorithm is definitely non-trivial and you won’t be expected to come up with such algorithm during an interview setting. However, I do hope that you enjoy reading this article and hopefully it helps you in understanding this interesting algorithm. You deserve a pat if you have gone this far! :) |
166 | 119 |
|
|
0 commit comments