题目:

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. windows进程通信 -- WM_COPYDATA消息

    WM_COPYDATA消息,在win32中用来进行进程间的数据传输. typedef struct tagCOPYDATASTRUCT { // cds DWORD dwData; DWORD cbD ...

  2. spring源码分析(一)IoC、DI

    创建日期:2016.08.06 修改日期:2016.08.07 - 2016.08.12 交流QQ:992591601 参考书籍:<spring源码深度解析>.<spring技术内幕 ...

  3. Memcache分布式部署方案

    基础环境 其实基于PHP扩展的Memcache客户端实际上早已经实现,而且非常稳定.先解释一些名词,Memcache是danga.com的一个开源项目,可以类比于MySQL这样的服务,而PHP扩展的M ...

  4. Atitit.异步的实现模式attilax大总结

    Atitit.异步的实现模式attilax大总结 1.1. 函数回调(包括的future模式)1 1.2. 事件机制( 包括定时器 listeners 1 1.3. 中断机制1 1.4. 订阅机制 发 ...

  5. paip.python错误解决23

    paip.python错误解决 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net/attilax ...

  6. web 安全

    一.客户端脚本安全 (1)跨站脚本攻击(XSS): XSS攻击,通常指黑客通过“html注入” 篡改了网页,插入了恶意的脚本,从而在用户浏览网页的时候,控制用户浏览器的一种攻击. 最常见的XSS攻击就 ...

  7. 利用@property实现可控的属性操作

    利用@property实现可控的属性操作 Python中没有访问控制符, 不像java之类的 public class Person{ private int x public int getAge( ...

  8. Android中的Activity相关知识总结

    一.什么是Activity? 简单理解:Activity是Android组件中最基本也是最为常见用的四大组件之一.是一个与用户交互的系统模块,一个Activity通常就是一个单独的屏幕(页面), 它上 ...

  9. Netty学习一:基本知识

    1. Netty基础知识 1.1 Netty出现的原因 Java NIO 太难用,存在BUG(如Epoll-Bug) 基于第一点,大多数高性能服务器被C和C++盘踞 同样基于第一点,Java NIO编 ...

  10. MongoDB修改器的使用2

    1."$inc"的使用 主要用来增加数值,比如网站的访问量,点击量,流量等 db.games.insert({game:"pinball",user:" ...