您的位置:首页 > 其它

[LeetCode] Shortest Palindrome 最短回文串

2015-09-18 06:27 483 查看
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.

这道题让我们求最短的回文串,LeetCode中关于回文串的其他的题目有 Palindrome Number 验证回文数字,Validate
Palindrome 验证回文字符串, Palindrome Partitioning 拆分回文串,Palindrome
Partitioning II 拆分回文串之二和 Longest Palindromic Substring 最长回文串。题目让我们在给定字符串s的前面加上最少个字符,使之变成回文串,那么我们来看题目中给的两个例子,最坏的情况下是s中没有相同的字符,那么最小需要添加字符的个数为s.size()
- 1个,第一个例子的字符串包含一个回文串,只需再在前面添加一个字符即可,还有一点需要注意的是,前面添加的字符串都是从s的末尾开始,一位一位往前添加的,那么我们只需要知道从s末尾开始需要添加到前面的个数。这道题如果用brute force无法通过OJ,所以我们需要用一些比较巧妙的方法来解。这里我们用到了KMP算法,KMP算法是一种专门用来匹配字符串的高效的算法,具体方法可以参见这篇博文从头到尾彻底理解KMP。我们把s和其转置r连接起来,中间加上一个其他字符,形成一个新的字符串t,我们还需要一个和t长度相同的一位数组p,其中p[i]表示从t[i]到开头的子串的相同前缀后缀的个数,具体可参考KMP算法中解释。最后我们把不相同的个数对应的字符串添加到s之前即可,代码如下:

class Solution {
public:
string shortestPalindrome(string s) {
string r = s;
reverse(r.begin(), r.end());
string t = s + "#" + r;
vector<int> p(t.size(), 0);
for (int i = 1; i < t.size(); ++i) {
int j = p[i - 1];
while (j > 0 && t[i] != t[j]) j = p[j - 1];
p[i] = (j += t[i] == t[j]);
}
return r.substr(0, s.size() - p[t.size() - 1]) + s;
}
};


参考资料:

https://leetcode.com/discuss/36807/c-8-ms-kmp-based-o-n-time-%26-o-n-memory-solution

/article/1362918.html

/article/4999938.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: