[模板]-Manacher

string Manacher(string s)
{
    string t = "$#";
    for(int i=0;i<s.size();i++)
    {
        t+=s[i];
        t+="#";
    }
    vector<int>p(t.size(),0); 
    int mx=0,id=0,reCenter=0,reLen=0;
    for(int i=1; i<t.size(); i++)
    {
        p[i]=mx>i?min(mx-i,p[2*id-i]):1;
        while(t[i+p[i]]==t[i-p[i]]) p[i]++;
        if(i+p[i]>mx)
        {
            mx=i+p[i];
            id=i;
        }
        if(p[i]>reLen)
        {
            reLen=p[i];
            reCenter=i;    
        }
    }
    return s.substr((reCenter-reLen)/2,reLen-1);
}
posted @ 2021-02-02 22:31  L·S·D  阅读(46)  评论(0编辑  收藏  举报