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)级别的时间 ...
随机推荐
- c# winform 动态画矩形 矩形大小可以拖动
http://jhlong12345.blog.163.com/blog/static/1230631292015544450189/# 结合上一篇,继续 矩形大小的调整 还有小bug,思路有了 ...
- Ubuntu下的MySQL安装
<1>安装mysql-server sudo apt-get update sudo apt-get install mysql-server mysql-client <2> ...
- fedora23的打印服务
cups: common unix printing system. 是通用的打印服务. whatever 不管什么; whichever: 不管哪个 可以使用 http://localhost:63 ...
- sql中的!=判断的注意事项
sql查询中where过滤条件为某字段 colName='xx'时一般不会出什么问题, 但如果想达到不为xx的时候就要注意了,用colName!= 'xx'可能就有问题了,因为该字段可能为空,为nul ...
- Bash 中同名的内部命令和外部命令
昨天有个人在 bug-bash 上问:为什么 [ --help 没有输出帮助信息.有人回答他了,原因是 coreutils 提供的 [ 命令才接受 --help 选项,Bash 自己的 [ 命令不接受 ...
- win7提示“User Profile Service服务未能登录”
注:本文由Colin撰写,版权所有!转载请注明原文地址,谢谢合作! 最近,有个同事打电话告诉我说他的用户名无法登陆到系统,提示“User Profile Service服务未能登录,无法加载用户配置文 ...
- Linux设置Memcached开机启动
Memcached开机启动方式 方法一: 在 /etc/rc.d/rc.local 文件中追加启动命令 /usr/local/memcached/bin/memcached -u root -d - ...
- 【Alpha版本】 第二天 11.8
一.站立式会议照片: 二.项目燃尽图: 三.项目进展: 成 员 昨天完成任务 今天完成任务 明天要做任务 问题困难 心得体会 胡泽善 我要招聘详情的展示 注册界面的实现 填写招聘时用户填写各个日期到可 ...
- JDK source 之 LinkedHashMap原理浅谈
注:本文参考JDK1.7.0_45源码. LinkedHashMap是基于HashMap实现的数据结构,与HashMap主要的不同为每个Entry是使用双向链表实现的,并且提供了根据访问顺序进行排序的 ...
- C#高级编程笔记 Delegate 的粗浅理解 2016年9月 13日
Delegate [重中之重] 委托 定义一:(参考)http://www.cnblogs.com/zhangchenliang/archive/2012/09/19/2694430.html 完全可 ...