LeetCode 5 Longest Palindromic Substring manacher算法,最长回文子序列,string.substr(start,len) 难度:2
https://leetcode.com/problems/longest-palindromic-substring/
manacher算法相关:http://blog.csdn.net/ywhorizen/article/details/6629268
class Solution {
public: string longestPalindrome(string s)
{
char ch[2001];int p[2001];
ch[2*s.size()] = 0;
for(int i = 0; i < 2 * s.size(); i++)
{
if((i & 1) == 1)ch[i] = s[(i>>1)];
else ch[i] = ' ';
}
int mx = 1,id = 0;
p[0] = 1;
int ss = 0,se = 0;
for(int i = 1; ch[i]; i++)
{
if(mx <= i)
{
p[i] = 1;
}
else
{
p[i] = min(p[id * 2 - i],mx - i);
}
while(ch[i + p[i]] == ch[i - p[i]] && i >= p[i])
{
p[i]++;
}
if(p[i] + i > mx)
{
id = i;
mx = p[i] + i;
}
int ts = (i - p[i] + 1)/2;
int te = (i + p[i] - 2)/2;
if(te - ts >se - ss){
se = te;ss = ts;
}
}
return s.substr(ss,se -ss + 1);
}
};
LeetCode 5 Longest Palindromic Substring manacher算法,最长回文子序列,string.substr(start,len) 难度:2的更多相关文章
- leetcode 5 :Longest Palindromic Substring 找出最长回文子串
题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...
- leetcode:Longest Palindromic Substring(求最大的回文字符串)
Question:Given a string S, find the longest palindromic substring in S. You may assume that the maxi ...
- leetcode 5 Longest Palindromic Substring(Manacher算法求最长回文串)
应用一下manacher算法就可以O(n)求出结果了.可以参考hdu3068 substr(start,length)函数是这样用的: substr 方法 返回一个从指定位置开始,并具有指定长度的子字 ...
- Manacher算法----最长回文子串
题目描述 给定一个字符串,求它的最长回文子串的长度. 分析与解法 最容易想到的办法是枚举所有的子串,分别判断其是否为回文.这个思路初看起来是正确的,但却做了很多无用功,如果一个长的子串包含另一个短一些 ...
- Manacher算法——最长回文子串
一.相关介绍 最长回文子串 s="abcd", 最长回文长度为 1,即a或b或c或d s="ababa", 最长回文长度为 5,即ababa s="a ...
- Manacher算法,最长回文串
给你10000长度字符串,然你求最长回文字串,输出长度,暴力算法肯定超时 #include <iostream> #include <string> #include < ...
- 最长回文子串-LeetCode 5 Longest Palindromic Substring
题目描述 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法
LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法 题记 本文是LeetCode题库的第五题,没想到做这些题的速度会这么慢,工作之 ...
- Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法)
Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法) Given a string s, find the longest pal ...
随机推荐
- 复旦大学2015--2016学年第二学期(15级)高等代数II期末考试第六大题解答
六.(本题10分) 设 $n$ 阶复方阵 $A$ 的特征多项式为 $f(\lambda)$, 复系数多项式 $g(\lambda)$ 满足 $(f(g(\lambda)),g'(\lambda))= ...
- html热力图的操作(点击图片的不同位置操作不同的事件)适合说明文档
页面核心代码 <div class="first_top"> <div class="back"> <img src=" ...
- UVA 10795 新汉诺塔问题
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- linux 下 apache相关;启动、停止、重启命令;配置文件位置等等
linux 下 apache启动.停止.重启命 基本的操作方法: 本文假设你的apahce安装目录为/usr/local/apache2,这些方法适合任何情况 apahce启动命令: 推荐/usr/l ...
- a error of misunderstanding
Last summer vacation, a classmate contacted with me and we finished a intelligent car project with i ...
- T-SQL编程练习(带注释)
use test; GO /*创建自定义函数的格式: * create function 函数名(参数 数据类型) * returns 返回数据类型 as * begin * 代码 * end */ ...
- QAbstractItemView::setRootIndex(const QModelIndex & index) 失效
问题: 在逻辑中使用了, QAbstractItemView::setRootIndex(const QModelIndex & index), 第一次设置生效, view 进入了model ...
- 回调函数及数组中sort()方法实现排序的原理
1.回调函数:把一个方法A当一个参数值传递到另外一个函数B中,在B执行的过程当中我们随时根据需求让A方法执行: 什么是回调 :它是异步编程基本的方法,需要异步处理的时候一般采用后续传递的方式,将后 ...
- iOS - Push 通知推送
1.UserNotifications 通知是 App 用来和用户交流的一种方式,特别是当 App 并没有在前台运行的时候.通知,正如它的名称所强调的,被用作向用户'通知'一个事件,或者仅仅向用户提示 ...
- iOS - iOS 应用
1.Xcode 项目属性 Product Name 软件名称.产品名称.项目名称 Organization Name 公司名称.组织名称 Organization Identifier 公司的唯一标识 ...