Longest palindromic substring - Wikipedia https://en.wikipedia.org/wiki/Longest_palindromic_substring
From Wikipedia, the free encyclopedia
Jump to navigationJump to search
In computer science, the longest palindromic substring or longest symmetric factor problem is the problem of finding a maximum-length contiguous substring of a given string that is also a palindrome. For example, the longest palindromic substring of "bananas" is "anana". The longest palindromic substring is not guaranteed to be unique; for example, in the string "abracadabra", there is no palindromic substring with length greater than three, but there are two palindromic substrings with length three, namely, "aca" and "ada". In some applications it may be necessary to return all maximal palindromic substrings (that is, all substrings that are themselves palindromes and cannot be extended to larger palindromic substrings) rather than returning only one substring or returning the maximum length of a palindromic substring.
Manacher (1975) invented a linear time algorithm for listing all the palindromes that appear at the start of a given string. However, as observed e.g., by Apostolico, Breslauer & Galil (1995), the same algorithm can also be used to find all maximal palindromic substrings anywhere within the input string, again in linear time. Therefore, it provides a linear time solution to the longest palindromic substring problem. Alternative linear time solutions were provided by Jeuring (1994), and by Gusfield (1997), who described a solution based on suffix trees. Efficient parallel algorithms are also known for the problem.[1]
The longest palindromic substring problem should not be confused with the different problem of finding the longest palindromic subsequence.
To find a longest palindrome in a string in linear time, an algorithm may take advantage of the following characteristics or observations about a palindrome and a sub-palindrome:
given string S
string S' = S with a bogus character (eg. '|') inserted between each character (including outer boundaries)
array P = \[0,...,0\] // To store the lengths of the palindrome for each center point in S'
// note: length(S') = length(P) = 2 × length(S) + 1
// Track the following indices into P or S'
R = 0 // The next element to be examined; index into S
C = 0 // The largest/left-most palindrome whose right boundary is R-1; index into S
i = 1 // The next palindrome to be calculated; index into P
**define** L = i − (R − i) // Character candidate for comparing with R; index into S
**define** i' = C − (i − C) // The palindrome mirroring i from C; index into P
**while** R < length(S'):
**If** i is within the palindrome at C (Cases 1 and 2):
Set P\[i\] = P\[i'\] // note: recall P is initialized to all 0s
// Expand the palindrome at i (primarily Cases 2 and 3; can be skipped in Case 1,
// though we have already shown that S'\[R\] ≠ S'\[L\] because otherwise the palindrome
// at i' would have extended at least to the left edge of the palindrome at C):
**while** S'\[R\] == S'\[L\]:
increment P\[i\]
increment R
**If** the palindrome at i extends past the palindrome at C:
update C = i
increment i
**return** max(P)
This diverges a little from Manacher's original algorithm primarily by deliberately declaring and operating on R in such a way to help show that the runtime is in fact linear. You can see in the pseudo-code that R, C and i are all monotonically increasing, each stepping through the elements in S' and P. (the end condition was also changed slightly to not compute the last elements of P if R is already at the end - these will necessarily have lengths less than P[C] and can be skipped).
The use of S' provides a couple of simplifications for the code: it provides a string aligned to P allowing direct use of the pointers in both arrays and it implicitly enables the inner while-loop to double-increment P[i] and R (because every other time it will be comparing the bogus character to itself).
Manachar’s Algorithm Tutorials & Notes | Algorithms | HackerEarth https://www.hackerearth.com/practice/algorithms/string-algorithm/manachars-algorithm/tutorial/
Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 1 - GeeksforGeeks https://www.geeksforgeeks.org/manachers-algorithm-linear-time-longest-palindromic-substring-part-1/
手机扫一扫
移动阅读更方便
你可能感兴趣的文章