leetcode+ 添加首部几个字母数,回文子串,KMP

点击打开链接
class Solution {
public:
    string shortestPalindrome(string s) {
        string r = s;
        reverse(r.begin(), r.end());
        string t = s+'#'+r;
        vector<int> next(t.size(), 0);
        for(int i=1; i<t.size(); i++){
            int j = next[i-1];
            while (j>0 && t[i]!=t[j]) {
                j = next[j-1];
            }
            next[i] = (j+= t[i]==t[j]);
        }
        return r.substr(0, s.size()-next.back()) + s;
    }
};

猜你喜欢

转载自blog.csdn.net/u013554860/article/details/81064444
今日推荐