Leetcode Longest Palindromic Substring

时间:2022-12-19 22:10:54

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. &lbrack;LeetCode&rsqb; Longest Palindromic Substring 最长回文串

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

  2. &lbrack;LeetCode&rsqb; 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&plus;&plus; leetcode Longest Palindromic Substring

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

  4. Leetcode&colon; Longest Palindromic Substring &amp&semi;&amp&semi; Summary&colon; Palindrome

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

  5. LeetCode&colon;Longest Palindromic Substring 最长回文子串

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

  6. Leetcode&colon; Longest Palindromic Substring&period; 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. &lbrack;LeetCode&rsqb;Longest Palindromic Substring题解&lpar;动态规划&rpar;

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

  9. Leetcode&colon;Longest Palindromic Substring分析和实现

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

随机推荐

  1. 整合ssh model &dollar;&dollar;&lowbar;javassist&lowbar;13 cannot be cast to javassist&period;util&period;proxy&period;Proxy

    经goole * 发现是 javassit 包冲突 项目使用的是maven 检查依赖包

  2. JavaScript基础12——js的方法重载

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. Unity摄像机

    把相机做为人物的子对象,就可以制作: 1.第1人称摄像机:把摄像机摆在眼睛前面 2.第3人称摄像机:把摄像机摆在人后上面 Clear Flags: http://www.haogongju.net/a ...

  4. Mac 版 QQ 可直接访问 iPhone 的相册 ?!

    在QQ的聊天窗口中,点击 发送图片 的按钮,会有两个选择项,其中一个就是 从iPhone相册中选取 ,如图 点击  从iPhone相册中选取 后,iPhone上的QQ会收到一条消息 “请选择要上传的照 ...

  5. redis整合spring &commat;Bean写法

    jedis是一款java连接redis的客户端,spring基于jedis进行了封装,提供了简洁的操作redis的方法. 使用maven进行管理jar包之间的依赖: <dependency&gt ...

  6. CODE大全浅谈谷歌adsense与PIN码

    我的博客:CODE大全:www.codedq.net:业余草:www.xttblog.com:爱分享:www.ndislwf.com或ifxvn.com. 近期由于校园招聘笔试和面试等诸多忙碌时间,博 ...

  7. 【书摘】一种基于Git的版本管理方案

    本篇摘录自<前端工程化体系设计与实践>一书,笔者认为是一套相对合理的方案,建议团队可以根据实际情况进行调整并增加协作命名规范. master分支--主分支 存储已发布版本的源码,不能在此分 ...

  8. 如何查找SHELL的进程号并杀死

    一.shell查找进程并杀死 #!/bin/sh tomcat_id=`ps -ef | grep tomcat | grep -v "grep" | awk '{print $2 ...

  9. python中的&ast;号

    from:https://www.douban.com/note/231603832/ 传递实参和定义形参(所谓实参就是调用函数时传入的参数,形参则是定义函数是定义的参数)的时候,你还可以使用两个特殊 ...

  10. Linux下grub&period;cnf详解

    grub.conf跟系统启动项有关,对于重置密码.来说小case... 1.介绍    在Red Hat Linux7.2之后,默认的引导加载程序从LTLO变为GRUB.这个引导加载程序使用户能够选择 ...