【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"
.
Credits:
Special thanks to @ifanchu for adding this problem and creating all test cases. Thanks to @Freezen for additional test cases.
转载自陆草纯。
首先确认一点基本知识,如果某个字符串str是回文的,那么str == reverse(str)
因此,将s逆转之后拼接在s后面,即news=s+reverse(s),该新字符串news首尾相同的部分,即为s中以s[0]为起始的最长回文子串pres
只不过这里我不用上述的遍历来做,而用类似KMP算法求next数组来做。
在KMP算法中求next数组就是s自我匹配的过程,next[i]的值就表示s[i]之前有几个元素是与s开头元素相同的。
因此,next[news.size()]的值就表示news中首尾相同的部分的长度。接下来就好做了。
注意:当next[news.size()]的值大于s.size()时,说明重复部分贯穿了s与reverse(s),应该修正为next[news.size()]+1-s.size()
------------------------------------------------------------------------------------------------------------------------------------------------------------
class Solution {
public:
string shortestPalindrome(string s) {
if(s == "")
return s;
string s2 = s;
reverse(s2.begin(), s2.end());
string news = s + s2;
int n = news.size();
vector<int> next(n+);
buildNext(news, next, n);
if(next[n] > s.size())
next[n] = next[n] + - s.size();
string pres = s.substr(next[n]);
reverse(pres.begin(), pres.end());
return pres + s;
}
void buildNext(string& s, vector<int>& next, int n)
{
int k = -;
int j = ;
next[] = -;
while(j < n)
{
if(k == - || s[j] == s[k])
{
k ++;
j ++;
next[j] = k;
}
else
{
k = next[k];
}
}
}
};
【Leetcode】Shortest Palindrome的更多相关文章
- 【leetcode】Shortest Palindrome(hard)★
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- 【leetcode】1278. Palindrome Partitioning III
题目如下: You are given a string s containing lowercase letters and an integer k. You need to : First, c ...
- 【LeetCode】336. Palindrome Pairs 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 HashTable 相似题目 参考资料 日期 题目地 ...
- 【LeetCode】9. Palindrome Number 回文数
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:回文数,回文,题解,Leetcode, 力扣,Python ...
- 【LeetCode】234. Palindrome Linked List 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】131. Palindrome Partitioning 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- 【leetcode】Valid Palindrome
题目简述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...
- 【题解】【字符串】【Leetcode】Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- 【leetcode】9. Palindrome Number
题目描述: Determine whether an integer is a palindrome. Do this without extra space. 解题分析: ^_^个人觉得这道题没有什 ...
随机推荐
- Delegate(委托)
在前面lambda章节中稍微提了一下委托,今天这章就让我们来深究一下委托. 委托的本质是一种类,他是继承MulticastDelegate类的. 而声明委托的关键字的delegate,如:public ...
- (转载)Oracle10g 数据泵导出命令 expdp 使用总结(三)
原文链接:http://hi.baidu.com/edeed/item/19aa0df856da3e19a6298894 Oracle10g 数据泵导出命令 expdp 使用总结(一) 14. JOB ...
- 使用joda-time工具类 计算时间相差多少 天,小时,分钟,秒
下面程序使用了两种方法计算两个时间相差 天,小时,分钟,秒 package jodotest; import java.text.ParseException; import java.text.Si ...
- PHP发送E-mail---新手教程
首先下载PHPmailer拓展包,其实就是别人封装好的类库,下载链接:http://pan.baidu.com/s/1slbhGo1 首先去163注册个账号,然后登陆进去,点击设置下面的 POP3/S ...
- babel如此简单
凡是看到这个标题点进来的同学,相信对babel都有了一定的了解.babel使用起来很简单,简单到都没有必要写一篇文章去介绍,直接看看官方文档就可以.所以我也在怀疑到底该不该写这篇文章.想来想去还是决定 ...
- MongoDB数据库基础操作
前面的话 为了保存网站的用户数据和业务数据,通常需要一个数据库.MongoDB和Node.js特别般配,因为Mongodb是基于文档的非关系型数据库,文档是按BSON(JSON的轻量化二进制格式)存储 ...
- docker学习笔记--重基础使用
最近一直在研究Elasticsearch,后来部门的同事遇到了一个docker集群的未授权访问漏洞,于是稍微看了一下docker进行了一下基本的入门,本文把自己学习docker的过程进行了一个详细的记 ...
- java核心机制
Java中有两种核心机制:Java虚拟机(Java Virtual Machine).垃圾收集机制(Garbage collection) 一.核心机制之Java虚拟机 ① Java虚拟机可以理解成一 ...
- JAVA入门[22]—thymeleaf
一.thymeleaf官网 官网:https://www.thymeleaf.org/index.html doc:https://www.thymeleaf.org/documentation.ht ...
- UWP中使用Composition API实现吸顶(1)
前几天需要在UWP中实现吸顶,就在网上找了一些文章: 吸顶大法 -- UWP中的工具栏吸顶的实现方式之一 在UWP中页面滑动导航栏置顶 发现前人的实现方式大多是控制ListViewBase的Heade ...