# Hard-题目46：214. Shortest Palindrome

106人阅读 评论(0)

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”.

class Solution {
public:
int longestPalindrom(string s) {
string s1;
s1.resize(2 * s.length() + 2);
int idx = 0;
s1[idx++] = '\$';
s1[idx++] = '#';
for (char a : s) {
s1[idx++] = a;
s1[idx++] = '#';
}
vector<int> p(s1.length(), 0);
int res = 0;
for (int id = 0, i = 1; i < s1.length(); ++i) {
if (i < id + p[id]) p[i] = min(p[2 * id - i], id + p[id] - i);
else p[i] = 1;
while (s1[i + p[i]] == s1[i - p[i]]) ++p[i];
if (id + p[id] < i + p[i]) id = i;
if (p[i] == i) res = max(res, i);
}
return res - 1;
}

string shortestPalindrome(string s) {
int pos = longestPalindrom(s);
string res;
for (int i = s.length() - 1; i >= pos; --i) res.push_back(s[i]);
return res + s;
}
};

12ms,25.88%,8ms,47.74%
Cmershen的碎碎念：

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：56567次
• 积分：3205
• 等级：
• 排名：第10668名
• 原创：270篇
• 转载：53篇
• 译文：0篇
• 评论：7条
文章分类
最新评论