题意: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”.

题解:首先想到一种可行的方案是将原串str翻转成inverStr直接放在前面显然构成回文,则我们试着将inverStr往右移,也就是减少添加字符个数,若翻转串与原串完全重合,则仍然满足要求,显然最少添加就是使翻转串与原串的最大重合。 
更进一步,我们知道重合的部分也是个回文串,即我们也是要求最长的str是回文子串的前缀,可以使用二维DP求解(TLE)。 
其实我们注意到重合的部分是inverStr的后缀,则我们可以考虑使用KMP算法,因为它就是用来求解即是前缀又是后缀的最长子串。 
一开始遇到瓶颈,因为直接将str+inverStr,求解后的pi[N]可能大于str的长度,则这个解说明不了问题。 
看到discuss加入一个不同与子串的字符“#”恍然大悟。

代码如下:

 class Solution {
public:
string shortestPalindrome(string s) {
int sLen = s.size();
string inverStr(s.rbegin(), s.rend());
string tmpStr = s+"#"+inverStr; vector<int> pi(tmpStr.size()+, );
//pi[i]代表字符串前i个字符中即是前缀又是后缀的最长子串长度
int k = ;
for(int i = ;i < tmpStr.size();i++) {
while(k != && tmpStr[k] != tmpStr[i]) {
k = pi[k];
}
if(tmpStr[k] == tmpStr[i]) {
k++;
}
pi[i+] = k;
} return inverStr.substr(, sLen-k)+s;
}
};

【To Read】Shortest Palindrome(KMP)的更多相关文章

  1. 【Leetcode】Shortest Palindrome

    Shortest Palindrome Given a string S, you are allowed to convert it to a palindrome by adding charac ...

  2. 【leetcode】Shortest Palindrome(hard)★

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  3. 【leetcode dp】132. Palindrome Partitioning II

    https://leetcode.com/problems/palindrome-partitioning-ii/description/ [题意] 给定一个字符串,求最少切割多少下,使得切割后的每个 ...

  4. 【Codeforces 600C】Make Palindrome

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 计算出来每个字母出现的次数. 把字典序大的奇数出现次数的字母换成字典序小的奇数出现次数的字母贪心即可. 注意只有一个字母的情况 然后贪心地把字 ...

  5. 【LeetCode OJ】Valid Palindrome

    Problem Link: http://oj.leetcode.com/problems/valid-palindrome/ The following two conditions would s ...

  6. 【算法Everyday】第三日 KMP算法

    题目 你知道的. 分析 分析不来. 代码 void OutputArray(int* pArr, int iLen) { ; i < iLen; i++) { printf("%d\t ...

  7. 【LeetCode练习题】Valid Palindrome

    Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...

  8. 【文文殿下】浅谈KMP算法next数组与循环节的关系

    KMP算法 KMP算法是一种字符串匹配算法,他可以在O(n+m)的时间内求出一个模式串在另一个模式串下出现的次数. KMP算法是利用next数组进行自匹配,然后来进行匹配的. Next数组 Next数 ...

  9. 【POJ - 3280】Cheapest Palindrome(区间dp)

    Cheapest Palindrome 直接翻译了 Descriptions 给定一个字符串S,字符串S的长度为M(M≤2000),字符串S所含有的字符的种类的数量为N(N≤26),然后给定这N种字符 ...

随机推荐

  1. PAT甲级——A1024 Palindromic Number

    A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...

  2. [Day3] Nginx配置Https

    一. 网络安全之Http与Https Http协议是互联网行业中设计的最好架构之一.20多年间,应用Http协议传输数据的软件越来越多,企图从http协议传输中非法获取.篡改用户重要数据的非法行为也越 ...

  3. Django-rest Framework(五)

    把十大接口做完了才能更好的了解后面的视图类 1.(重点)二次封装Response;自定义APIResponse继承Response,重写 ____init____方法 from rest_framew ...

  4. Vue. 之 替换 左上角 title标签处的图标

    Vue. 之 替换 左上角 title标签处的图标 1.icon命名为favicon.ico放在项目的位置:src/assets/favicon.ico 2.在index.html中写入: <l ...

  5. MATLAB---fopen、fprintf函数

    1 概述 fopen()是个将数据按指定格式读入到matlab中的函数. fprintf()是个将数据按指定格式写入到文本文件中的函数. 2 用法 2.1 fopen函数 matlab中fopen函数 ...

  6. 2019.10.30 csp-s模拟测试94 反思总结

    头一次做图巨的模拟题OWO 自从上一次听图巨讲课然后骗了小礼物以后一直对图巨印象挺好的233 T1: 对于XY取对数=Y*log(x) 对于Y!取对数=log(1*2*3*...*Y)=log1+lo ...

  7. 从大数据到快数据 数据智创未来——2019 CCF大数据与计算智能大赛正式开赛!

    8月17日,以“数据驱动,智创未来”为主题的2019 CCF大数据与计算智能大赛(CCF Computing Intelligence Contest,简称CCF BDCI)全球启动仪式,在北京大学正 ...

  8. LUOGU P4171 [JSOI2010]满汉全席

    传送门 解题思路 2-SAT 裸题. 代码 #include<iostream> #include<cstdio> #include<cstring> #inclu ...

  9. Spring MVC 搭建web项目示例

    环境为Eclipse 1:新建Dynamic web project  : springMvcDemo 2:下载spring的jar包,把jar包复制到WEB-INF/lib目录下 3.添加配置文件w ...

  10. [DEPRECATION] Encountered positional parameter near xxx Positional parameter are considered deprecated; use named parameters or JPA-style positional parameters instead.

    WARN:30 20:55:45,340ms- [HqlSqlWalker]1009行-[DEPRECATION] Encountered positional parameter near line ...