题意: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. apt-get正在等待报头(waiting for headers)

    可能的解决方法 1. 删除/var/cache/apt/archives/下的所有文件.可能是上次没有成功导致遗留了部分文件. 2. 如果使用的是代理,需要检查DNS.如果机器不能连接DNS服务器,要 ...

  2. com.microsoft.sqlserver.jdbc.SQLServerException: 将截断字符串或二进制数据。

    遇到这个错误 数据库表结构定义为:varchar(50) 实际插入数据的字符长度超过了50,会引发这种错误. 如果你是debug调试的. 或许你在  getSession().flush();  上报 ...

  3. Spring Boot 容器选择 Undertow 而不是 Tomcat Spring Boot 内嵌容器Unde

    Spring Boot 内嵌容器Undertow参数设置 配置项: # 设置IO线程数, 它主要执行非阻塞的任务,它们会负责多个连接, 默认设置每个CPU核心一个线程 # 不要设置过大,如果过大,启动 ...

  4. 新手必踩坑之display: inline-block

    今日励志语 往日不可追,来日犹可期,祝大家2019年继往开来 迷之间隙 我们创建一个导航列表,并将其列表 item 设置为 inline-block,主要代码如下: <div class=&qu ...

  5. 2019-8-31-C#-程序集数量对软件启动性能的影响

    title author date CreateTime categories C# 程序集数量对软件启动性能的影响 lindexi 2019-08-31 16:55:58 +0800 2018-10 ...

  6. 针对老式浏览器(主要是IE6、7、8)的css3-mediaqueries.js自适应布局

    <meta name="viewport" content="width=device-width, initial-scale=1" /> vie ...

  7. HttpRequest 工具

    1.根据 url 和 encoding 获取当前url页面的 html 源代码 public static string GetHtml(string url, Encoding encoding) ...

  8. 分布式事务 XA 两段式事务 X/open CAP BASE 一次分清

    分布式事务: 分布式事务是处理多节点上 的数据保持 类似传统 ACID 事物特性的 一种事物. XA:是一种协议,一种分布式事务的协议,核心思想是2段式提交. 1 准备阶段  2 提交阶段.XA协议是 ...

  9. vue打包之部署在非根路径下的三两事

    首先,感叹一下,2019年已经过去一半,想想自己做了些什么,好像也没做什么. 今天试着配一个nginx,以前的nginx都是指向的/根路径,今天的nginx指向的非/根路径,遇到许多问题的,总结总结. ...

  10. shell linux基本命令实例、笔记

    1. 在当前文件夹下.查找20分钟内,被訪问过的文件, 并将文件的详情显示出来: find ./ -name '*.log' -mmin -20 -exec ls -l {} \;   当然,须要指出 ...