题目:

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. mysql将字符串转化为数字

    我的字段为内容为数字,但是类型为字符串,需要使用CASE转换即可 SELECT MAX(CAST(C_id AS UNSIGNED)) INTO id 即查询出来最大的C_id,否则会按照字符串查询最 ...

  2. [array] leetCode-4-Median of Two Sorted Arrays-Hard

    leetCode-4-Median of Two Sorted Arrays-Hard descrition There are two sorted arrays nums1 and nums2 o ...

  3. android 人脸检测你一定会遇到的坑

    笔者今年做了一个和人脸有关的android产品,主要是获取摄像头返回的预览数据流,判断该数据流是否包含了人脸,有人脸时显示摄像头预览框,无人脸时摄像头预览框隐藏,看上去这个功能并不复杂,其实在开发过程 ...

  4. JS小游戏:贪吃蛇(附源码)

    javascript小游戏:贪吃蛇 此小游戏采用的是面向对象的思想,将蛇,食物,和游戏引擎分为3个对象来写的. 为方便下载,我把js写在了html中, 源码中暂时没有注释,等有空我在添加点注释吧. 游 ...

  5. (11.20)Java小知识!

      经过一段时间的学习,我也终于来到了Java语言的核心篇,也就是对象与类的学习,今天想要和大家分享的是关于类的小知识点. 1.类的声明: 类可以看成创建Java对象的模板.类亦可以理解成Java一种 ...

  6. maven学习之1

    最近用maven的时候各种出问题,打算系统的学习一下maven,搞明白依赖之类的. (一)创建工程: mvn archetype:generate 这样就可以根据提示来建立一个maven项目,常用的有 ...

  7. 2017EIS CTFwriteup

    EIS2017也就是2017年高校网络信息安全管理 运维挑战赛,全国一百多所高校参赛,侥幸拿了个地区三等奖,事先不知道理论赛占分比,不然就会是二等奖(吐槽),生活没有如果,下次努力吧. 比赛已经结束大 ...

  8. [转]the service mysql57 failed the most recent status[/br]mysql57 was not found解决办法

    转自:http://forums.mysql.com/read.php?169,622722,622877#msg-622877 安装完mysql5.7.12后想要stop或者restart都会出现以 ...

  9. 结合提供者模式解析Jenkins源码国际化的实现

    关键字:提供者模式,设计模式,github,gerrit,源码学习,jenkins,国际化,maven高级,maven插件 本篇文章的源码展示部分由于长度问题不会全部粘贴展示,或许只是直接提及,需要了 ...

  10. 如何用while循环 输出一个九九乘法表

    方法一 i = 1 while i < 10: k = 1 while k <= i: print('%d*%d=%2d '% (i,k,i*k),end='') #end='' 表示不换 ...