Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.

Example:

Input: "babad"

Output: "bab"

Note: "aba" is also a valid answer.

Example:

Input: "cbbd"

Output: "bb"

题解:

Solution 1

  暴力搜索,所有可能,注意到"bab"和"baab"两种类型的回文字符串即可。

 class Solution {
public:
string longestPalindrome(string s) {
int start = , len = ;
int n = s.size();
if (n < )
return s;
for (int i = ; i < n - ; ++i) {
rangeOfPalindrome(s, i, i + , start, len); // "baab"
rangeOfPalindrome(s, i, i, start, len); // "bab"
}
return s.substr(start, len);
} void rangeOfPalindrome(const string& s, int left, int right, int& start, int& len) {
int length = len;
int step = ;
while ((left - step >= ) && (right + step < s.size())) {
if (s[left - step] != s[right + step])
break;
++step;
}
length = * (step - ) + right - left + ;
if (length > len) {
len = length;
start = left - (step - );
}
} };

  代码简化:

 class Solution {
public:
string longestPalindrome(string s) {
string res;
int n = s.size(), idx = , len = ;
for (int i = ; i < n; ++i) {
search(s, i, i, idx, len);
search(s, i, i + , idx, len);
}
return s.substr(idx, len);
}
void search(const string& s, int i, int j, int& idx, int& len) {
while (i >= && j < s.size() && s[i] == s[j]) {
--i;
++j;
}
if (len < j - i - ) {
len = j - i - ;
idx = i + ;
}
}
};

Soluion 2

  DP问题。一个长度为 n(n>1) 的回文字符串S(s1, s2,...,sn),若将字符s0和sn+1分别放置在S的首尾,此时如果s0 == sn+1,那么新的字符串S'也一定是回文字符串。

  那么递归式为 dp[i][j] = 1                                            if i == j

           = s[i] == s[j]                            if i + 1 = j

= s[i] == s[j] && dp[i + 1][j - 1]   if i + 1 < j

 class Solution {
public:
string longestPalindrome(string s) {
int n = s.size();
if (n < )
return s;
int len = , start = ;
vector<vector<int>> dp(n, vector<int>(n, )); for (int i = ; i < n; ++i) {
for (int j = ; j <= i; ++j) {
dp[j][i] = (s[i] == s[j]) && (i - j <= || dp[j + ][i - ]);
if (dp[j][i] && len < i - j + ) {
len = i - j + ;
start = j;
}
}
} return s.substr(start, len);
}
};

  Manacher算法

Solution 3

【LeetCode】005. Longest Palindromic Substring的更多相关文章

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

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:最长回文子串,题解,leetcode, 力扣,python ...

  2. 【LeetCode】5. Longest Palindromic Substring 最大回文子串

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

  3. 【leetcode】5. Longest Palindromic Substring

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

  4. 【一天一道LeetCode】#5 Longest Palindromic Substring

    一天一道LeetCode系列 (一)题目 Given a string S, find the longest palindromic substring in S. You may assume t ...

  5. 【LeetCode】516. Longest Palindromic Subsequence 最长回文子序列

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 代码 刷题心得 日期 题目地址:https://le ...

  6. 【leetcode】516. Longest Palindromic Subsequence

    题目如下: 解题思路:很经典的动态规划题目,但是用python会超时,只好用C++了. 代码如下: class Solution { public: int longestPalindromeSubs ...

  7. 【SP1812】LCS2 - Longest Common Substring II

    [SP1812]LCS2 - Longest Common Substring II 题面 洛谷 题解 你首先得会做这题. 然后就其实就很简单了, 你在每一个状态\(i\)打一个标记\(f[i]\)表 ...

  8. 【SP1811】LCS - Longest Common Substring

    [SP1811]LCS - Longest Common Substring 题面 洛谷 题解 建好后缀自动机后从初始状态沿着现在的边匹配, 如果失配则跳它的后缀链接,因为你跳后缀链接到达的\(End ...

  9. 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)

    [LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...

随机推荐

  1. 解决 flex align-items:center 无法居中(微信小程序)

    因为最近再做小程序,需要用到flex布局,因为写惯了web项目,初次学习确实感弹性布局的强大(关键是不用再管可恶的ie了). 但是也遇到了align-items:center无法居中的问题,想了很久终 ...

  2. ajax数据请求的理解

    一,请求 发送请求有两种方式:get 跟 post . 1.get仅请求数据,不需要服务端做处理,最后会返回指定的资源. 2.post可以提交数据,服务端根据提交的数据做处理,再返回数据. 二,创建X ...

  3. 【CodeChef】Small factorials(BigInteger笔记)

    You are asked to calculate factorials of some small positive integers. Input An integer t, 1<=t&l ...

  4. 20145222黄亚奇 《网络对抗技术》 MAL_逆向与Bof基础

    学习目的 通过一些方法,使能够运行本不该被运行的代码部分,或得到shell的使用: 将正常运行代码部分某处call后的目标地址,修改为另一部分我们希望执行.却本不应该执行的代码部分首地址(这需要我们有 ...

  5. PHP实现链式操作

    什么是链式操作 我们经常会在一些应用框架中看到如下代码: $db = new Database; $db->where('cid = 9')->order('aid desc')-> ...

  6. jQuery 获取jsp页面中用iframe引入的jsp页面中的值

    <iframe scrolling="no" src="<c:url value='/unitBaseperson/view.do?para=9&op ...

  7. QT 中一些数学计算函数

    QT的一些範例中有出現 qmax, qmin 等 math函式的身影,但我在官方文件中卻找不到與 math函式相關的說明,所以我就把函式的source裡面提供的方法整理條列,並且看看還有哪些 math ...

  8. 在openrc中从keystone V2 到 V3

    #!/bin/bash ] then echo "Usage $0 <keystone.rc>" exit fi . $ NEW_OS_AUTH_URL=`echo $ ...

  9. CSS3中与文字相关的样式

    1.给文字添加阴影:text-shadow属性(特别指出IE浏览器要IE10+的版本才支持)    语法如下: text-shadow:length length length color; 其中,第 ...

  10. php特级课---3、常用的网站加速技术有哪些

    php特级课---3.常用的网站加速技术有哪些 一.总结 一句话总结:网站加速技术是一组技术的组合,来提升网站的速度 1.Squid代理缓存技术 2.页面静态化缓存 3.Memcache 4.Sphi ...