题目:

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. Windows 配置 allure report 环境

    1:配置Java环境(运行allure 需要) 2:安装powershell 3:安装scoop方法 :运行 powershell 输入 : iex (new-object net.webclient ...

  2. Java 核心内容相关面试题【1】

    1.什么是 transient 变量? transient 变量是指不会被序列化的变量. 2.什么是同步(synchronization)? 在多线程环境中,同步是指控制多个线程访问共享资源的方式.没 ...

  3. 使用Docker跑MySQL 作为Django的存储后端

    Docker的好处不科普了,用过的都说好. 不想污染自己开发机器上的文件环境,本萌新使用Docker运行Mysql,Redis来作为Django的存储后端和缓存. 在第一次安装过程中,我遇到了一些问题 ...

  4. 一个可扩展的深度学习框架的Python实现(仿keras接口)

    一个可扩展的深度学习框架的Python实现(仿keras接口) 动机 keras是一种非常优秀的深度学习框架,其具有较好的易用性,可扩展性.keras的接口设计非常优雅,使用起来非常方便.在这里,我将 ...

  5. 《java.util.concurrent 包源码阅读》15 线程池系列之ScheduledThreadPoolExecutor 第二部分

    这篇文章主要说说DelayedWorkQueue. 在ScheduledThreadPoolExecutor使用DelayedWorkQueue来存放要执行的任务,因为这些任务是带有延迟的,而每次执行 ...

  6. 《java.util.concurrent 包源码阅读》19 PriorityBlockingQueue

    前面讲ScheduledThreadPoolExecutor曾经重点讲到了DelayedWorkQueue,这里说的PriorityBlockingQueue其实是DelayedWorkQueue的简 ...

  7. 【笔记】web 的回流与重绘及优化

    最近看了幕课网 web 前端性能优化的课程,其中说到了浏览器的回流(reflow) 及 重绘(repaint).觉得以后面试或许会被问到所以做一下笔记: 课程从回流及重绘这两个点延伸出了一个知识点就是 ...

  8. LINQ学习系列-----2.2 迭代器

    在学习本篇迭代器之前,强烈建议可以先学习一位具有工匠精神博主的文章,链接如下: 农码一生---先说IEnumerable,我们每天用的foreach你真的懂它吗? 本篇文章,是在该博主博文的基础上再次 ...

  9. C语言之阶乘

    #include<stdio.h>#include<stdlib.h>#include<time.h>int main(){ int num,i,result=1; ...

  10. Django框架的安装

    下载Django框架 创建一个django项目 在E盘Mysite文件夹下创建了一个django项目叫mysite 当前文件夹下会产生一个mysite的文件夹,目录结构如下: manage.py -- ...