Manacher algorithm for finding the longest palindrome substring
0. Longest palindrome substring
1. Manacher algorithm
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-
The character index=5 is within the substring, that is, the right subscript of the substring is max+P [max]=3+3=6, 5<6. Then, since index=5 is in the substring, it can be symmetric about max, and the symmetric point is index '=1. Since it is symmetric, the known information about index' can be used: the palindrome string length centered on index 'is P [index']=1 is "bab"; Since the palindrome string with index=5 as the center is symmetrical, the length of the palindrome string is at least 1, that is, the part from str [index-1] to str [index+1]. When calculating the palindrome string with index=5 as the center, you can skip index=5 to min (index=5+1, max+P [max]) This part (the smaller one is because if the length of palindrome string with index=5 as the center exceeds the longest known right boundary, it can only be determined that the segment from 5 to the right boundary max+P [max] is symmetric about 5, but it cannot be determined whether the rightmost substring with the right boundary max+P [max] to 5 as the center is symmetric), and the matching continues from min (index=5+1, max+P [max])+1. -
If index>max+P [max], the current position exceeds the position of the known information, it can only be matched with ordinary methods. -
Another important problem is that if the string length is even, then a midpoint character will not be found in the above process. The solution is to add a special symbol on both sides of each character in the original string, which is required to be unused in the original string. In this way, even length strings can be changed to odd length strings.