【回文】leetcode - Shortest Palindrome
题目:
Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrome you can find by performing this transformation.
For example:
Given "aacecaaa"
, return "aaacecaaa"
.
Given "abcd"
, return "dcbabcd"
.
分析:
利用manacher算法进行求解。时间复杂度O(n)。空间复杂度O(n).
- class Solution {
- public:
- string shortestPalindrome(string s) {
- if(s.size()<=1)
- return s;
- string str(s.size()*2+1,'#');
- for(int i=0,j=1;i<s.size();++i,j+=2)
- str[j]=s[i];
- int res=1,id=1,size=str.size();
- vector<int> p(size,1);
- p[1]=2;
- for(int i=2;i<=size/2;++i)
- {
- int maxright=p[id]+id-1;
- if(i>maxright)
- {
- //注意检查越界
- while(i - p[i]>=0 && str[i+p[i]]==str[i-p[i]])
- ++p[i];
- }
- else
- {
- int idleft=id-p[id]+1;
- int k=i-id,j=id-k,tmp=j-p[j]+1;//i和j关于id对称
- if(tmp>idleft)
- p[i]=p[j];
- else if(tmp<idleft)
- p[i]=p[id]-k;
- else
- {
- p[i]=p[j];
- while(i - p[i]>=0 && str[i+p[i]]==str[i-p[i]])
- ++p[i];
- }
- }
- if(p[i]+i>p[id]+id)
- id=i;
- if(i-p[i]+1==0)
- res=i;
- }
- if (res == s.size())
- return s;
- else
- {
- string tmp = string(s.begin() + res, s.end());
- reverse(tmp.begin(), tmp.end());
- return tmp + s;
- }
- }
- };
【回文】leetcode - Shortest Palindrome的更多相关文章
- [Swift]LeetCode214. 最短回文串 | Shortest Palindrome
Given a string s, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- Leetcode之回溯法专题-131. 分割回文串(Palindrome Partitioning)
Leetcode之回溯法专题-131. 分割回文串(Palindrome Partitioning) 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. ...
- 回文数字(Palindrome Number)
总时间限制:1000ms 内存限制: 65536kB 描述 给出一系列非负整数,判断是否是一个回文数.回文数指的是正着写和倒着写相等的数. 输入 一行,一个01字符串. 输出 若干行,每行是一个非负整 ...
- [LeetCode] Shortest Palindrome 最短回文串
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- LeetCode 680. 验证回文字符串 Ⅱ(Valid Palindrome II) 1
680. 验证回文字符串 Ⅱ 680. Valid Palindrome II 题目描述 给定一个非空字符串 s,最多删除一个字符.判断是否能成为回文字符串. 每日一算法2019/5/4Day 1Le ...
- LeetCode 131. 分割回文串(Palindrome Partitioning)
题目描述 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab" 输出: [ ["aa" ...
- leetcode 125 验证回文字符串 Valid Palindrome
验证回文字符串 C++ 思路就是先重新定义一个string ,先遍历第一遍,字符串统一小写,去除空格:然后遍历第二遍,首尾一一对应比较:时间复杂度O(n+n/2),空间O(n); class Solu ...
- [leetcode/lintcode 题解] 有效回文 II · Valid Palindrome II
[题目描述] 给一个非空字符串 s,你最多可以删除一个字符.判断是否可以把它变成回文串. 在线评测地址: https://www.lintcode.com/problem/valid-palindro ...
- LeetCode Shortest Palindrome
原题链接在这里:https://leetcode.com/problems/shortest-palindrome/ 题目: Given a string S, you are allowed to ...
随机推荐
- 获取 修改 CSS 样式
内联(style里的)样式 element.style.color element.style.getPropertyValue("color") 非内联样式 window.g ...
- HTML学习笔记 div布局及table布局案例 第三节 (原创)参考使用表
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- WebService--axis
axis WebService虽然现在已经很少使用,但是还是把它的配置过程写出来,开发环境jdk 1.6 服务端: 1,导入需要jar包,自行下载 2,创建WebService接口 public in ...
- 关于双11过后MATLAB许可过期问题的解决方案
在距离双11还有2个月之前,matlab会提示:Your MATLAB license will expire in 50 days --------------------------------- ...
- Scrum Meeting Alpha - 3
Scrum Meeting Alpha - 3 NewTeam 2017/10/27 地点:新主楼F座二楼 任务反馈 团队成员 完成任务 计划任务 安万贺 找到了几个开源项目,参考了API的包装方式, ...
- 六:在线工具网站,让你PC上要装的软件少一半!
记住这几个在线工具网站,让你PC上要装的软件少一半! 一.uzer.me——丰富的云端应用聚合 这个云平台将我们常用的Office系列软件.Adobe家族的系列软件……乃至CAD制图都整合在了云端,随 ...
- C#实现DirectShow技术开发准备
DirectShow组件在“C:\WINDOWS\system32”目录下的Quartz.dll动态库中,要使C#代码引用COM对象和接口,必须将COM类型库转换为.NET框架元数据,从而有效地创建一 ...
- HTML的用法
今天学习了HTMIL,标签.这个东西吧,没什么很难的,就是得多练多打.今天的一些个人心得: 标签:成对存在的名称 标签注意:1.标签名放在<> 2.标签成对存在的 3.结束标签有斜杠/ 例 ...
- LINQ学习系列-----1.3 扩展方法
这篇内容继续接着昨天的Lambda表达式的源码继续下去.昨天讲了Lambda表达式,此篇讲扩展方法,这两点都是Linq带来的新特性. 一.扩展方法介绍 废话不多说,先上源码截图: 上图中Ge ...
- 读Kafka Consumer源码
最近一直在关注阿里的一个开源项目:OpenMessaging OpenMessaging, which includes the establishment of industry guideline ...