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. 前端 JS&&DOM续

    JS续 1.序列化 JSON.stringify(obj)   序列化 JSON.parse(str)        反序列化 2.转义 decodeURI( )                   ...

  2. SOA宣言和微服务特点

    如果从概念层来看,我更喜欢把SOA归为企业架构的范畴,从企业架构出发把业务分解为不同业务域的服务,关注系统间的服务互联互通的规范,并不关心如何实现.也就是说在企业架构上使用SOA支撑业务,而在方案架构 ...

  3. 每天一个Linux命令(44)crontab命令

        crontab命令被用来提交和管理用户需要周期性执行的任务,与windows下的计划任务类似.     (1)用法:     用法: crontab  [-u user]  file cron ...

  4. 011_Eclipse中使用HDFSFileSystemAPI事例介绍

    需求 1.文件操作 1)上传本地文件到HDFS 2)读取文件 3)在hadoopfs中新建文件,并写入 4)重命名文件 5)删除hadoopfs上的文件 2.目录操作 1)读取某个目录下的所有文件 2 ...

  5. for语句中多重定义

    "}; vector<string> vecStr(Arr, Arr + sizeof(Arr)/sizeof(string)); , sz = vecStr.size(); i ...

  6. 【CodeChef】Factorial(n!末尾0的个数)

    The most important part of a GSM network is so called Base Transceiver Station (BTS). These transcei ...

  7. 跨平台移动开发_PhoneGap API Camera 使用摄像头采集照片.

    camera对象提供对设备默认摄像头应用程序的访问. 程序运行效果 相关代码 <!DOCTYPE html> <html> <head> <title> ...

  8. C/C++ 字符串操作函数 思维导图梳理

    这些常用的字符串操作函数都是包在string.h头文件中. 分享此图,方便大家记忆 <(^-^)> 选中图片点击右键,在新标签页中打开图片会更清晰

  9. codeforces 439D 思维

    题意:两个数组a,b,每次操作可将其中一个数组的一个数字加1或减1,求最小操作次数使得a数组的最小值大于等于b数组的最大值. 思路: 解法一:考虑最终状态,假设a为数组a中最小的数,b为数组b中最大的 ...

  10. java arrays类学习

    java.util.Arrays类能方便地操作数组,它提供的所有方法都是静态的. 具有以下功能: (1)给数组赋值:通过fill方法. (2)对数组排序:通过sort方法,按升序. (3)比较数组:通 ...