题目:

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.

解题思路:

主要有三种:

第一种:Manacher算法,也是最快的,时间复杂度为O(n)

第二种:DP算法,时间复杂度为O(n*n)

第三种:中心法,时间复杂度为O(n*n)

实现代码:

#include <iostream>
#include <vector>
using namespace std; /** 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.
*/ class Solution {
public:
//Manacher算法(O(n))
string longestPalindrome(string s) {
string p;
if(s.empty())
return p;
int id = 0;
int mx = 0;
//以下对要操作的副本str进行格式化,使得其为奇数
string str("^");
for(int i = 0; i < s.size(); i++)
{
str += "#";
str += s[i];
}
str += "#$";
vector<int> r(str.size(), 0); for(int i = 1; i < str.size()-1; i++)
{
if(mx > i)
r[i] = min(r[2*id - i], mx - i);
else
r[i] = 1;
while(str[i+r[i]] == str[i-r[i]])//为了防止越界,str头尾插入了'^'和'$'字符
r[i]++;
if(r[i] + i > mx)
{
mx = r[i] + i;
id = i;
}
} int maxlen = 0;
int maxid = 0;
for(int i = 1; i < str.size()-1; i++)
{
if(r[i] > maxlen)
{
maxlen = r[i];
maxid = i;
}
}
//(maxid-1)/2为原字符串中最长回文字串中心字符位置
//(maxlen-1)/2为原字符串中最长回文子串半径
//maxlen-1为原字符串中最长回文字串长度
return s.substr((maxid-1)/2 - (maxlen-1)/2, maxlen-1); } //DP O(n*n)
string longestPalindrome2(string s) {
string p;
if(s.empty())
return p;
int len = s.size();
vector<vector<bool>> dp(len, vector<bool>(len, false));//dp[i][j]表示i~j的字串是否为回文串
int maxstart = 0;
int maxlen = 1;
for(int i = 0; i < len-1; i++)
{
dp[i][i] = true;
if(s[i] == s[i+1])
{
dp[i][i+1] = true;
maxstart = i;
maxlen = 2;
}
} for(int l = 3; l <= len; l++)
{
for(int i = 0; i < len-l+1; i++)
{
int j = i+l-1;
if(s[i] == s[j] && dp[i+1][j-1])
{
dp[i][j] = true;
maxstart = i;
maxlen = l;
} }
}
return s.substr(maxstart, maxlen); } //中心法,以每一个字符作为回文串中心,向两边扩展
string longestPalindrome3(string s) {
string p;
if(s.empty())
return p;
int len = s.size();
int maxstart = 0;
int maxlen = 0;
for(int i = 0; i < len; i++)
{
int l = i-1;
int r = i+1;
int tmpmax = 1;//已i为中心的回文串:奇数
while(l >= 0 && r < len && s[l--] == s[r++])
tmpmax++;
if(maxlen < tmpmax*2 -1)
{
maxlen = tmpmax*2 -1;
maxstart = l+1;
} int l2 = i;
int r2 = i+1;
int tmpmax2 = 0;//已i和i+1为中心的回文串,偶数时
while(l2 >= 0 && r2 < len && s[l2--] == s[r2++])
tmpmax2++; if(maxlen < tmpmax2*2)
{
maxlen = tmpmax2*2;
maxstart = l2+1;
} } return s.substr(maxstart, maxlen); } }; int main(void)
{
string s("abbacdd");
Solution solution;
string p = solution.longestPalindrome3(s);
cout<<p<<endl;
return 0;
}

LeetCode5:Longest Palindromic Substring的更多相关文章

  1. Leetcode5:Longest Palindromic Substring@Python

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

  2. No.005:Longest Palindromic Substring

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

  3. leetcode:Longest Palindromic Substring(求最大的回文字符串)

    Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...

  4. lintcode :Longest Palindromic Substring 最长回文子串

    题目 最长回文子串 给出一个字符串(假设长度最长为1000),求出它的最长回文子串,你可以假定只有一个满足条件的最长回文串. 样例 给出字符串 "abcdzdcab",它的最长回文 ...

  5. LeetCode第[5]题(Java):Longest Palindromic Substring 标签:String、动态规划

    题目中文:求最长回文子串 题目难度:Medium 题目内容: Given a string s, find the longest palindromic substring in s. You ma ...

  6. LeetCode OJ:Longest Palindromic Substring(最长的回文字串)

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  7. LeetCode第五题:Longest Palindromic Substring

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

  8. 【LeetCode5】Longest Palindromic Substring★★

    1.题目描述: 2.解题思路: 题意:求一个字符串的最长回文子串. 方法一:中心扩展法.遍历字符串的每一个字符,如果存在回文子串,那么中心是某一个字符(奇数)或两个字符的空隙(偶数),然后分两种情况( ...

  9. LeetCode:Longest Palindromic Substring 最长回文子串

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

随机推荐

  1. ios UIView autoresizingSubview 属性

    自动尺寸调整行为 当您改变视图的边框矩形时,其内嵌子视图的位置和尺寸往往也需要改变,以适应原始视图的新尺寸.如果视图的autoresizesSubviews属性声明被设置为YES,则其子视图会根据au ...

  2. Canvas 内部元素添加事件处理

    目录 前言 自定义事件 有序数组 元素父类 事件判断 其他 立即执行函数 apply, call, bind addEventListener 传参 调用父类的构造函数 对象检测 isPointInP ...

  3. EF架构~linq模拟left join的两种写法,性能差之千里!

    回到目录 对于SQL左外连接我想没什么可说的,left join将左表数据都获出来,右表数据如果在左表中不存在,结果为NULL,而对于LINQ来说,要实现left join的效果,也是可以的,在进行j ...

  4. css_04之显示、定位

    1.显示方式:display:取值:none(隐藏,不占页面空间,脱离文档流)/block(元素变为块级)/inline(元素变为行内)/inline-block(元素变为行内块): 2.显示效果:v ...

  5. 使用SQLAlchemy

    使用SQLAlchemy 参考: http://www.sqlalchemy.org/ https://www.keakon.net/2012/12/03/SQLAlchemy%E4%BD%BF%E7 ...

  6. VS报错:_CRT_SECURE_NO_WARNINGS

    常见报错:warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead ...

  7. JAVA实现Excel的读写--poi

    上一篇为大家介绍了通过xls.jar的方式生成Excel的方法,本篇就为大家再介绍一下通过poi方式实现Excel文件的读写操作,内容很简单,代码注释很清晰. 1.生成Excel文件: import ...

  8. java中并不是任意多个接口都可以实现多实现

    interface A{ public abstract void show(); } interface B{ public abstract int show(); } public class ...

  9. (转)Shell函数

    Shell函数类似于Shell脚本,里面存放了一系列的指令,不过Shell的函数存在于内存,而不是硬盘文件,所以速度很快,另外,Shell还能对函数进行预处理,所以函数的启动比脚本更快. 1.  函数 ...

  10. MAC下安装与配置MySQL

    MAC下安装与配置MySQL   MAC下安装与配置MySQL 一 下载MySQL 访问MySQL的官网http://www.mysql.com/downloads/ 然后在页面中会看到“MySQL ...