【HDU5421】Victor and String(回文树)

【HDU5421】Victor and String(回文树)

题面

Vjudge
大意:
你需要支持以下操作:
动态在前端插入一个字符
动态在后端插入一个字符
回答当前本质不同的回文串个数
回答当前回文串个数

题解

回文树前端插入的操作,学一学感觉并不难?
额外维护一下一个前端插入的\(last\)
然后就和后端插入一模一样,前端插入时就是前端插入的\(last\)
唯一会产生的影响的就是当前字符差完后,
发现现在的\(last\)就是整个串,那么就要更新另外一端的\(last\)为当前这一端的\(last\)

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define ll long long
#define RG register
#define MAX 222222
inline int read()
{
    RG int x=0,t=1;RG char ch=getchar();
    while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
    if(ch=='-')t=-1,ch=getchar();
    while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
    return x*t;
}
struct Node
{
    int son[26],ff;
    int len,dep;
}t[MAX];
char s[MAX];
ll ans;
int tot,l,r,pre,suf,n;
void init()
{
    l=1e5,r=l-1;memset(s,'\0',sizeof(s));
    memset(t,0,sizeof(t));ans=0;
    t[pre=suf=0].ff=t[tot=1].ff=1;t[1].len=-1;
}
void extend(int c,int n,int &last,int op)
{
    int p=last;
    while(s[n]!=s[n-op*t[p].len-op])p=t[p].ff;
    if(!t[p].son[c])
    {
        int v=++tot,k=t[p].ff;t[v].len=t[p].len+2;
        while(s[n]!=s[n-op*t[k].len-op])k=t[k].ff;
        t[v].ff=t[k].son[c];t[p].son[c]=v;
        t[v].dep=t[t[v].ff].dep+1;
    }
    last=t[p].son[c];ans+=t[last].dep;
    if(t[last].len==r-l+1)pre=suf=last;
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        init();
        while(n--)
        {
            int opt=read();
            if(opt<=2)
            {
                char c=getchar();
                if(opt==1)s[--l]=c,extend(c-97,l,pre,-1);
                else s[++r]=c,extend(c-97,r,suf,1);
            }
            else if(opt==3)printf("%d\n",tot-1);
            else printf("%lld\n",ans);
        }
    }
    return 0;
}

转载于:https://www.cnblogs.com/cjyyb/p/9210996.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值