《LeetBook》leetcode题解(5):Longest Palindromic [M]——回文串判断
我现在在做一个叫《leetbook》的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看
书的地址:https://hk029.gitbooks.io/leetbook/
005.Longest Palindromic [M]
- 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);}
};
转载于:https://www.cnblogs.com/voidsky/p/5490817.html
《LeetBook》leetcode题解(5):Longest Palindromic [M]——回文串判断相关推荐
- C#LeetCode刷题之#409-最长回文串(Longest Palindrome)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3788 访问. 给定一个包含大写字母和小写字母的字符串,找到通过这 ...
- leetcode 214. Shortest Palindrome | 214. 最短回文串(Java)
题目 https://leetcode.com/problems/shortest-palindrome/ 题解 看了 Related Topics - Rolling Hash 下的相关题目,看到了 ...
- LeetCode简单题之最长回文串
题目 给定一个包含大写字母和小写字母的字符串 s ,返回 通过这些字母构造成的 最长的回文串 . 在构造过程中,请注意 区分大小写 .比如 "Aa" 不能当做一个回文字符串. 示例 ...
- UVa 11027 - Palindromic Permutation 回文串,组合加搜索
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=s ...
- C#LeetCode刷题之#125-验证回文串(Valid Palindrome)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3899 访问. 给定一个字符串,验证它是否是回文串,只考虑字母和数 ...
- LeetCode解题记录(409)——最长回文串
LeetCode解题记录--最长回文串 题目描述 示例 题目理解 解题思路 题目描述 给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串. 在构造过程中,请注意区分大小写.比 ...
- leetcode 探索 数组和字符串 验证回文串
问题 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: "A man, a plan, ...
- 回文串判断(string类:反转reverse)
2029-Palindromes _easy version Problem Description "回文串"是一个正读和反读都一样的字符串,比如"level" ...
- 链表回文串判断 老酒装新壶 双指针加链表反转(代码好记易写)
解题思路 使用双指针slow,fast对链表进行遍历,当fast走到最后(链表长度为偶数slow为中间两个右边的,长度为奇数为链表的中间元素),slow走到中间(链表长度为偶数slow为中间两个右边的 ...
最新文章
- RHEL5系统 sendmail+qpopper 架设简易邮件服务器
- udhcpd解析域名_Linux作DHCP服务器之三3.dhcpd.leasesdhcpd.leases是DHCP客户.doc
- Git复习(二)之远程仓库、注册GitHub账号、SSH警告、使用GitHub
- 堆(heap)和栈(stack)
- c 引用mysql报错_安装TPCC-MySQL报错
- 计算机有关的文献检索题目,文献检索第二次计算机检索实习题目(2016.4.10).doc
- 漫谈数据挖掘从入门到进阶
- Power BI Desktop交互式和弦图
- python里order什么意思_python中OrderedDict的使用方法详解
- react系列之isMounted is an Antipattern
- 【数据分享】某产品付费用户数据
- lvs nginx-proxy nginx 取用户真实IP
- 医院药房管理系统 php,his 源码 医院管理系统
- Pr2023(Premiere Pro)中文版软件下载「附带安装教程」
- AutoCAD 2019 for Mac 汉化安装手册
- 方向余弦,向量夹角,向量的投影
- 借用传感器用计算机测速度题,图甲为利用距离传感器发出和接受超声波信号检测汽车速度的示意图.距离传感器发出的超声波遇到物......
- Java打印乘法口诀表(任何数)
- 计算机病毒及其防治技术期末复习
- 阿里云服务器怎么样可以实现 frp 内网穿透
热门文章
- flink 三种时间机制_Flink的时间类型和watermark机制
- leetcode —— 15. 三数之和
- Java出现No enclosing instance of type Test is accessible. Must qualify the allocation with an enclosin
- CV之Haar特征描述算子-人脸检测
- edu汇编语言——实训课程
- Python GUI界面编程初步 02 - Tkinter基础
- 蔡高厅老师 - 高等数学阅读笔记 - 14 定积分 -定积分的换元法 - 广义积分和伽马函数(65、66、67)
- 【中级】【后台】 微信小程序 - 腾讯云 - wafer2 - PHP - DEMO - 003 - 源码分析 - 03 - 腾讯后台初始化 和 CodeIgniter
- Samsung x210 Android makefile 的一些配置实例问题
- 嵌入式linux 零基础-实践起步 --- 001 --- linux的版本