Luo Gu -P3805-Manacher template

link:

https://www.luogu.org/problem/P3805

Meaning of the questions:

A string S is given only by the lowercase English characters a, b, c ... y, z consisting of palindromic find the longest string length of S.

A string of n

Ideas:

Horse-drawn vehicles algorithms.

Code:

#include <bits/stdc++.h>
using namespace std;

const int MAXN = 5e7+10;

int hw[MAXN];
char a[MAXN], s[MAXN<<1];

void Manacher(char *ori)
{
    int maxr = 0, mid;
    int len = strlen(ori);
    for (int i = 1;i < len;i++)
    {
        if (i < maxr)
            hw[i] = min(hw[mid*2-i], maxr-i);
        else
            hw[i] = 1;
        while (ori[i+hw[i]] == s[i-hw[i]])
            ++hw[i];
        if (hw[i]+i > maxr)
        {
            mid = i;
            maxr = hw[i]+i;
        }
    }
}

void Change(char *ori, char *pst)
{
    pst[0] = pst[1] = '#';
    int len = strlen(ori);
    for (int i = 0;i < len;i++)
        pst[i*2+2] = ori[i], pst[i*2+3] = '#';
    pst[len*2+2] = 0;
}

int main()
{
    scanf("%s", a);
    Change(a, s);
    Manacher(s);
    int ans = 1;
    for (int i = 0;i < strlen(s);i++)
        ans = max(ans, hw[i]);
    printf("%d\n", ans-1);


    return 0;
}

Guess you like

Origin www.cnblogs.com/YDDDD/p/11588548.html