题目:https://leetcode.com/problems/longest-palindromic-substring/

算法分析

这道题的解法有三种:暴力法、动态规划、Manacher算法。三种方法的时间复杂度分别为O(n3),O(n2),O(n)。暴力法过于简单粗暴,Manacher算法又有点不好理解,所以,这里只介绍动态规划法。对于O(n2)的时间复杂度,也是可以接受的吧。如果,你想追求5G的极速享受,请转接http://articles.leetcode.com/2011/11/longest-palindromic-substring-part-ii.html

既然要利用动态规划,首要任务就是要找到状态转移方程。乍看此题,哪里来的状态转移。所以,我们需要构建状态。

这里,我们要定义一个二维数组Matrix[len(str)][len(str)],用来存储状态。对于为什么这么定义,我只能说记住就行了,以后类似的题目都是可以套用的。

下面,解释一下状态的含义。Matrix[i][j]表示的是字符串str从第i位置到第j位置是否为回文子串,如果是,则存储True,如果不是,则存储False。

这样状态转移方程就可以写出来了:

Matrix[i][j]={True,False,     Matrix[i+1][j−1] and str[i]=str[j]     else

最后,需要注意一些特殊状态的赋值:

Matrix[i][i]=True

Matrix[i][i+1]=True when str[i]=str[i+1]

这样,状态转移方程就建立完成了。我们只需要把这个矩阵填充完,就可以找到最大的回文子串了。

怎么找最大回文子串?你可以在程序开头定义两个变量,分别记录最大回文子串的长度和开始位置,这样在每次更新Matrix矩阵的过程中,比较当前找到的最大回文子串的长度与变量里记录的最大长度,然后决定是否更新,如果更新,就把最大回文子串的开始位置也更新一下。这样,当整个矩阵填充完,相应的最大回文子串也就找到了。

代码实现

动态规划:

class Solution(object):
def longestPalindrome(self, s1):
"""
:type s: str
:rtype: str
"""
length = len(s1)
max = 0
start = 0
matrix = [[True if i == j else False for i in range(length)] for j in range(length)]
for i in range(length-1):
if s1[i] == s1[i+1]:
matrix[i][i+1] = True
max = 1
start = i
for step in range(2, length):
for i in range(length-step):
if s1[i] == s1[i+step] and matrix[i+1][i+step-1]:
matrix[i][i+step] = True
if max < step:
max = step
start = i
return s1[start:start+max+1]

Manacher算法:

class Solution(object):
def longestPalindrome(self, s):
t = '$#' + '#'.join(s) + '#_'
return t
p = [0] * 4010
mx, id, mmax, right = 0, 0, 0, 0
for i in range(1, len(t) - 1):
if mx > i:
p[i] = min(p[2 * id - i], mx - i)
else:
p[i] = 1
while t[i + p[i]] == t[i - p[i]]:
p[i] += 1
if i + p[i] > mx:
mx = i + p[i]
id = i
if mmax < p[i]:
mmax = p[i]
right = i
return s[right//2 - mmax//2: right//2 - mmax//2 + mmax - 1]

Longest Palindromic Substring的更多相关文章

  1. 最长回文子串-LeetCode 5 Longest Palindromic Substring

    题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...

  2. leetcode--5. Longest Palindromic Substring

    题目来自 https://leetcode.com/problems/longest-palindromic-substring/ 题目:Given a string S, find the long ...

  3. [LeetCode] Longest Palindromic Substring 最长回文串

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  4. No.005:Longest Palindromic Substring

    问题: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...

  5. Leetcode Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  6. 【leedcode】 Longest Palindromic Substring

    Given a , and there exists one unique longest palindromic substring. https://leetcode.com/problems/l ...

  7. [LeetCode_5] Longest Palindromic Substring

    LeetCode: 5. Longest Palindromic Substring class Solution { public: //动态规划算法 string longestPalindrom ...

  8. 5. Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  9. leetcode-【中等题】5. Longest Palindromic Substring

    题目 Given a string S, find the longest palindromic substring in S. You may assume that the maximum le ...

  10. Leetcode5:Longest Palindromic Substring@Python

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

随机推荐

  1. linux下的find文件查找命令与grep文件内容查找命令

    在使用linux时,经常需要进行文件查找.其中查找的命令主要有find和grep.两个命令是有区的. 区别:(1)find命令是根据文件的属性进行查找,如文件名,文件大小,所有者,所属组,是否为空,访 ...

  2. Provisional, Temporary 和Interim 的区别

    1 Provisional  adj. 临时的.暂时的.暂定的:n. 临时邮票 强调在一定时期内暂时的.双方同意的但还不是最终确定的决定或者条约等. Such as例如: Provisional go ...

  3. Non-ASCII characters are not allowed outside of literals and identifiers

    出现这种问题,一般是在代码里面非注释的地方,出现了非ascii字符. 比较常见的情况是,在代码中出现了中文字符.比如在引用字符串时,用了中文的引号.或者在一行代码结尾处,使用了中文的分号. 这种问题在 ...

  4. python中threading模块详解(一)

    python中threading模块详解(一) 来源 http://blog.chinaunix.net/uid-27571599-id-3484048.html threading提供了一个比thr ...

  5. Makefile 自动化变量

    Makefile中常用自动化变量解释如下: $@------规则的目标文件名 $<------规则的第一个依赖项文件名 $^------规则的所有依赖文件列表,以空格隔开. $?-------所 ...

  6. Chap6: question 46 - 48

    46. 求 1+2+ … +n. 要求:不用乘除法.for.while.if.else.switch.case 以及条件判断语句(A?B:C). a. 利用构造函数求解 #include <io ...

  7. IPTV视频基本概念

      480x320, 640x480 标清 1024x720p 高清 1920x1080i (隔行扫描) 也属于高清 1920x1080p 全高清 3840x2160,7680x4320 超(高)清 ...

  8. vbs脚本要求在cmd中输入输出用StdIn ,StdOut

    Dim StdIn, StdOutSet StdIn = WScript.StdInSet StdOut = WScript.StdOut Do While Not StdIn.AtEndOfStre ...

  9. 使用DotNetOpenAuth搭建OAuth2.0授权框架

    标题还是一如既往的难取. 我认为对于一个普遍问题,必有对应的一个简洁优美的解决方案.当然这也许只是我的一厢情愿,因为根据宇宙法则,所有事物总归趋于混沌,而OAuth协议就是混沌中的产物,不管是1.0. ...

  10. windows2008r2环境双实例安装mysql5.6

    windows2008r2环境双实例安装mysql5.6 环境:windows2008 r2 标准版 1.默认安装了一个mysql5.6端口为3306 2.使用msi文件安装需要.net4.0支持,安 ...