题意: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. mysql 创建定时器

    mysql定时器是系统给提供了event,而oracle里面的定时器是系统给提供的job.废话少说,下面创建表: create table mytable ( id int auto_incremen ...

  2. 入门servlet:request请求转发和共享数据

    request 请求转发:一种在服务器内部的资源跳转方式 步骤: 1.通过request对象获取请求转发器对象:RequestDispatcher getRequestDispatcher(Strin ...

  3. JDBC连接整个过程

    1.导入驱动(放在lib下) connector-java-5.0.8-bin.jar 2.导入配置文件(放在src下) jdbc.properties driverClass=com.mysql.j ...

  4. JDK8日期时间操作小汇总

    统一使用java.time.*包下的类 1.获取当前的日期.时间.日期加时间 LocalDate todayDate = LocalDate.now(); //今天的日期 LocalTime now ...

  5. bzoj 4241 历史研究——分块(区间加权众数)

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4241 套路:可以大力预处理,如果求区间加权众数,可以预处理i~j块(或 j 位置)的最大值, ...

  6. idea小操作

    1.IDEA 实用功能Auto Import:自动优化导包(自动删除.导入包) 2.设置System.out.println();等快捷键 3.将idea的背景修改为图片 4.Linux ifconf ...

  7. JS 过滤HTML标签,取得纯文本

    一.过滤掉所有HTML标签如下: str.innerHTML.replace(/<.*?>/g,"") 二.过滤掉带属性的某一个标签,如<span class=' ...

  8. ifconfig命令为centos linux系统配置临时的局域名IP、网关以及子网掩码

    ifconfig eth0 192.168.1.25 netmask 255.255.255.0 broadcast 192.168.1.1 up netmask:子网掩码broadcast:默认网关

  9. Linux下安装ssdb

    安装ssdb wget --no-check-certificate https://github.com/ideawu/ssdb/archive/master.zip unzip master cd ...

  10. php pdo操作数据库的方法

    PDO 安装 你可以通过 PHP 的 phpinfo() 函数来查看是否安装了PDO扩展. 1.在 Unix /linux系统上安装 PDO 在Unix上或Linux上你需要添加以下扩展: exten ...