【模板】后缀自动机 LCP

来源:互联网 发布:网络教育商业计划书 编辑:程序博客网 时间:2024/06/05 03:03

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

#include<iostream>#include<cstdio>#include<cstring>#define N 2000005using 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;}
0 0
原创粉丝点击