Manacher算法详解

1 算法背景

  Manacher算法用于解决回文子串问题。给定字符串 s s s,求其回文子串,回文子串形如 a b a aba aba a b b a abba abba。例如:

s = "abcbbc"
回文子串有:"bcb""cbbc"
最长回文子串为:"cbbc"

1975年Manacher,提出Manacher算法将回文子串问题的时间复杂度提升为 O ( n ) O(n) O(n)

2 常规求解

  回文串分奇回文(形如 a b a aba aba)和偶回文(形如 a b b a abba abba),最常规的求解方法就是以每个字符为中心向两端拓展,以及以两个相邻且相等字符为中心向两端拓展。这样做在处理上和时间复杂度上都很麻烦。下面来详细看看Manacher算法是怎样做的。

3 Manacher算法

3.1 字符串预处理

  Manacher算法首先将整个字符串的首尾以及相邻字符之间插入一个没出现过的特殊字符,假设原字符串为:
a b c b o h h o u abcbohhou abcbohhou
插入 # \# #后得到的新字符串为:
# a # b # c # b # o # h # h # o # u # \#a\#b\#c\#b\#o\#h\#h\#o\#u\# #a#b#c#b#o#h#h#o#u#
这样做的好处就是将原来字符串中的奇回文和偶回文都变成了奇回文,如本例子中的 # b # c # b # \#b\#c\#b\# #b#c#b# # o # h # h # o # \#o\#h\#h\#o\# #o#h#h#o#,这样做就避免了处理上的不便。通常在编写算法的时候会额外在首尾加上两个其他未出现过的字符,方便边界处理,见3.5节伪代码。比如说这样:
$ # a # b # c # b # o # h # h # o # u # % \$\#a\#b\#c\#b\#o\#h\#h\#o\#u\#\% $#a#b#c#b#o#h#h#o#u#%

3.2 回文半径数组

  Manacher算法需要引入一个辅助数组 a r r arr arr,数组长度等于新字符串的长度,数组元素代表以当前字符为中心的最长回文子串的半径,所以叫它回文半径数组。上面的例子及对应的回文半径数组如下。

new_s$#a#b#c#b#o#h#h#o#u#%
arr1212141212125212121

知道了这个数组的定义后,我们可以很清楚地发现,以任意一个字符为中心的最长回文子串的长度就是该字符对应的数组元素值减1。例如 # b # c # b # \#b\#c\#b\# #b#c#b#是以 c c c为中心的回文子串,对应真实的 b c b bcb bcb长度就是 4 − 1 = 3 4-1=3 41=3 # o # h # h # o # \#o\#h\#h\#o\# #o#h#h#o#是以 # \# #为中心的回文子串,对应真实的 o h h o ohho ohho长度就是 5 − 1 = 4 5-1=4 51=4
  所以最重要的就是填充这个回文半径数组了。

3.3 对称关系

  回文子串中心的两侧是镜面对称的如 a b a c a b a abacaba abacaba,这也就意味着在除了中心的整个回文子串内,以某一个字符为中心的回文串长度,等于以其对称位置字符为中心的回文串长度。例如 a b a c a b a abacaba abacaba中,以左边 b b b为中心的回文串长度等于以右边 b b b为中心的回文串长度。

3.4 填充数组

  数组需要遍历填充,在遍历之前先维护两个变量,结合图片(假设数组已经填好一部分)解释其含义:
在这里插入图片描述

  • c e n t e r center center:表示到目前为止最右边界的回文子串的中心的位置,如上图,遍历到当前位置时,左边 c c c的半径能够得最远,别的中心加半径都够不到这么远;
  • r _ b o r d e r r\_border r_border:表示以 c e n t e r center center为中心, a r r [ c e n t e r ] arr[center] arr[center]为半径的回文子串的右边界,如上图。也就是说 r _ b o r d e r = c e n t e r + a r r [ c e n t e r ] r\_border=center+arr[center] r_border=center+arr[center]

因此当遍历到第 i i i个字符的时候,如果 i < r _ b o r d e r i\lt r\_border i<r_border,那就意味着当前字符落入了以 c e n t e r center center为中心的回文子串中,那么我们就可以利用对称关系找到其对称字符的位置 j = 2 ∗ c e n t e r − i j=2*center-i j=2centeri,同时令 a r r [ i ] = a r r [ j ] arr[i]=arr[j] arr[i]=arr[j]。需要注意的是, a r r [ i ] arr[i] arr[i]还受右边界的影响,因此:
a r r [ i ] = m i n ( a r r [ 2 ∗ c e n t e r − i ] ,    r _ b o r d e r − i ) arr[i]=min(arr[2*center-i],\ \ r\_border-i ) arr[i]=min(arr[2centeri],  r_borderi)
  拿上图的当前位置来说, # \# #落入了 c c c为中心的回文串中,那么这个位置就等于它关于 c c c对称位置的值,即 1 1 1。那什么时候受右边界影响呢,假设 c c c左边那个 # \# #的值为4(仅仅是假设!),那么当前位置就应该是 r _ b o r d e r − i = 3 r\_border-i=3 r_borderi=3,因为我们不知道 r _ b o r d e r r\_border r_border往右那部分是啥,这也是上式的由来。
  我们希望 r _ b o r d e r r\_border r_border越靠右边越好,这样能加速判断,所以要时刻更新 c e n t e r center center r _ b o r d e r r\_border r_border

3.5 算法流程以及伪代码

  算法流程如下:

  1. 字符串预处理得到 n e w _ s new\_s new_s
  2. 建立回文半径数组;
  3. 遍历字符串填充数组:
    1)如果 i < r _ b o r d e r i\lt r\_border i<r_border,则 a r r [ i ] = m i n ( a r r [ 2 ∗ c e n t e r − i ] ,    r _ b o r d e r − i ) arr[i]=min(arr[2*center-i],\ \ r\_border-i) arr[i]=min(arr[2centeri],  r_borderi),否则 a r r [ i ] = 1 arr[i]=1 arr[i]=1
    2)以 n e w _ s [ i ] new\_s[i] new_s[i]为中心向两边拓展,同时更新 a r r [ i ] arr[i] arr[i]
    3)如果 a r r [ i ] + i > r _ b o r d e r arr[i] + i\gt r\_border arr[i]+i>r_border,更新 c e n t e r = i center=i center=i r _ b o r d e r = a r r [ i ] + i r\_border=arr[i]+i r_border=arr[i]+i

算法伪代码如下:

findPalindrome(s):

new_s = reprocess(s) #字符串预处理,插入无关字符以及首尾边界字符

for i = 1 to new_s.length then
	if arr[i] < r_border:  #当前元素落在右边界内,则找对称关系
		arr[i] = min(arr[2*center-i], r_border-i)
	else                   #否则arr[i] = 1
		arr[i] = 1
	end if 
	
	while new_s[i-arr[i]] == new_s[i+arr[i]] then #以i为中心向两端拓展,同时半径自增。首尾再加入无关字符就是为了处理边界
		arr[i]++
	end while

	if arr[i] + i > r_border then   #如果以字符为中心的回文子串右端超过右边界,则更新右边界和对称中心
		center = i
		r_border = arr[i]+i
	end if


3.6 实例

  关说不练就比较难受了,拿一个简单例子 b a b a d babad babad来过一遍,对照着上面的伪代码。最开始让 r _ b o r d e r = c e n t e r = 0 r\_border=center=0 r_border=center=0(别的值也无所谓,别影响后面第一步判断就行),开始填充下面这个数组。

new_s$#b#a#b#a#d#%
arr

(1) i = 1 ≥ r _ b o r d e r i=1 \ge r\_border i=1r_border a r r [ 1 ] = 1 arr[1] = 1 arr[1]=1,拓展后 a r r [ 1 ] arr[1] arr[1]仍为1,更新 c e n t e r = 1 , r _ b o r d e r = 2 center=1,r\_border=2 center=1,r_border=2
在这里插入图片描述

new_s$#b#a#b#a#d#%
arr1

(2) i = 2 ≥ r _ b o r d e r i=2 \ge r\_border i=2r_border a r r [ 2 ] = 1 arr[2] = 1 arr[2]=1,拓展后 a r r [ 2 ] arr[2] arr[2]为2,更新 c e n t e r = 2 , r _ b o r d e r = 4 center=2,r\_border=4 center=2,r_border=4
在这里插入图片描述

new_s$#b#a#b#a#d#%
arr12

(3) i = 3 < r _ b o r d e r i=3 \lt r\_border i=3<r_border a r r [ 3 ] = m i n ( a r r [ 1 ] , r _ b o r d e r − 3 ) = 1 arr[3] = min(arr[1],r\_border-3 )=1 arr[3]=min(arr[1],r_border3)=1,拓展后 a r r [ 3 ] arr[3] arr[3]仍为1,不更新 c e n t e r , r _ b o r d e r center,r\_border center,r_border
在这里插入图片描述

new_s$#b#a#b#a#d#%
arr121

(4) i = 4 ≥ r _ b o r d e r i=4 \ge r\_border i=4r_border a r r [ 4 ] = 1 arr[4] =1 arr[4]=1,拓展后 a r r [ 4 ] arr[4] arr[4]为4,更新 c e n t e r = 4 , r _ b o r d e r = 8 center=4,r\_border=8 center=4,r_border=8
在这里插入图片描述

new_s$#b#a#b#a#d#%
arr1214

(5) i = 5 < r _ b o r d e r i=5 \lt r\_border i=5<r_border a r r [ 5 ] = m i n ( a r r [ 3 ] , r _ b o r d e r − 5 ) = 1 arr[5] =min(arr[3],r\_border-5 )=1 arr[5]=min(arr[3],r_border5)=1,拓展后 a r r [ 5 ] arr[5] arr[5]仍为1,不更新 c e n t e r , r _ b o r d e r center,r\_border center,r_border
在这里插入图片描述

new_s$#b#a#b#a#d#%
arr12141

(6) i = 6 < r _ b o r d e r i=6 \lt r\_border i=6<r_border a r r [ 6 ] = m i n ( a r r [ 2 ] , r _ b o r d e r − 6 ) = 2 arr[6] =min(arr[2],r\_border-6 )=2 arr[6]=min(arr[2],r_border6)=2,拓展后 a r r [ 6 ] arr[6] arr[6]为4,更新 c e n t e r = 6 , r _ b o r d e r = 10 center=6,r\_border=10 center=6,r_border=10
在这里插入图片描述

new_s$#b#a#b#a#d#%
arr121414

(7) i = 7 < r _ b o r d e r i=7 \lt r\_border i=7<r_border a r r [ 7 ] = m i n ( a r r [ 5 ] , r _ b o r d e r − 7 ) = 1 arr[7] =min(arr[5],r\_border-7 )=1 arr[7]=min(arr[5],r_border7)=1,拓展后 a r r [ 7 ] arr[7] arr[7]仍为1,不更新 c e n t e r , r _ b o r d e r center,r\_border center,r_border
在这里插入图片描述

new_s$#b#a#b#a#d#%
arr1214141

(8) i = 8 < r _ b o r d e r i=8 \lt r\_border i=8<r_border a r r [ 8 ] = m i n ( a r r [ 4 ] , r _ b o r d e r − 8 ) = 2 arr[8] =min(arr[4],r\_border-8 )=2 arr[8]=min(arr[4],r_border8)=2,拓展后 a r r [ 8 ] arr[8] arr[8]仍为2,不更新 c e n t e r , r _ b o r d e r center,r\_border center,r_border
在这里插入图片描述

new_s$#b#a#b#a#d#%
arr12141412

(9) i = 9 < r _ b o r d e r i=9 \lt r\_border i=9<r_border a r r [ 9 ] = m i n ( a r r [ 3 ] , r _ b o r d e r − 9 ) = 1 arr[9] =min(arr[3],r\_border-9 )=1 arr[9]=min(arr[3],r_border9)=1,拓展后 a r r [ 9 ] arr[9] arr[9]仍为1,不更新 c e n t e r , r _ b o r d e r center,r\_border center,r_border
在这里插入图片描述

new_s$#b#a#b#a#d#%
arr121414121

(10) i = 10 ≥ r _ b o r d e r i=10 \ge r\_border i=10r_border a r r [ 10 ] = 1 arr[10] =1 arr[10]=1,拓展后 a r r [ 10 ] arr[10] arr[10]为2,更新 c e n t e r = 10 , r _ b o r d e r = 12 center=10,r\_border=12 center=10,r_border=12
在这里插入图片描述

new_s$#b#a#b#a#d#%
arr1214141212

(11) i = 11 < r _ b o r d e r i=11 \lt r\_border i=11<r_border a r r [ 11 ] = m i n ( a r r [ 9 ] , r _ b o r d e r − 11 ) = 1 arr[11] =min(arr[9],r\_border-11 )=1 arr[11]=min(arr[9],r_border11)=1,拓展后 a r r [ 11 ] arr[11] arr[11]仍为1,不更新 c e n t e r , r _ b o r d e r center,r\_border center,r_border
在这里插入图片描述

new_s$#b#a#b#a#d#%
arr12141412121

(12)结束。

4 结束

  Leetcode 5是这个算法的练习。

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值