《LeetBook》leetcode题解(5):Longest Palindromic [M]——回文串判断
我现在在做一个叫《leetbook》的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看
书的地址:https://hk029.gitbooks.io/leetbook/
005.Longest Palindromic [M]
题目
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.
思路
可以用的算法有改良KMP还有manacher(马拉车)算法,毫无疑问,manacher算法是专门用来解决最长子串问题的,也是最简便的。关于这个算法可以看: Manacher算法
class Solution {
public:
string longestPalindrome(string s) {
//manacher
int bound = 0;
int id,max_pos=0;
int new_len = 2*s.length()+2;
vector<int> P(new_len,1);
string new_str(new_len-1,'#');
//生成新串,把所有的字符串通过’#’扩展成奇数
for(int i = 0;i < s.length();i++)
{
new_str[2*i+1] = s[i];
}
new_str = '$'+new_str +='\0'; //防止越界
//manacher算法
for(int i=1;i < new_len; i++)
{
if(i < bound)
{
P[i] = min(bound-i,P[2*id-i]); //如果在范围内,找对称面的P[id-(i-id)]和max_pos-i的最小值
}
while(new_str[i-P[i]] == new_str[i+P[i]])//查找以这个字符为中心的回文串
{
P[i]++;
}
//更新id和bound
if(i+P[i] > bound)
{
bound = i+P[i];
id = i;
}
max_pos = P[i] > P[max_pos]?i:max_pos;
}
int len = P[max_pos]-1;
int start = (max_pos-P[max_pos])/2;
return s.substr(start,len);
}
};
《LeetBook》leetcode题解(5):Longest Palindromic [M]——回文串判断的更多相关文章
- LeetCode 5. Longest Palindromic Substring & 回文字符串
Longest Palindromic Substring 回文这种简单的问题,在C里面印象很深啊.希望能一次过. 写的时候才想到有两种情况: 454(奇数位) 4554(偶数位) 第1次提交 cla ...
- 链表回文串判断&&链式A+B
有段时间没有练习了,链表回文串判断用到了栈.链式A+B将没有的项用0补充.链表有没有头节点,及结点和链表的区别,即pNode和pHead. //#include<iostream> //u ...
- 链表回文串判断&&链式A+B
有段时间没有练习了,链表回文串判断用到了栈.链式A+B将没有的项用0补充.链表有没有头节点,及结点和链表的区别,即pNode和pHead. //#include<iostream> //u ...
- C#LeetCode刷题之#409-最长回文串(Longest Palindrome)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3788 访问. 给定一个包含大写字母和小写字母的字符串,找到通过这 ...
- leetcode 每日签到 409. 最长回文串
题目: 最长回文串 给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串. 在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串. 注意: ...
- C#LeetCode刷题之#125-验证回文串(Valid Palindrome)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3899 访问. 给定一个字符串,验证它是否是回文串,只考虑字母和数 ...
- LeetCode(125):验证回文串
Easy! 题目描述: 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: "A man, ...
- leetcode刷题1--动态规划法回文串2
题目是: Given a string s,partition s such that every substring of the partition is a palindrome Return ...
- Openjudge-计算概论(A)-回文串判断
描述: 任意给定一个非空的字符串,判断其是否是回文串.回文串是指正向看和反向看均相等的串,如AbcDcbA和cDDc.如果是回文串,则输出1,否则,输出0 输入长度不小于1不大于100的字符串输出如果 ...
随机推荐
- 对/proc/cpuinfo文件下的各个参数的说明及实践
我们通常要检查系统的cpu的相关信息,之前在进行查看cpu的信息的时候,我最长用的方式 是,直接将/etc/cpuinfo下的所有的内容进行显示,然后通过对全部文件的查看,来提取我们需要的信息,虽然查 ...
- MFC中的一般经验之谈3
Window消息可以分为三类:(1)标准Window消息(CWnd子类处理),(2)控制通知消息(CWnd子类处理),(3)命令消息(应用中的5类都可以).所有派生自CCmdObjec对象的类都可以处 ...
- [leetcode] 7. Binary Tree Level Order Traversal II
这次相对来讲复杂点,题目如下: Given a binary tree, return the bottom-up level order traversal of its nodes' values ...
- Linux Socket - 内核非阻塞功能
select 函数 int select(int maxfdp,fd_set *readfds,fd_set *writefds,fd_set *errorfds,struct timeval*tim ...
- sqlserver数据库存储汉字出现?
问题:有些相对复杂的汉字在数据库里会变成? 解决办法:原来数据类型是varchar,将数据类型修改为nvarchar
- 网站图标ICO
效果图: 代码: 1 2 3 4 5 6 <head> ... <link rel="shortcut icon" href="/favicon.i ...
- swagger 在本地正常调试 发布后出现500 : {"Message":"出现错误。"}
点击项目属性 勾上xml 解决
- django drf unique_together和UniqueTogetherValidator
联合唯一可以使用django中的unique_together,和DRF中的UniqueTogetherValidator->https://www.django-rest-framework. ...
- python中的内置函数,递归,递归文件显示(二),二分法
1.部分内置函数 repr()显示出字符串的官方表示形式,返回一个对象的string形式 # repr 就是原封不动的输出, 引号和转义字符都不起作用 print(repr('大家好,\n \t我叫周 ...
- Windows安装Node.js报错:2503、2502的解决方法
以管理员身份用msiexec安装 1.以管理员身份运行cmd命令 (Win + X, A) 以管理员身份运行cmd 2.cd到自己msi路径 用msiexec安装 用msiexec安装nodejs