题目:

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. H5常用代码:适配方案3

    在H5项目中有一种常见的宣传页,就是那种整屏整屏的,带着炫丽进场动画的移动宣传页,不仅是一种欣赏也起到了很大宣传作用. 对于这种整屏的适配,前面通过视口的兼容处理也是可以做到的,但是在窄屏下会在上下有 ...

  2. 《Wireshark数据包分析实战》 - http背后,tcp/ip抓包分析

    作为网络开发人员,使用fiddler无疑是最好的选择,方便易用功能强. 但是什么作为爱学习的同学,是不应该止步于http协议的,学习wireshark则可以满足这方面的需求.wireshark作为抓取 ...

  3. salesforce 零基础学习(十八)WorkFlow介绍及用法

    说起workflow大家肯定都不陌生,这里简单介绍一下salesforce中什么情况下使用workflow. 当你分配许多任务,定期发送电子邮件,记录修改时,可以通过自动配置workflow来完成以上 ...

  4. CCNA网络工程师学习进程(1)网络的基本概述

        在互联网快速发展的今天,了解互联网成为一项必须的技能,因此在学习编程之余学习如何配置网络还是很有必要的. 本系列博客计划分为三个部分,包括思科CCNA.CCNP和华为的网络工程师认证有关的知识 ...

  5. 查看Query Plan

    在执行一个查询语句时,查询优化器编译查询语句,产生一个足够好的Compiled Plan,将其缓存到plan cache中.Compiled plan是基于batch的,如果一个batch含有多个qu ...

  6. KlayGE 4.4中渲染的改进(一):只需要SM3的TBDR

    转载请注明出处为KlayGE游戏引擎,本文的永久链接为http://www.klayge.org/?p=2736 KlayGE从4.0开始引入deferred rendering层(DR),并且这几个 ...

  7. sys.dm_db_wait_stats

    sys.dm_db_wait_stats 返回在操作期间执行的线程所遇到的所有等待的相关信息. 可以使用此聚合视图来诊断 Azure SQL Database 以及特定查询和批处理的性能问题. 执行查 ...

  8. BOM之navigator、history、screen对象

    navigator对象 [定义] navigator已经成为识别客户端浏览器的事实标准.下表中列出存在于所有浏览器的属性和方法 [检测插件] 检测浏览器插件是一种最常见的检测例程. [1]对于非IE浏 ...

  9. Android线程处理之Handler

    上一篇已经简单为大家介绍了一下关于Handler的使用,本篇我们就一起再来探讨一下Handler的高级使用,上一篇我们仅仅是简单的进行UI界面的更新,本篇我们来一起探讨一下如何把子线程的数据发送到主线 ...

  10. How do annotations work internally--转

    原文地址:http://stackoverflow.com/questions/18189980/how-do-annotations-work-internally The first main d ...