Leetcode Longest Palindromic Substring
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的更多相关文章
- [LeetCode] Longest Palindromic Substring 最长回文串
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- [LeetCode] Longest Palindromic Substring(manacher algorithm)
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- C++ leetcode Longest Palindromic Substring
明天就要上课了,再过几天又要见班主任汇报项目进程了,什么都没做的我竟然有一种迷之淡定,大概是想体验一波熬夜修仙的快乐了.不管怎么说,每天还是要水一篇博文,写一个LeetCode的题才圆满. 题目:Gi ...
- Leetcode: Longest Palindromic Substring && Summary: Palindrome
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- LeetCode:Longest Palindromic Substring 最长回文子串
题目链接 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- Leetcode: Longest Palindromic Substring. java
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- LeetCode——Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- [LeetCode]Longest Palindromic Substring题解(动态规划)
Longest Palindromic Substring: Given a string s, find the longest palindromic substring in s. You ma ...
- Leetcode:Longest Palindromic Substring分析和实现
问题大意是在给定字符串中查找最长的回文子串,所谓的回文就是依据中间位置对称的字符串,比如abba,aba都是回文. 这个问题初一看,非常简单,但是会很快发现那些简单的思路都会带来O(n^3)级别的时间 ...
随机推荐
- centos 软件包安装与管理
http://rpmfind.net/linux/RPM/index.html http://pkgs.org/ https://sourceforge.net/
- Druid初步学习
Druid是一个JDBC组件,它包括三部分: DruidDriver 代理Driver,能够提供基于Filter-Chain模式的插件体系. DruidDataSource 高效可管理的数据库连接池 ...
- Npoi导入导出Excel操作
之前公司的一个物流商系统需要实现对订单的批量导入和导出,翻阅了一些资料,最后考虑使用NPOI实现这个需求. 在winform上面实现excel操作:http://www.cnblogs.com/Cal ...
- 关于学习JavaScript 的 高三编程 一些心得
面对JS 问题来说,很多的细节问题以及 弱类型转换的问题,往往会成为学习js 路上的一个阻碍. 那么问题来了,今天我看到的是 高三 里面的 基本概念的 语法问题. 直奔主题.(还是帖代码先) sw ...
- hdu4790 Just Random (数学?)
acm.hdu.edu.cn/showproblem.php?pid=4790 题意:x随机取a~b,y随机取c~d,求(x+y)mod p = m 的概率.(结果用分数表示) 题解: 数学概率题,运 ...
- 微信 5.3 for iPhone已放出 微信iphone版更新下载
就在几个小时前,微信发布了更新,本次只放出微信 5.3 for iPhone,距离发布微信5.3内测版也就几天时间.和往常一样微信iphone版先发布,微信android版延后发布,微信看重的是ios ...
- Ionic常用命令行解释
原文链接 安装ionic npm install -g ionic 更新www/lib/ionic 目录的文件,如有项目中有bower,此命令会运行bower update ionic, 否则则会从C ...
- MVC前台页面做登录验证
最近接触了一个电商平台的前台页面,需要做一个登录验证,具体情况是:当用户想要看自己的订单.积分等等信息,就需要用户登录之后才能查询,那么在MVC项目中我们应该怎么做这个前台的验证呢? 1.我在Cont ...
- 使用eclipse+fiddler+微信web开发者工具调试本地微信页面
前面已经说了调试服务器上的微信页面,放链接:http://www.cnblogs.com/Gabriel-Wei/p/5977850.html 还有fiddler调试链接:http://www.cnb ...
- iOS10 SiriKit QQ适配详解
原文连接 1. 概述 苹果在 iOS10 开放了 SiriKit 接口给第三方应用.目前,QQ已经率先适配了 Siri 的发消息和打电话功能.这意味着在 iOS10 中你可以直接告诉 Siri 让它帮 ...