题目:

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. css中单位px和em,rem的区别[转载]

    PX特点 1. IE无法调整那些使用px作为单位的字体大小: 2. 国外的大部分网站能够调整的原因在于其使用了em或rem作为字体单位: 3. Firefox能够调整px和em,rem,但是96%以上 ...

  2. Red Hat 7.0 DNS服务配置笔记

    先挂载镜像,然后配置yum,然后安装yum install -y bind 配置静态 IP.DNS就是他本身的IP地址. 修改DNS的配置文件,在后面加入区域配置信息.vim /etc/named.c ...

  3. SpringBoot下配置FreeMarker配置远程模版

    需求产生原因 要求在同一个接口中,根据不同的参数,返回不同的视图结果 所有的视图中的数据基本一致 要求页面能静态化,优化SEO 例如:A接口返回客户的信息 客户A在调用接口时,返回其个性化定制的页面A ...

  4. Python爬虫入门:Urllib库的基本使用

    1.分分钟扒一个网页下来 怎样扒网页呢?其实就是根据URL来获取它的网页信息,虽然我们在浏览器中看到的是一幅幅优美的画面,但是其实是由浏览器解释才呈现出来的,实质它 是一段HTML代码,加 JS.CS ...

  5. maven---settings.xml配置

    <?xml version="1.0" encoding="UTF-8"?> <settings xmlns="http://mav ...

  6. ubuntu小技巧(不定期更新)

    1.gedit打开windows保存的txt出现乱码 默认情况下,用ubuntu打开windows保存含有中文的txt文件时会出现乱码. 只需在终端运行以下两条命令则可解决. gsettings se ...

  7. jenkins+docker 持续构建非docker in docker jenkins docker svn maven

    工欲善其事必先利其器,为了解脱程序员的,我们程序员本身发明了很多好用的工具,通过各种工具的组合来达到我们想要的结果 本文采用jenkins docker svn maven作为相关工具,项目sprin ...

  8. 原生js绑定和解绑事件,兼容IE,FF,chrome

    主要是最近项目中用到了原生的js 解绑和绑定 事件  然后今天研究了一下,其实问题不大,不过要注意不要把单词写错了,今天我就找了好久单词写错了. 需求:当鼠标移上去以后,给Select加载元素,接着解 ...

  9. eclipse中hibernate和mybatis中xml配置文件的没有标签提醒解决方法

    当我们使用eclipse编写Mybatis或hibernate的xml文件时,面对众多标签的配置文件,却没有自动提醒,对于工作和学习都十分不方便. 之所以没有自动提醒,是因为dtd文件没有加载成功. ...

  10. Winform界面中实现菜单列表的动态个性化配置管理

    在我们一般的应用系统里面,由于系统是面向不同类型的用户,我们所看到的菜单会越来越多,多一点的甚至上百个,但是我们实际工作接触的菜单可能就是那么几个,那么对于这种庞大的菜单体系,寻找起来非常不便.因此对 ...