Manacher’s Algorithm
class Solution {
public:string longestPalindrome(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
string ts = "$#";
for(int i=0;i<s.length();i++){
ts += s[i];
ts += '#';
}
ts += '%';
int mx = -1,id,*rad;
rad = new int[ts.length()];
for(int i=1;i<ts.length();i++){
if(mx>i){
rad[i] = min(mx-i,rad[2*id-i]);
}
else
rad[i] = 1;
while(ts[i-rad[i]]==ts[i+rad[i]])
rad[i]++;
if(i+rad[i]>mx){
mx = i+rad[i];
id = i;
}
}
int pi = -1, mLen = -1;
for(int i=1;i<ts.length();i++){
if(rad[i]>mLen){
mLen = rad[i];
pi = i;
}
}
string ans = "";
for(int i=1;i<mLen;i++){
ans += ts[pi-mLen+2*i];
}
return ans;
}
};