首页 > 编程语言 > 详细

Manacher 算法

时间:2018-11-08 13:02:08      阅读:136      评论:0      收藏:0      [点我收藏+]

技术分享图片

 

回文串匹配!!!!NOIp回来在更。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cctype>

inline void read(int & x)
{
    x = 0;
    int k = 1;
    char c = getchar();
    while (!isdigit(c))
        if (c == -) c = getchar(), k = -1;
        else c = getchar();
    while (isdigit(c))
        x = (x << 1) + (x << 3) + (c ^ 48),
        c = getchar();
    x *= k;
}

char s[11001010], S[33003030];
int l, len, f[33001010], p, ans;

void Manacher()
{
    int nowr = 1, nowm = 1; f[1] = 1;
    for (int i = 2; i <= len; ++i)
    {
        if (i > nowr) f[i] = 1;
        else p = (nowm << 1) - i, f[i] = std::min(f[p], (nowr - i) << 1 | 1);
        int x = (f[i] >> 1) + 1;
        while (i > x && i + x <= len && S[i + x] == S[i - x])
            ++x, f[i] += 2;
        if (i + x - 1 > nowr) nowr = i + x - 1, nowm = i;
        ans = std::max(ans, f[i]);
    }
}

signed main()
{
    scanf("%s", s + 1);
    l = strlen(s + 1); S[1] = $; 
    for (int i = 1; i <= l; ++i)
        S[i << 1] = s[i],
        S[i << 1 | 1] = $;
    S[(l << 1) + 2] = \0;
    len = strlen(S + 1);
    Manacher();
    printf("%d", ans >> 1);
    return 0;
}

 

Manacher 算法

原文:https://www.cnblogs.com/yanyiming10243247/p/9928400.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!