蒟蒻的珍藏(烂大街的manacher算法,本蒟蒻脑子不好所以将模板寄存此处)

· · 算法·理论

#include <iostream>
#include <string>
#include <vector>
using namespace std;
string s;
string manacher(string s)
{
    int size = s.size();
    if (size <= 1)
    {
        return s;
    }
    string ns = "?!";
    for (int i = 0;i < size;i++)
    {
        ns += s[i];
        ns += '!';
    }
    int l = ns.size(), c = 0, m = 0, pos = 0, maxl = 0;
    vector <int> p(l);
    for (int i = 1;i < l;i++)
    {
        if (i < m)
        {
            p[i] = min(p[2 * c - i], m - i + 1);
        }
        else
        {
            p[i] = 1 + (ns[i] != '!');
        }
        if (i - p[i] >= 0 && i + p[i] < ns.size())
        {
            while (ns[i - p[i]] == ns[i + p[i]])
            {
                p[i]++;
            }
        }
        if (p[i] + i - 1 > m)
        {
            c = i;
            m = p[i] + i - 1;
        }
        if (p[i] - 1 > maxl)
        {
            maxl = p[i] - 1;
            pos = i;
        }
    }
    string ans = "";
    char temp;
    for (int i = 0;i < 2 * maxl - 1;i++)
    {
        temp = ns[pos - (maxl - 1) + i];
        if (temp != '!')
        {
            ans += temp;
        }
    }
    return ans;
}
int main()
{
    cin >> s;
    cout << manacher(s) << endl << manacher(s).length();
    return 0;
}