【模板】后缀自动机 LCP

先贴,今天太晚,明天填坑。
题目描述太不良心了,误解了题意*n
这里写图片描述

#include<iostream>
#include<cstdio>
#include<cstring>
#define N 2000005
using namespace std;
int ch[N][26],pre[N],dis[N];
char a[N],b[N];
int sz=0,n,m,p,q,np,nq,now,last,S,ans=0,len=0;
void ins(int x){
    p=last;np=++sz;last=sz;
    dis[np]=dis[p]+1;
    for(;p&&!ch[p][x];p=pre[p]) ch[p][x]=np;
    if(!p) {pre[np]=S;return;}
    q=ch[p][x];
    if(dis[q]==dis[p]+1) pre[np]=q;
    else{
        nq=++sz;dis[nq]=dis[p]+1;
        memcpy(ch[nq],ch[q],sizeof(ch[q]));
        pre[nq]=pre[q];
        pre[q]=pre[np]=nq;
        for(;ch[p][x]==q;p=pre[p])ch[p][x]=nq;
    }
}
void getans(int x){
    if(ch[now][x]) len++,now=ch[now][x];
    else{
        while(now&&!ch[now][x]) now=pre[now];
        if(now==0) now=S,len=0;
        else len=dis[now]+1,now=ch[now][x];
    }
    printf("%d\n",len);
}
int main(){
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    scanf("%d %d\n",&n,&m);
    scanf("%s\n%s\n",a,b);
    now=last=S=++sz;
    for(int i=0;i<n;i++) ins(a[i]-'a');
    for(int i=0;i<m;i++) getans(b[i]-'a');
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值