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.

题目的意思:输入一个字符串S,找出其最长回文串

用动态规划求解

决策变量:dp[i][j]记录从s[i]到s[j]组成的子串是否为回文。

      dp[i+1][j-1]     , 当s[i]与s[j]相等时

dp[i][j] =

      false      , 当s[i]与s[j]不相等时

单个字符是回文串,故要初始化对角线

紧邻的两个相同字符也是回文

时间复杂度O(n^2)

class Solution {
public:
string longestPalindrome(string s) {
int n = s.length(), startIndex = , maxLen = ;
// vector<vector<bool> > dp(n,vector<bool>(n,false));
bool dp[][] = {false};
for(int i = ; i < n; ++ i) dp[i][i] = true;
for(int i = ; i < n-; ++ i ){
if(s[i] == s[i+]){
dp[i][i+] = true;
startIndex= i;
maxLen = ;
}
}
for(int len = ; len <= n; ++len){
for(int i = ; i < n-len+; ++ i){
int j = i+len-;
if(s[i] == s[j] && dp[i+][j-]){
dp[i][j] = true;
startIndex =i;
maxLen = len;
}
}
}
return s.substr(startIndex,maxLen);
}
};

动态规划求解

利用Manacher 算法求解,时间复杂度为O(n)

可以参考http://www.felix021.com/blog/read.php?2040

http://leetcode.com/2011/11/longest-palindromic-substring-part-ii.html

string preProcess(string s){
int n = s.length();
if( n == ) return "^$";
string res="^";
for(int i = ; i < n; ++ i) res+="#"+string(,s[i]);
res+="#$";
return res;
} string longestPalindrome(string s){
string T = preProcess(s);
int n = T.length();
vector<int> p(n,);
int center = , radius = ,maxv = ;
for(int i = ; i < n-; ++ i){
p[i] = (radius > i) ? min(radius-i,p[*center-i]) : ;
while(T[i++p[i]] == T[i--p[i]]) p[i]++;
if(i+p[i] > radius){
center = i;
radius = i+p[i];
}
}
int maxLen = , centerIndex = ;
for(int i = ; i < n-; ++ i){
if(p[i] > maxLen){
maxLen = p[i];
centerIndex = i;
}
}
centerIndex = (centerIndex - -maxLen)/;
return s.substr(centerIndex,maxLen);
}

Manacher算法

  

Leetcode Longest Palindromic Substring的更多相关文章

  1. [LeetCode] Longest Palindromic Substring 最长回文串

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

  2. [LeetCode] Longest Palindromic Substring(manacher algorithm)

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

  3. C++ leetcode Longest Palindromic Substring

    明天就要上课了,再过几天又要见班主任汇报项目进程了,什么都没做的我竟然有一种迷之淡定,大概是想体验一波熬夜修仙的快乐了.不管怎么说,每天还是要水一篇博文,写一个LeetCode的题才圆满. 题目:Gi ...

  4. Leetcode: Longest Palindromic Substring && Summary: Palindrome

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

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

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

  6. Leetcode: Longest Palindromic Substring. java

    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. [LeetCode]Longest Palindromic Substring题解(动态规划)

    Longest Palindromic Substring: Given a string s, find the longest palindromic substring in s. You ma ...

  9. Leetcode:Longest Palindromic Substring分析和实现

    问题大意是在给定字符串中查找最长的回文子串,所谓的回文就是依据中间位置对称的字符串,比如abba,aba都是回文. 这个问题初一看,非常简单,但是会很快发现那些简单的思路都会带来O(n^3)级别的时间 ...

随机推荐

  1. JavaScript中getBoundingClientRect()方法详解

    获取浏览器滚动的高度: scrollTop=document.documentElement.scrollTop || document.body.scrollTop getBoundingClien ...

  2. 跟随 Web 标准探究DOM -- Node 与 Element 的遍历

    写在前面 这篇没有什么 WebKit 代码的分析,因为……没啥好分析的,在实现里无非就是树的(先序DFS)遍历而已,囧哈哈哈……在WebCore/dom/Node.h , WebCore/dom/Co ...

  3. soj 2013年 Nanjing Slection

    这样加边比STL快! 不明白为什么要+mod #include<iostream> #include<cstdio> #include<queue> #includ ...

  4. jquery 事件委托

    什么事件委托? DOM在为页面中的每个元素分派事件时,相应的元素一般都在事件冒泡阶段处理事件.在类似 body > div > a 这样的结构中,如果单击a元素,click事件会从a一直冒 ...

  5. struts2 国际化

    国际化概述: 软件国际化:一个软件根据来访者地区不同,显示不同语言. 国际化: * 必须有一组资源包: * 一组属性文件命名: 基本名称_语言(小写)_国家(大写).properties * 如:me ...

  6. sublime text3 输入中文的解决方法

    1. 下载我们需要的文件,打开终端 ,输入: git clone https://github.com/lyfeyaj/sublime-text-imfix.git 2. 将下载的文件解压之后,移到当 ...

  7. (2016弱校联盟十一专场10.3) B.Help the Princess!

    题目链接 宽搜一下就行. #include <iostream> #include<cstdio> #include<cstring> #include<qu ...

  8. 【XLL 框架库函数】 TempActiveRow/TempActiveRow12

    创建一个包含所有激活工作表行的 XLOPER/XLOPER12 LPXLOPER TempActiveRow(WORD row); LPXLOPER12 TempActiveRow12(ROW row ...

  9. Error:identifer “blockIdx” and __syncthreads() undefined

    #include "device_launch_parameters.h" for blockIdx #include "device_functions.h" ...

  10. EasyUI第一章Application之Basic CRUD(增删改查)

    先看效果图: 增加: 修改: 删除: 具体实现: html与js代码: @{ Layout = null; } <!DOCTYPE html> <html> <head& ...