[Leetcode] [Python] Longest palindromic Substring

Source: Internet
Author: User

#-*-Coding:utf8-*-
‘‘‘
__author__ = ' [email protected] '
https://oj.leetcode.com/problems/longest-palindromic-substring/

Given A string S, find the longest palindromic substring in S. Assume that the maximum length of S is 1000,
And there exists one unique longest palindromic substring.

===comments by dabay===
Start judging from the longest string until the length is 2. However, such an algorithm will timeout time Limit exceeded.
Using a well-known algorithm called Manacher ' s algorithm:
Each character is separated by a # to be traversed, and the length of the oldest string is recorded from each character to the opposite side.
Add # Here to avoid the problem that palindrome strings are odd or even.
Finally, in the resulting array of record lengths, you can find the maximum palindrome string.

S # a # b # b # # a # b # C # b # a #
P 1 2 1 2 5 2 1 4 1 2 1 6 1 2 1 2 1

The mid record has the center position of the palindrome substring with the largest right boundary, and Max_right records the right edge of the maximum substring.

Initialize position 0

For each position I
If I do not exceed max_right:
Judging if the palindrome length based on the mid symmetry point exceeds the boundary
If the boundary is not exceeded:
Do not continue to judge, the direct assignment is the value of this symmetry point, continue to judge the next I
If the boundary is exceeded:
The part within the boundary does not have to be judged (because of symmetry, it must be a palindrome), continue to look for the longest palindrome, record length
If I exceeds the max_right:
Look for the longest palindrome, record length

If Max_right has been expanded, update mid and Max_right
‘‘‘
Class Solution:
# @return A string
def longestpalindrome (self, s):
S_list = [' # ']
For C in S:
S_list.append (c)
S_list.append (' # ')

Max_right = 0
MID = 0
p = [0 for _ in S_list]
P[0] = 1

For I in xrange (1, Len (P)):
If I <= max_right:
If P[2*mid-i] < max_right-i:
P[i] = P[2*mid-i]
Continue
Else
j = Max_right-i
While I + J < Len (s_list) and i-j >= 0 and s_list[i+j] = = S_list[i-j]:
j = j + 1
P[i] = j
Else
j = 1
While I + J < Len (s_list) and i-j >= 0 and s_list[i+j] = = S_list[i-j]:
j = j + 1
P[i] = j
If Max_right < P[i] + i-1:
Max_right = P[i] + i-1
MID = I

max_p = max (P)
Max_index = P.index (max_p)

Longest = ""
For I in range (max_index-max_p+1, max_index+max_p):
If s_list[i] = = ' # ':
Continue
Longest = longest + s_list[i]
return longest


def main ():
s = solution ()
String = " CUBWQVHXDAMMPKWKYCRQTEGEPYXTOHSPEASRDTINJHBESILSVFFMNZZNMLTSSPJWUOGDYZVANALOHMZRYWDWQQCUKJCEOTHYDLGTOCUKC "
#string = "ABBABCBA"
Print S.longestpalindrome (String)


if __name__ = = "__main__":
Import time
Start = Time.clock ()
Main ()
Print "%s sec"% (Time.clock ()-start)

[Leetcode] [Python] Longest palindromic Substring

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.