【HDU4622】Reincarnation(后缀自动机)

题面

Vjudge
题意:给定一个串,每次询问l~r组成的子串的不同子串个数

题解

看到字符串的大小很小
而询问数太多
所以我们预处理任意的答案

枚举左端点,依次向右加入新节点
直接统计答案即可

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define MAX 2222
inline int read()
{
    int x=0,t=1;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;
}
int ans[MAX][MAX];
int last,tot;
char ch[MAX];
struct Node
{
    int son[26];
    int ff,len;
}t[MAX<<1];
void init()
{
    last=tot=1;
    memset(t,0,sizeof(t));
}
void extend(int c)
{
    int p=last,np=++tot;last=np;
    t[np].len=t[p].len+1;
    while(p&&!t[p].son[c])t[p].son[c]=np,p=t[p].ff;
    if(!p)t[np].ff=1;
    else
    {
        int q=t[p].son[c];
        if(t[q].len==t[p].len+1)t[np].ff=q;
        else
        {
            int nq=++tot;
            t[nq]=t[q];
            t[nq].len=t[p].len+1;
            t[np].ff=t[q].ff=nq;
            while(p&&t[p].son[c]==q)t[p].son[c]=nq,p=t[p].ff;
        }
    }
}
int main()
{
    int T=read();
    while(T--)
    {
        scanf("%s",ch+1);
        for(int i=1,l=strlen(ch+1);i<=l;++i)
        {
            init();
            for(int j=i;j<=l;++j)
            {
                extend(ch[j]-97);
                ans[i][j]=ans[i][j-1]+t[last].len-t[t[last].ff].len;
            }
        }
        int Q=read();
        while(Q--)
        {
            int l=read(),r=read();
            printf("%d\n",ans[l][r]);
        }
    }
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值