【回文】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 ...
随机推荐
- ECMAScript arguments 对象(摘自W3C)
arguments 对象 在函数代码中,使用特殊对象 arguments,开发者无需明确指出参数名,就能访问它们. 例如,在函数 sayHi() 中,第一个参数是 message.用 argument ...
- ThreadLocal 原理解析
1.对Thread local 理解 ThreadLocal 是为了解决线程间同步而创建的一个新的思路.简单来说就是每个线程都保存一个变量副本. 如果在Thread 内部定义一个field变量,也可以 ...
- 动态代理:JDK动态代理和CGLIB代理的区别
代理模式:代理类和被代理类实现共同的接口(或继承),代理类中存有指向被代理类的索引,实际执行时通过调用代理类的方法.实际执行的是被代理类的方法. 而AOP,是通过动态代理实现的. 一.简单来说: JD ...
- 通过扩大IE使用内存,解决skyline在IE下模型不能加载的方法
环境:skyline TerraExploere 6.6.1,win10 专业版 64位,ie 11 情况描述:在ie下浏览三维场景,ie占用内存不断增大并且内存占用固定在一个最高范围内,三维场景中部 ...
- [Intel Edison开发板] 06、Edison开发在linux中烧写、配置、搭建开发环境
1.前言 linux上烧写.配置.搭建Edison环境,千万不要用默认的setup tool for ubuntu!!! (即使,你用的就是ubuntu) 因为,其默认的工具会从一个坏链接下载配置文件 ...
- 【3】测试搭建成功的单机hadoop环境
1.关闭防火墙service iptables stop,(已经设置开机关闭的忽略) 2.进入hadoop目录,修改hadoop配置文件(4个) core-site.xml(核心配置,fs.defau ...
- 《天书夜读:从汇编语言到windows内核编程》十一 用C++编写内核程序
---恢复内容开始--- 1) C++的"高级"特性,是它的优点也是它的缺点,微软对于使用C++写内核程序即不推崇也不排斥,使用C++写驱动需注意: a)New等操作符不能直接使用 ...
- eclipse中Maven工程使用Tomcat7以上插件
Maven中使用tomcat:run命令默认是使用Tomcat6的版本, 现在要用到Tomcat7以上的版本,在eclipse的Maven工程中配置如下 第一步:在项目的pom里面加入如下配置: 官网 ...
- asp.net core 实现一个简单的仓储
一直有自己写个框架的想法,但是一直没有行动起来,最近比较闲,正好可以开工了. 现在已经完成了两部分.1.一个简单仓储,实现使用的是ef 2.IOC部分,这里是把内置的ioc替换成了aotofac,这部 ...
- 本地git部署web连接azure的git存储库
本地git部署web 创建本地存储仓库 输入以下命令创建git本地仓库(会在当前目录下生产一个.git的目录) git init 然后提交内容 在git仓库所在的目录下存放好需要的网页文件 将文 ...