题目:

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的更多相关文章

  1. [Swift]LeetCode214. 最短回文串 | Shortest Palindrome

    Given a string s, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  2. Leetcode之回溯法专题-131. 分割回文串(Palindrome Partitioning)

    Leetcode之回溯法专题-131. 分割回文串(Palindrome Partitioning) 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. ...

  3. 回文数字(Palindrome Number)

    总时间限制:1000ms 内存限制: 65536kB 描述 给出一系列非负整数,判断是否是一个回文数.回文数指的是正着写和倒着写相等的数. 输入 一行,一个01字符串. 输出 若干行,每行是一个非负整 ...

  4. [LeetCode] Shortest Palindrome 最短回文串

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  5. LeetCode 680. 验证回文字符串 Ⅱ(Valid Palindrome II) 1

    680. 验证回文字符串 Ⅱ 680. Valid Palindrome II 题目描述 给定一个非空字符串 s,最多删除一个字符.判断是否能成为回文字符串. 每日一算法2019/5/4Day 1Le ...

  6. LeetCode 131. 分割回文串(Palindrome Partitioning)

    题目描述 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab" 输出: [ ["aa" ...

  7. leetcode 125 验证回文字符串 Valid Palindrome

    验证回文字符串 C++ 思路就是先重新定义一个string ,先遍历第一遍,字符串统一小写,去除空格:然后遍历第二遍,首尾一一对应比较:时间复杂度O(n+n/2),空间O(n); class Solu ...

  8. [leetcode/lintcode 题解] 有效回文 II · Valid Palindrome II

    [题目描述] 给一个非空字符串 s,你最多可以删除一个字符.判断是否可以把它变成回文串. 在线评测地址: https://www.lintcode.com/problem/valid-palindro ...

  9. LeetCode Shortest Palindrome

    原题链接在这里:https://leetcode.com/problems/shortest-palindrome/ 题目: Given a string S, you are allowed to ...

随机推荐

  1. :after伪类+content内容生成

    :after伪类+content 清除浮动的影响 浮动元素会让此div的高度塌陷.如下例子: .box{padding:10px; background:gray;} .l{float:left;} ...

  2. ASP.NET Core 2.0 集成测试无法执行的问题

    问题表现: Microsoft.AspNetCore.Mvc.Razor.Compilation.CompilationFailedException : One or more compilatio ...

  3. 如何在RHEL7上搭建Samba服务实现Windows与Linux之间的文件共享

    如何在RHEL7上搭建Samba服务实现Windows与Linux之间的文件共享 实现环境:VMware workstations.RHEL7.0 第一步:配置网卡IP及yum软件仓库 命令:vim ...

  4. [转载] ZooKeeper实现分布式队列Queue

    转载自http://blog.fens.me/zookeeper-queue/ 让Hadoop跑在云端系列文章,介绍了如何整合虚拟化和Hadoop,让Hadoop集群跑在VPS虚拟主机上,通过云向用户 ...

  5. [收藏] Java源码阅读的真实体会

    收藏自http://www.iteye.com/topic/1113732 刚才在论坛不经意间,看到有关源码阅读的帖子.回想自己前几年,阅读源码那种兴奋和成就感(1),不禁又有一种激动. 源码阅读,我 ...

  6. [转载] Dubbo实现RPC调用使用入门

    转载自http://shiyanjun.cn/archives/341.html 使用Dubbo进行远程调用实现服务交互,它支持多种协议,如Hessian.HTTP.RMI.Memcached.Red ...

  7. 删除一个大表导致其他表Opening tables

  8. 查看oracle数据库里哪些语句耗时最长或者效率最低

    CPU: select * from (select v.sql_id, v.child_number, v.sql_text, v.elapsed_time, v.cpu_time, v.disk_ ...

  9. 完美解决--用VS中的Git做代码管理器,与他人共享代码

    1.创建代码仓库,这里说一下为什么要创建仓库,Git不能够作为源代码管理器,vs中自带的也只能够在本地进行管理,要和他们共享的话必须要有服务器端去存储代码,类似于SVN,它就有客户端和服务器端,这里推 ...

  10. Composer笔记

    安装 windows中安装Composer 一般来说,windows下安装composer有两种办法,一种是直接下载并运行Composer-Setup.exe,这种方法在中国似乎很难完成安装.另一种就 ...