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. URL Schemes

    APP 被唤醒离不开对URL Schemes的认知. 苹果选择沙盒来保障用户的隐私和安全,但沙盒也阻碍了应用间合理的信息共享,于是有了 URL Schemes 这个解决办法. URL Schemes ...

  2. ubuntu常用命令

    <一> 安装文件 一.deb包的安装方式 sudo dpkg -i *.deb 二.编译安装方式 建立编译环境sudo apt-get install build-essential 到源 ...

  3. 2Struts2基础----青软S2SH(笔记)

  4. JAVA关键字与保留字说明及使用

    1.abstract 2.boolean 3.break 4.byte 5.case 6.catch 7.char 8.class 9.continue 10.default 11.do 12.dou ...

  5. H5案例分享:使用JS判断客户端、浏览器、操作系统类型

    使用JS判断客户端.浏览器.操作系统类型 一.JS判断客户端类型 JS判断客户端是否是iOS或者Android手机移动端 通过判断浏览器的userAgent,用正则来判断手机是否是ios和Androi ...

  6. Lua手动编译姿势

    LUA-5.3.3.tar.gz Lua源码+链接2016年5月30日更新 手动编译姿势: 已经装有VS2010 使用VS自带的 cl.exe以及 VS命令簿 打开文件地址 运行自己的bat文件 my ...

  7. python3 黑板客爬虫闯关游戏(二)

    第二关猜登录密码,需要用到urllib.request和urllib.parse 也很简单,给代码 import urllib.request as ur import urllib.parse as ...

  8. Java GridBagLayout 简单使用

    这里只介绍了很基础布局构建及使用,主要是关于 GridBagLayout. 首先整套流程大概是, 声明一个 GridBagLayout 对象 private GridBagLayout gridBag ...

  9. 使用EmBitz开发STM32项目的环境配置

    一.EmBitz软件获取与安装 1.EmBitz软件的获取 EmBitz原名Em::Blocks,是基于Code::Blocks开发的,面向嵌入式的C/C++集成开发环境.支持J-Link和ST-Li ...

  10. HTML登录注册界面怎么制作?

    在没有学习CSS样式的前提下,是如何做一个简单的注册界面的. 一.表单标签(form) 首先我们先写一个<form></form>的标签,form标签属于表单标签,通常我们的登 ...