Manacher's algorithm for finding longest palindromic substring

Given a string S, find the longest palindromic substring in S.

An O(N) Solution (Manacher’s Algorithm):
First, we transform the input string, S, to another string T by inserting a special character ‘#’ in between letters. The reason for doing so will be immediately clear to you soon.

For example: S = “abaaba”, T = “#a#b#a#a#b#a#”.

To find the longest palindromic substring, we need to expand around each Ti such that Ti-d … Ti+d forms a palindrome. You should immediately see that d is the length of the palindrome itself centered at Ti.

We store intermediate result in an array P, where P[ i ] equals to the length of the palindrome centers at Ti. The longest palindromic substring would then be the maximum element in P.

Using the above example, we populate P as below (from left to right):

T = # a # b # a # a # b # a #
P = 0 1 0 3 0 1 6 1 0 3 0 1 0

Looking at P, we immediately see that the longest palindrome is “abaaba”, as indicated by P6 = 6.

Did you notice by inserting special characters (#) in between letters, both palindromes of odd and even lengths are handled graciously? (Please note: This is to demonstrate the idea more easily and is not necessarily needed to code the algorithm.)

Now, imagine that you draw an imaginary vertical line at the center of the palindrome “abaaba”. Did you notice the numbers in P are symmetric around this center? That’s not only it, try another palindrome “aba”, the numbers also reflect similar symmetric property. Is this a coincidence? The answer is yes and no. This is only true subjected to a condition, but anyway, we have great progress, since we can eliminate recomputing part of P[ i ]‘s.

Let us move on to a slightly more sophisticated example with more some overlapping palindromes, where S = “babcbabcbaccba”.


Above image shows T transformed from S = “babcbabcbaccba”. Assumed that you reached a state where table P is partially completed. The solid vertical line indicates the center (C) of the palindrome “abcbabcba”. The two dotted vertical line indicate its left (L) and right (R) edges respectively. You are at index i and its mirrored index around C is i’. How would you calculate P[ i ] efficiently?

Assume that we have arrived at index i = 13, and we need to calculate P[ 13 ] (indicated by the question mark ?). We first look at its mirrored index i’ around the palindrome’s center C, which is index i’ = 9.


The two green solid lines above indicate the covered region by the two palindromes centered at i and i’. We look at the mirrored index of i around C, which is index i’. P[ i' ] = P[ 9 ] = 1. It is clear that P[ i ] must also be 1, due to the symmetric property of a palindrome around its center.

As you can see above, it is very obvious that P[ i ] = P[ i' ] = 1, which must be true due to the symmetric property around a palindrome’s center. In fact, all three elements after C follow the symmetric property (that is, P[ 12 ] = P[ 10 ] = 0, P[ 13 ] = P[ 9 ] = 1, P[ 14 ] = P[ 8 ] = 0).


Now we are at index i = 15, and its mirrored index around C is i’ = 7. Is P[ 15 ] = P[ 7 ] = 7?

Now we are at index i = 15. What’s the value of  P[ i ] ? If we follow the symmetric property, the value of  P[ i ] should be the same as P[ i' ] = 7. But this is wrong. If we expand around the center at T15, it forms the palindrome “a#b#c#b#a”, which is actually shorter than what is indicated by its symmetric counterpart. Why?


Colored lines are overlaid around the center at index i and i’. Solid green lines show the region that must match for both sides due to symmetric property around C. Solid red lines show the region that might not match for both sides. Dotted green lines show the region that crosses over the center.

It is clear that the two substrings in the region indicated by the two solid green lines must match exactly. Areas across the center (indicated by dotted green lines) must also be symmetric. Notice carefully that P[ i ' ] is 7 and it expands all the way across the left edge (L) of the palindrome (indicated by the solid red lines), which does not fall under the symmetric property of the palindrome anymore. All we know is  P[ i ]  ≥ 5, and to find the real value of P[ i ] we have to do character matching by expanding past the right edge (R). In this case, since P[ 21 ] ≠ P[ 1 ], we conclude that P[ i ] = 5.

Let’s summarize the key part of this algorithm as below:

if P[ i' ] ≤ R – i,
then P[ i ] ← P[ i' ]
else P[ i ] ≥ R  i. (Which we have to expand past the right edge (R) to find P[ i ].

See how elegant it is? If you are able to grasp the above summary fully, you already obtained the essence of this algorithm, which is also the hardest part.

The final part is to determine when should we move the position of C together with R to the right, which is easy:

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.

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.

from http://leetcode.com/2011/11/longest-palindromic-substring-part-ii.html


本地跟单专家顾问(EA)是一种专为MetaTrader 4平台设计的自动化交易工具。该版本强调其无限制特性,允许用户在任何时段、不同地理区域及各类账户上自由部署,从而为交易者提供了高度灵活的操作空间。其核心机制采用同向复制策略,即接收端会完全模仿发送端的交易方向与操作,适合那些信赖信号源稳定性的用户,以期通过跟随策略实现相近的投资回报。 系统架构包含两个独立模块:信号发送端与信号接收端。发送端安装于主导交易决策的账户,接收端则配置于需同步执行的账户,二者协同工作,实现了交易指令的自动传递与执行,有效减少了人工干预的需求。此外,该工具特别注重与MT4服务器时间的同步,确保交易执行时点的精确性,避免因时区偏差可能引发的操作失误,这对于依赖时间敏感性的外汇市场尤为重要。 文件标识中的特定代号可能指向开发者的内部版本标记或某种定制化交易逻辑,具体含义需结合进一步的技术文档予以确认。整体而言,该EA为多账户管理与策略复制提供了一个集成化解决方案,有助于提升交易执行的效率并降低操作风险。但需注意,市场环境处于持续变动中,任何自动化工具均需经过充分验证与适应性测试,历史表现不能作为未来收益的保证。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
针对XMC1300系列微控制器的直流无刷电机驱动软件开发方案(嵌入式系统设计) 本方案详细阐述基于英飞凌XMC1300系列微控制器的直流无刷电机控制系统的软件实现方法。该方案专注于嵌入式环境下的电机驱动程序设计,涵盖核心控制算法、硬件资源调度及系统稳定性保障等关键技术环节。 在具体实施层面,开发工作将围绕磁场定向控制原理展开,通过精确的转子位置检测与电流闭环调节,实现电机的高效平稳运行。系统软件架构采用模块化设计,包括PWM信号生成模块、ADC采样处理模块、保护机制模块以及通讯接口模块。其中,PWM模块负责输出六路互补信号以驱动三相逆变桥;ADC模块用于实时采集相电流与直流母线电压;保护机制模块集成过流、过压及过热检测功能,确保系统运行安全。 开发过程需严格遵循嵌入式软件工程规范,重点考虑代码执行效率与资源占用优化。程序将充分利用XMC1300芯片内置的CCU4、CCU8定时器单元及快速模拟数字转换器,以实现高精度定时与快速电流采样。同时,软件设计中融入了位置估算算法与启动策略,确保电机在无传感器模式下仍能可靠启动并稳定运行。整个驱动方案旨在构建一个响应迅速、控制精确且鲁棒性强的电机控制系统。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值