LeetCode 214 Shortest Palindrome
214-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)内解决。用Manacher求每个元素为中心的回文串半径时,只需要求前一半即可,因为以后一一半的任意元素为中心的回文子串不可能从第一个元素开始。
string shortestPalindrome(string s) {
//Manacher求出以每个字符为中心的回文串长度
string str = "$#";
for (int i = 0; i != s.size(); ++i) {
str.push_back(s[i]);
str.push_back('#');
}
str.push_back('~');
int id = 0;
int mx = 0;
//只需要求前一半字符的p[]数组中的值。
vector<int> p(str.size() / 2 + 1);
for (int i = 1; i != p.size(); ++i) {
if (i < mx)
p[i] = mx - i < p[2 * id - i] ? mx - i : p[2 * id- i];
else
p[i] = 1;
while (str[i - p[i]] == str[i + p[i]]) ++p[i];
if (i + p[i] > mx) {
mx = i + p[i];
id = i;
}
}
int index = 0;
int len = 1;
//找出最后一个以第一个字符开始的回文串
for (int i = 1; i != p.size(); ++i) {
if ((i - p[i]) / 2 == 0) {
len = p[i];
index = i;
}
}
index = (index - len) / 2;
--len;
//生成最终结果的回文串
string result;
for (int i = s.size() - 1; i >= index + len; --i) result.push_back(s[i]);
result += s;
return result;
}
利用KMP求next数组的方法
解决该问题可以转化成寻找以第一个元素开始的最大的回文子串。
我们知道回文串的逆序串等于它本身,记字符串为str,逆串为reverse,以第一个元素开始的最大的回文子串将匹配到reverse的后缀,如果把令str+reverse称为新串,则有最大匹配的前缀和后缀的长度是以第一个元素开始的最大回文子串。KMP算法中的next数组保存的就是该元素之前的子串的最大匹配前缀和后缀的长度,我们就可以利用该算法来求解该问题。
KMP算法中的next数组是包括整串的最大匹配前缀和后缀的长度,所以解决该问题的next需要比KMP的next大1,最终next[strlen(str+reverse)]就是第一个元素开始的最大的回文子串的长度。
由于我们用来next数组的字符串是str+reverse拼接的,最终有可能最大匹配前缀和后缀的长度超过了str的长度,所以最终求得的next[strlen(str+rever)]如果大于str的长度,就需要减去str的长度。当然也可以在拼接的时候在str和reverse中间插入一个特殊字符(str中不会出现的字符),即str+‘#’+reverse,来避免最大匹配前缀和后缀的长度超过str的长度。
string shortestPalindrome(string s) {
//生成新string
string reverse_s = s;
reverse(reverse_s.begin(), reverse_s.end());
string str = s + '#' + reverse_s;
//求next数组
vector<int> next(str.size() + 1);
next[0] = -1;
int k = -1;
int j = 0;
while (j < next.size()) {
while (k >= 0 && str[k] != str[j]) k = next[k];
++j;
++k;
next[j] = k;
}
//需要逆序插到前面的子串
string pre = s.substr(next[str.size()], s.size() - next[str.size()]);
reverse(pre.begin(), pre.end());
return pre + s;
}
LeetCode 214 Shortest Palindrome的更多相关文章
- [LeetCode] 214. Shortest Palindrome 最短回文串
Given a string s, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- 【LeetCode】214. Shortest Palindrome 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 前缀是否回文 判断前缀 相似题目 参考资料 日期 题 ...
- 【LeetCode】214. Shortest Palindrome
Shortest Palindrome Given a string S, you are allowed to convert it to a palindrome by adding charac ...
- 214. Shortest Palindrome
题目: Given a string S, you are allowed to convert it to a palindrome by adding characters in front of ...
- 【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】Shortest Palindrome
Shortest Palindrome Given a string S, you are allowed to convert it to a palindrome by adding charac ...
- 214 Shortest Palindrome 最短回文串
给一个字符串 S, 你可以通过在字符串前面添加字符将其转换为回文串.找到并返回可以用这种方式转换的最短回文串.例如:给出 "aacecaaa",返回 "aaacecaaa ...
- [LeetCode] Shortest Palindrome 最短回文串
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
随机推荐
- [转]基于AWS的自动化部署实践
作者 徐桂林 发布于 2014年1月22日 -------------------------------------------------------------------- 1. 背景 在过去 ...
- mount什么意思
root@Home-Cloud:/mnt# dfFilesystem 1K-blocks Used Available Use% Mounted on/dev/root ...
- ABP的第一个程序和遇到的一些问题
ABP在这里就不多介绍了.在这篇文章中主要介绍使用模板生成的ABP项目使用遇到的一些问题. 1.首先在http://www.aspnetboilerplate.com/ 官方网站上创建一个模板项目, ...
- LVM原理及PV、VG、LV、PE、LE关系图
PV(physical volume):物理卷在逻辑卷管理系统最底层,可为整个物理硬盘或实际物理硬盘上的分区.VG(volume group):卷组建立在物理卷上,一卷组中至少要包括一物理卷,卷组建立 ...
- 慕课网-Java入门第一季-6-7 使用 Arrays 类操作 Java 中的数组
来源:http://www.imooc.com/code/1556 Arrays 类是 Java 中提供的一个工具类,在 java.util 包中.该类中包含了一些方法用来直接操作数组,比如可直接实现 ...
- java时间类简单总结
java时间类(Data类) 1.Data类(没有考虑到国际化,好多方法已过时java.util.Data包中) 父类(是类不是接口含有直接子类3个): 日期格式为:年月日时分秒(不包含毫秒部分) ...
- CSS3学习笔记1-选择器和新增属性
前言:之前自学了一些关于CSS3的知识,在学习过程中也遇到过坑,不过总算磕磕绊绊的学习完了关于CSS3的相关知识,于是最近把之前的笔记和代码整理了一下,也算是一个对CSS3知识的回顾复习吧,也希望能够 ...
- map和list遍历基础
本文属原创,转载请注明出处:http://www.cnblogs.com/robinjava77/p/5456085.html (Robin) Map import java.util.HashMap ...
- The source attachment does not contain the source for the file ActionSupport.class 错误
报错 Syntax error, insert ";" to complete FieldDeclaration 报错 The source attachment does ...
- Robot Framework:RF中对时间操作的datetime库常用关键字
[转自:http://blog.csdn.net/r455678/article/details/52993765] 1.对固定日期进行操作,增加或减去单位时间或者时间段 2.对两个时间段进行操作 3 ...