【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. 解题分析: ^_^个人觉得这道题没有什 ...
随机推荐
- Android精品源码与技术博文
Android精品源码android遵循Material Design风格天气源码基于exoplay 自定义播放器 支持直播 1 ExoUserPlayer 基本播放器...几种动画效果Animati ...
- webpack的Hot Module Replacement运行机制
使用webpack打包,难免会使用Hot Module Replacement功能,该功能能够实现修改.添加或删除前端页面中的模块代码,而且是在页面不刷新的前提下.它究竟是怎么运作的呢?本文主要从调试 ...
- 关于IO流代码BufferedReader
package JBJADV003;import java.io.*;public class BufferedReaderTest { /** * @param args */ public sta ...
- tp框架为什么验证码加载不出来?----- ob_clean() 可解决
在用tp做验证码时,代码逻辑都正确,但就是加载不出图片来,如何解决呢?在创建验证码之前加上 ob_clean(); public function haha(){ ob_clean(); $v = n ...
- 面向对象15.3String类-常见功能-获取-1
API使用: 查API文档的时候,有很多方法,首先先看返回的类型 下面的方法函数有的是有覆写Object类的如1.1图,如果没有复写的话是写在1.2图片那里的,如果找到了相对于的方法,可以点击进去可以 ...
- 【转载】 ISO14229系列之二:诊断指令格式和相关概念
转载链接:http://www.cnblogs.com/autogeek/p/4458658.html 1. 简单的通信机制 其实诊断通信的机制很简单,可以类比client-server通信方式,即客 ...
- 51nod_1040:最大公约数之和(数论)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1040 给出一个n,求1-n这n个数,同n的最大公约数的和. ...
- log4j日志重定向
配置相应的类名或者包名,将日志重新定向到输入文件里 log4j.rootLogger=INFO,DEBUG,CONSOLE ##过滤日志 log4j.logger.[类名|包名]=INFO,[输出目的 ...
- oracle分组-神奇的cube和rollup
先看代码: 表结构如下: emp表 EMPNO NOT NULL NUMBER(4) ENAME ...
- (转)java for循环的执行顺序和几种常用写法
算是温习吧.问题比较基础,但是也比较重要.(虽然是C,但是很经典) for循环可以说在每个程序中都少不了的,语句头包括三个部分:初始化,判读条件,一个表达式. 但是这三个部分的执行顺序是什么,这是我们 ...