Codeforces Global Round 7 D2. Prefix-Suffix Palindrome (Hard version) -- manacher

Posted hh13579

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Global Round 7 D2. Prefix-Suffix Palindrome (Hard version) -- manacher相关的知识,希望对你有一定的参考价值。

D2. Prefix-Suffix Palindrome (Hard version)

题目链接
manacher做法

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int p[N*2];
string manacher(string ss)
{
    int len = ss.size();
    string s;
    s.resize(len*2+2);
    for(int i=len;i>=0;i--)
    {
        s[i*2+2] = ss[i];
        s[i*2+1] = ‘#‘;
    }
    s[0] = ‘*‘;
    int maxlen = 0,id = 0;
    for(int i=2;i<=len*2;i++)
    {
        if(p[id]+id>i)
            p[i] = min(p[id*2-i],p[id]+id-i);
        else
            p[i] = 1;
        while(s[i+p[i]]==s[i-p[i]])
            p[i]++;
        if(p[id]+id<p[i]+i)
            id = i;
        if(p[i]==i)
            maxlen = p[i]-1;
    }
    return ss.substr(0,maxlen);
}
int main()
{
    string s;
    cout << s;
    int T;
    cin >> T;

    while(T--)
    {
        cin >> s;
        int l = 0 ,r = s.size()-1;
        string ans;
        while(s[l]==s[r])
        {
            l++;
            r--;
        }
        if(l>r)
        {
            cout << s <<"
";
            continue;
        }
        string ss = s.substr(l,s.size()-2*l);
        string s1 = manacher(ss);
        reverse(ss.begin(),ss.end());
        string s2 = manacher(ss);
        ans = s.substr(0,l);
        if(s1.size()>s2.size())
        {
            ans += s1;
        }
        else
        {
            ans += s2;
        }
        ans += s.substr(s.size()-l,l);
        cout << ans << "
";
    }
    return 0;
}

以上是关于Codeforces Global Round 7 D2. Prefix-Suffix Palindrome (Hard version) -- manacher的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Global Round 7 题解(未完)(ABCD)

Codeforces Global Round 7 总结

Codeforces Global Round 7

Codeforces Global Round 7 题解

Codeforces Global Round 7 D2. Prefix-Suffix Palindrome (Hard version) -- manacher

Codeforces Global Round 7 D2. Prefix-Suffix Palindrome (Hard version)(Manacher算法+输出回文字符串)