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. 实现对第三方应用任意SO注入

    实现对第三方应用任意SO注入 0x01 应用在Android中运行,从外部对该进程可以进行任意SO文件动态注入,就是应用动态运行我们的SO文件 0x02 基本的逻辑是: 1.    获取目标进程的pi ...

  2. asp.net 移除Server, X-Powered-By, 和X-AspNet-Version头

    我们在开发Asp.net中,最后部署在IIS上. 然后发送HTTP请求,返回的HTTP头中包含Server, X-Powered-By, 和 X-AspNet-Version信息. 这些信息有时给攻击 ...

  3. VC引用静态库

    对于路径的设置: Tools->Options->Directory中设置的Lib路径,是给VC环境设置的, 所以只要是这个VC打开的项目,都会包含这些路径. Project->Se ...

  4. Shell编程之Expect自动化交互程序

    一.Expect自动化交互程序 1.spawn命令 通过spawn执行一个命令或程序,之后所有的Expect操作都会在这个执行过的命令或程序进程中进行,包括自动交互功能. 语法: spawn [ 选项 ...

  5. 表单元素disabled禁用后不能自动提交到服务器

    表单元素disabled禁用后不能自动提交到服务器,项目中需要一个元素只展示不修改,所以把一个input元素设置成了disabled="disabled",但是提交的时候改数据值是 ...

  6. Android电容屏(二):驱动调试分析【转】

    本文转载自:http://blog.csdn.net/xubin341719/article/details/7833383 以goodix的gt8105为例 一.总体架构 硬件部分:先看一个总体的图 ...

  7. Struts2 hibernate spring 概念总结

    Hibernate工作原理及为什么要用? 原理:1.通过Configuration().configure();读取并解析hibernate.cfg.xml配置文件2.由hibernate.cfg.x ...

  8. java深入探究15-SpringMVC

    测试代码:链接:http://pan.baidu.com/s/1c1QGYIk 密码:q924 回顾spring+struts web.xml配置;struts核心过滤器;spring监听器-> ...

  9. HDU 2419 Boring Game(并查集+map)

    感觉做得有点复杂了,但是AC了还是...爽... 题意:给你n个点每个点有一个价值,接下来有m条边,然后是q个操作,每个操作有三种情况: F X K:寻找与X点直接或间接相连的不小于价值K的最小价值, ...

  10. org.apache.http.NoHttpResponseException: XX.XX.XX.XX:80 failed to respond

    解决: Finally I fix the issue and it is caused by buffer size. By default, buffer size of httpclient i ...