5. Longest Palindromic Substring[M]最长回文子串
题目
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example1:
Input: "babad"
Output: "bab"
Note: "aba is also a valid answer. "
Example2:
Input: "cbbd"
Output: "bb"
思路
思路1:动态规划
Step1:刻画一个最优解方程
\(dp[i][j]\)表示子串\(s[i, \cdots,j]\)是否是一个回文子串
Step2:递归定义最优解的值
(1)初始化:
- dp[i][i] = true, i = [0, 1, ... ,n-1];
- dp[i][i-1] = true, i = [1,2,...,n-1]
- 其余为false
(2)状态转移表
- dp[i][j] = (s[i] == s[j] && dp[i+1][j-1])
状态转移表更新如图1:
Step3:计算最优解的值
根据状态转移表,以及递推公式,计算dp[i][j]。
思路2:中心扩展法
以某字符为中心,分别计算回文长度。分为回文子串为奇数、偶数两种情况
- 奇数:以当前遍历字符为中心判断
- 偶数:以当前遍历字符与其相邻字符为中心判断
思路3:Manacher算法
又称为马拉车算法,可以在时间复杂都为O(n)的情况下求解一个字符串的最长回文子串的问题。
Manacher算法通过为字符串虚拟增加#(并不是真的增加#),使得长度为奇数和长度为偶数的回文子串放在一起考虑(使得回文子串长度都为奇数),如图1。具体操作:在字符串的首部、尾部、相邻字符之间虚拟增加#号。
(1)Len数组的性质
(2)Len数组的计算
思路4:字符串分片(python)
利用字符串的分片操作来检测是否是回文。
Tips
动态规划
将待求解问题分解为若干个非互相独立的子问题,先求子问题,再求原问题。(通常需要将不同阶段的不同状态保存在二维数组内)。
C++
- 思路1
class Solution {
public:
string longestPalindrome(string s) {
int nLength = s.size();
if(nLength<1)
return s;
vector<vector<bool> > dp(nLength, vector<bool>(nLength, 0)); //dp[i][j]表示子串s[i,...,j]是否是一个回文子串
int strBegin = 0; //回文子串的开始
int strEnd = 0; //回文子串的结尾
//初始化
for(int i = 1;i < nLength; i++){
dp[i][i] = true;
dp[i][i-1] = true; //这个是针对子串长度为2,"bb"、"aa"的情况
}
dp[0][0] = true;
//动态规划
for(int i = 2;i <= nLength; i++){ //回文长度
for(int j = 0; j <= nLength - i ; j++){ //回文子串起始
if(s[j] == s[i+j - 1] && dp[j+1][i+j-2]){
dp[j][j+i-1] = true;
if(strEnd - strBegin + 1 < i){
strBegin = j;
strEnd = i + j -1;
}
}
}
}
return s.substr(strBegin,strEnd-strBegin+1);
}
};
- 思路2
class Solution {
public:
string longestPalindrome(string s) {
int nLength = s.size();
if(nLength == 1)
return s;
int strBegin = 0;
int maxLength = 0;
for(int i = 1;i < nLength; i++){
//如果回文子串是奇数,以i为中心搜索
int left = i - 1;
int right = i + 1;
while(left >=0 && right < nLength && s[left] == s[right] )
{
left --;
right ++;
}
if(right - left - 1 > maxLength){ //right -1 - (left + 1) + 1
maxLength = right - left - 1;
strBegin = left + 1;
}
//如果回文子串是偶数,
left = i - 1;
right = i;
while(left >=0 && right < nLength && s[left] == s[right]){
left --;
right ++;
}
if(right - left - 1 > maxLength){
maxLength = right - left - 1;
strBegin = left + 1;
}
}
return s.substr(strBegin,maxLength);
}
};
- 思路3
class Solution {
public:
string longestPalindrome(string s) {
if(s.size() <= 1)
return s;
string dummy = init(s);
int nLength = dummy.size();
int maxLen = 0;
int mx = 0;
int id = 0;
vector<int> len(nLength, 0);
for(int i =1;i< nLength - 1; i++){
if(i < mx)
len[i] = min(len[2*id -i], mx - i);
else
len[i] = 1;
while(dummy[i - len[i]] == dummy[i + len[i]])
len[i] ++;
if(mx < i + len[i]){
id = i;
mx = i + len[i];
}
}
int index = 0;
for(int i = 1; i < nLength-1; i++){
if(len[i] > maxLen){
maxLen = len[i];
index = i;
}
}
return s.substr((index - maxLen)/2, maxLen-1);
}
//初始化
string init(const string& s){
string result = "$#";
int nLength = s.size();
for(int i=0;i < nLength; i++){
result.push_back(s[i]);
result.push_back('#');
}
return result;
}
};
Python
class Solution(object):
def longestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
if len(s) == 1:
return s
result = ""
for i in range(len(s)):
j = i + 1
while j <= len(s) and len(result) <= len(s[i:]):
if s[i:j] == s[i:j][::-1] and len(s[i:j]) > len(result):
result = s[i:j]
j += 1
return result
参考
[1] https://blog.csdn.net/suool/article/details/38383045
5. Longest Palindromic Substring[M]最长回文子串的更多相关文章
- 21.Longest Palindromic Substring(最长回文子串)
Level: Medium 题目描述: Given a string s, find the longest palindromic substring in s. You may assume ...
- 面试常用算法——Longest Palindromic Substring(最长回文子串)
第一种: public static void main(String[] args) { String s = "abcbaaaaabcdcba"; int n,m; Strin ...
- Manacher's algorithm: 最长回文子串算法
Manacher 算法是时间.空间复杂度都为 O(n) 的解决 Longest palindromic substring(最长回文子串)的算法.回文串是中心对称的串,比如 'abcba'.'abcc ...
- 最长回文子串-LeetCode 5 Longest Palindromic Substring
题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- [译+改]最长回文子串(Longest Palindromic Substring) Part II
[译+改]最长回文子串(Longest Palindromic Substring) Part II 原文链接在http://leetcode.com/2011/11/longest-palindro ...
- [译]最长回文子串(Longest Palindromic Substring) Part I
[译]最长回文子串(Longest Palindromic Substring) Part I 英文原文链接在(http://leetcode.com/2011/11/longest-palindro ...
- 求最长回文子串 - leetcode 5. Longest Palindromic Substring
写在前面:忍不住吐槽几句今天上海的天气,次奥,鞋子里都能养鱼了...裤子也全湿了,衣服也全湿了,关键是这天气还打空调,只能瑟瑟发抖祈祷不要感冒了.... 前后切了一百零几道leetcode的题(sol ...
- LeetCode:Longest Palindromic Substring 最长回文子串
题目链接 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- lintcode :Longest Palindromic Substring 最长回文子串
题目 最长回文子串 给出一个字符串(假设长度最长为1000),求出它的最长回文子串,你可以假定只有一个满足条件的最长回文串. 样例 给出字符串 "abcdzdcab",它的最长回文 ...
随机推荐
- DB2大数据量优化查询解决方案
利用DB2表分区的功能对大数据量的表进行分区,可以优化查询. 表分区介绍: 表分区是一种数据组织方案,它根据一列或多列中的值把表数据划分为多个称为数据分区 的存储对象. (我觉得表分区就类似于Wind ...
- cropper+pillow处理上传图片剪裁(二)
上次说到处理上传头像的方法是临时保存在本地,其实这样做并不妥,而且修改后的头像重新上传到model中也很麻烦,差了很多资料,解决了这个问题 大致思路:用户上传原图和修改数据—PIL修改后将图片保存为内 ...
- 初学 Ajax(涉及 php)
一直知道 ajax 但是尚未真正了解, 这次看了慕课网的<Ajax全接触>,算是有所收获,入了个门. 需要用到php,因为 Ajax也是向服务器请求(不知道这么解释对不对), 所以还需要配 ...
- 图片全屏轮播插件poposlides
jQuery轻量级全屏自适应焦点图插件poposlides 在线演示本地下载
- JavaScript数组的操作
<html> <head> <meta charset="utf-8"> <title>无标题文档</title> &l ...
- [INS-30131] 执行安装程序验证所需的初始设置失败问题解决,windows下oracle安装步骤
[INS-30131] 执行安装程序验证所需的初始设置失败问题解决,windows下oracle安装步骤 配置: 系统:windows10 数据库:Oracle Database 12c 第 1 版 ...
- BluetoothA2dp蓝牙音箱的连接
1:权限 <uses-feature android:name="android.hardware.bluetooth_le" android:required=" ...
- UBuntu安裝使用PIP
Windows下安裝python包還是比較方便的,直接在FLD網站下載對應的EXE文件就可以安裝,在linux系統下,使用pip,easy egg 管理工具可以減輕安裝負擔. 原文鏈接:http:// ...
- HTML 引入 CSS、JS 的三种方式
描述部分按 CSS 来的,其实 JavaScript 也一样,具体区别看代码 外部样式表 当样式需要被应用到很多页面的时候,外部样式表将是理想的选择.使用外部样式表,你就可以通过更改一个文件来改变整个 ...
- Django 的 一些基本操作:视图函数,路由配置
当安装好Django 通过上篇的随笔创好项目我们就能来耍下了, 但你会在你的项目中发现有一个settings.py 的文件,对的你肯定想到了需要配置,好了话不多说 Settings.py 找到下面的位 ...