[LeetCode][Python]Longest Palindromic Substring
# -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com'
https://oj.leetcode.com/problems/longest-palindromic-substring/ Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000,
and there exists one unique longest palindromic substring. ===Comments by Dabay===
从最长的字符串开始判断,直到长度为2。但是这样的算法会超时Time Limit Exceeded。
使用一个著名的算法,叫Manacher's ALGORITHM:
每一个字符之间加上一个#来隔开,遍历的时候,从每个字符往两边延生,记录最长子串的长度。
这里加入#,避免了回文字符串是奇数或偶数的问题。
最后,在生成的记录长度的数组中,找到最大回文子串即可。 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 mid记录包含最大右边界的回文子串的中心位置,max_right记录最大子串的右边界。 初始化位置0 对于每一个位置i
如果i不超过max_right:
判断基于mid对称点的回文长度是否超过边界
如果没有超过边界:
不用继续判断了,直接赋值为这个对称点的值,继续判断下一个i
如果超过边界:
在边界内的部分不用判断了(因为对称性,肯定是回文),继续寻找最长回文,记录长度
如果i超过了max_right:
寻找最长回文,记录长度 如果max_right得到了拓展,更新mid和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的更多相关文章
- LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法
LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法 题记 本文是LeetCode题库的第五题,没想到做这些题的速度会这么慢,工作之 ...
- Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法)
Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法) Given a string s, find the longest pal ...
- 求最长回文子串 - leetcode 5. Longest Palindromic Substring
写在前面:忍不住吐槽几句今天上海的天气,次奥,鞋子里都能养鱼了...裤子也全湿了,衣服也全湿了,关键是这天气还打空调,只能瑟瑟发抖祈祷不要感冒了.... 前后切了一百零几道leetcode的题(sol ...
- LeetCode 5 Longest Palindromic Substring(最长子序列)
题目来源:https://leetcode.com/problems/longest-palindromic-substring/ Given a string S, find the longest ...
- 【JAVA、C++】LeetCode 005 Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- leetcode:Longest Palindromic Substring(求最大的回文字符串)
Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...
- leetcode 5 :Longest Palindromic Substring 找出最长回文子串
题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...
- 蜗牛慢慢爬 LeetCode 5.Longest Palindromic Substring [Difficulty: Medium]
题目 Given a string s, find the longest palindromic substring in s. You may assume that the maximum le ...
- 【LeetCode】Longest Palindromic Substring 解题报告
DP.KMP什么的都太高大上了.自己想了个朴素的遍历方法. [题目] Given a string S, find the longest palindromic substring in S. Yo ...
随机推荐
- jquery 实现横向滑动自动切换源码(同时显示多张图片)
html代码: <!doctype html> <html lang="en"> <head> <meta charset="U ...
- C++进程间通信(常用理解例子)-买票
#include "stdafx.h" #include <iostream>using namespace std; #include "windows.h ...
- python 初学笔记 (一)
初学python第一天,希望自己真正了解计算机语言,并且做出成效. 写下学习笔记,记录学习进度,娱乐学习,不断成长. python详细介绍: python是什么?运用到哪里?有哪些在使用它? pyth ...
- 动态创建分页 LINQ+EF
public class Message { public int MessageId { get; set; } public string MessageTitle { get; set; } p ...
- Oracle EBS-SQL (BOM-8):检查物料属性(无采购员).sql
select msi.segment1 物料编码, msi.DESCRIPTION ...
- 联想企业网盘:SaaS服务集群化持续交付实践
1 前言 当代信息技术飞速发展,软件和系统的代码规模都变得越来越大,而且组件众多,依赖繁复,每次新版本的发布都仿佛是乘坐一次无座的绿皮车长途夜行,疲惫不堪.软件交付是一个复杂的工程,涉及到软 ...
- haproxy hdr_beg 配置
v-dev-app01:/root# ping www.zjdev.com PING www.zjdev.com (192.168.32.16) 56(84) bytes of data. 64 by ...
- 安装gstreamer
安装gstreamerglib2.44.0locate libffi.soexport LIBFFI_CFLAGS=-L/opt/vagrant/embedded/lib/./confiure./co ...
- (Pre sell) ZOPO ZP998 (C2 II) 5.5 inch smart phone True Octa Core MTK6592 1920X1080 FHD screen 401 ppi 2GB/32GB 14.0Mp camera-in Mobile Phones from Electronics on Aliexpress.com
(Pre sell) ZOPO ZP998 (C2 II) 5.5 inch smart phone True Octa Core MTK6592 1920X1080 FHD screen 401 p ...
- Hibernate 多表关联映射- Hibernate中使用的集合类型(set,list,array,bag,map)
Set类型的使用: <hibernate-mapping package="cn.model"> <class name="Department&quo ...