题目:求最长回文子串

题解:manacher算法,百度一堆讲解- -,我也不说了,想知道啥的自己百度去吧

class Solution {
public:
    string longestPalindrome(string s) {
        string manaStr = "$#";
        for (int i=0;i<s.size();i++){
            manaStr += s[i];
            manaStr += '#';
        }
        vector<int> rd(manaStr.size(), 0);
        int pos = 0, mx = 0;
        int start = 0, maxLen = 0;
        for (int i = 1; i < manaStr.size(); i++) {
            rd[i] = i < mx ? min(rd[2 * pos - i], mx - i) : 1;
            while (i+rd[i]<manaStr.size() && i-rd[i]>0 && manaStr[i + rd[i]] == manaStr[i - rd[i]])
                rd[i]++;
                if (i + rd[i] > mx){
                    pos = i;
                    mx = i + rd[i];
                }
                if (rd[i] - 1 > maxLen){
                    start = (i - rd[i]) / 2;
                    maxLen = rd[i] - 1;
                }
            }
        return s.substr(start, maxLen);
    }
};
02-13 12:49