双倍回文

记录一下吧
回文代码如下:

#include <cstdio>
#include <cstring>
#include <string.h>
char str[1000002 + 1200];

int fast(char *p)
{
    int ans = 1;
    for (int i = 1; p[i]; ++i)
    {
        int s = i, e = i, t;
        while (p[e + 1] == p[i]) ++e;
        i = e;
        while (p[s - 1] == p[e + 1]) --s, ++e;
        if ((t = e - s + 1) > ans) ans = t;
    }
    return ans;
}

int main()
{
    str[0]='$';
    int n;
    scanf("%d", &n);
    while (n --) {
        scanf("%s", str + 1);
        printf("%d\n", fast(str));
    }
    return 0;
}

改进一下就是双倍回文了:

#include <bits/stdc++.h>

using namespace std;

char str[500002 + 1200];

vector <int> E[500010];

int fast(char *p)
{
    int ans = 0;
    for (int i = 1; p[i]; ++i)
    {
        int s = i, e = i;
        while (p[e + 1] == p[i])
        {
            ++e;
            E[i].push_back(e);
            if((e-i+1)%4 == 0)
                ans = max(ans, e-i+1);
        }
        i = e;
        while (p[s - 1] == p[e + 1])
        {
            --s;
            ++e;
            E[s].push_back(e);
            if((e-s+1)%4==0)
            {
                int mid = s+(e-s)/2;
                int f = 0, n = E[s].size();
                for(int j = 0; j < n; j++)
                    if(E[s][j] == mid)
                    {
                        f = 1;
                        break;
                    }
                if(f)
                    ans = max(ans, e-s+1);
            }
        }
    }
    return ans;
}

int main()
{
    str[0]='$';
    int n;
    scanf("%d", &n);
    scanf("%s", str + 1);
    printf("%d\n", fast(str));
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值