BZOJ 2565 最长双回文串(manacher)

题意:求一个最长的双回文串,双回文串定义是两个回文串是连续的但是不相交。

做法:manacher预处理一下。然后用h1[i]代表以i为结尾的最长回文串,h2[i]代表以i为起点的最长回文串。我这里用了一个单调队列,因为对于一个点来说,作为结尾想要最长,那么自然希望最远的中点,所以维护一个位置单调递增,右边界也单调递增的单调队列即可。正过来做一次反过来做一次就可以了。

后来发觉太沙茶了,其实完全不需要那么麻烦。。设上次更新到的位置为j,那么这次i为中心的回文串范围要更新的点是从j到i的最远。j是单调递增的。这样也是o(n)。而且只需要2个for循环就可以了。。。

不过懒得改了。。

AC代码:

#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<ctype.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<cstdlib>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<string.h>
#include<string>
#include<sstream>
#include<bitset>
using namespace std;
#define ll long long
#define ull unsigned long long
#define eps 1e-8
#define NMAX 10000000
#define MOD 1000000007
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1)
#define mp make_pair
template<class T>
inline void scan_d(T &ret)
{
    char c;
    int flag = 0;
    ret=0;
    while(((c=getchar())<'0'||c>'9')&&c!='-');
    if(c == '-')
    {
        flag = 1;
        c = getchar();
    }
    while(c>='0'&&c<='9') ret=ret*10+(c-'0'),c=getchar();
    if(flag) ret = -ret;
}
const int maxn = 100000+10;
char ch[maxn],s[maxn<<1];
int str[maxn<<1];
void manacher(char *p, int m)
{
    int x,y = 0;
    for(int i = 1; i < m; i++)
    {
        if(i > y)
        {
            int j = 1;
            for(; p[i-j] == p[i+j]; j++);
            str[i] = j;
            y = i+j-1; x = i;
        }
        else
        {
            int dui = 2*x-i;
            if(str[dui] < y-i+1) str[i] = str[dui];
            else
            {
                int j = y-i+1;
                for(; p[i-j] == p[i+j]; j++);
                str[i] = j;
                y = i+j-1; x = i;
            }
        }
    }
}
int q[maxn<<1][2];
int h1[maxn<<1],h2[maxn<<1];
int main()
{
#ifdef GLQ
    freopen("input.txt","r",stdin);
//    freopen("o.txt","w",stdout);
#endif
    while(~scanf("%s",ch))
    {
        int len = strlen(ch),cnt = 1;
        for(int i = 0; i < len; i++)
        {
            s[cnt++] = '#';
            s[cnt++] = ch[i];
        }
        s[0] = '$';
        s[cnt++] = '#';
        s[cnt] = '\0';
        manacher(s,cnt);
        int head = 0,rear = -1;
        for(int i = 1; i < cnt; i++)
        {
            if(rear < head) h1[i] = i;
            else
            {
                h1[i] = 2*q[head][0]-i;
                if(q[head][1] == i) head++;
            }
            if(str[i] == 1) continue;
            if(rear < head || str[i]+i-1 > q[rear][1])
            {
                rear++;
                q[rear][0] = i;
                q[rear][1] = str[i]+i-1;
            }
        }
        head = 0; rear = -1;
        for(int i = cnt-1; i >= 1; i--)
        {
            if(rear < head) h2[i] = i;
            else
            {
                h2[i] = 2*q[head][0]-i;
                if(q[head][1] == i) head++;
            }
            if(str[i] == 1) continue;
            if(rear < head || i-str[i]+1 < q[rear][1])
            {
                rear++;
                q[rear][0] = i;
                q[rear][1] = i-str[i]+1;
            }
        }
        int ans = 0;
        for(int i = 2; i < cnt-2; i += 2)
        {
            ans = max(ans,i-h1[i]+1+(h2[i+2]-(i+2)+1));
        }
        ans = ans/2+1;
        printf("%d\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值