描述
求一个字符串的最长回文子串
解决
利用manacher算法解决
class Solution {
public:
string longestPalindrome(string s) {
string str = "$#";
int lenth = s.size();
for (int i = 0; i != lenth; ++i){
str = str + s[i] + "#";
}
int res[2001];
memset(res, 0, sizeof(res));
int lenth1 = str.size();
int mx = 0, id = 0;
for (int i = 1; i < lenth1 - 1; ++i){
res[i] = mx > i ? min(res[2 * id - i], mx - i) : 1;
while (str[i + res[i]] == str[i - res[i]]){
++res[i];
}
if (res[i] + i> mx){
mx = res[i] + i;
id = i;
}
}
int max_lenth = 0;
for (int i = 0; i < lenth1; ++i){
if (res[i] > max_lenth){
max_lenth = res[i];
id = i;
}
}
return s.substr((id - max_lenth) / 2, res[id] - 1);
}
};