bzoj1212: [HNOI2004]L语言

来源:互联网 发布:2017年的网络新词 编辑:程序博客网 时间:2024/05/22 10:54

传送门
对于被匹配的串暴力建立trie
然后直接每个位置往下匹配就行了。

#include<cstring>#include<cmath>   #include<cstdio>  #include<iostream>  #include<cstdlib>   #include<algorithm>using namespace std;char s[15],str[1050005];int f[1050005],n,m,len,now,ch[305][30],bo[305],tot,ans,t;inline void updata(int x){    int now=ch[0][str[x]-'a'];    for (;now;now=ch[now][str[++x]-'a']){        if (bo[now]) f[x]=1;        if (x==len) return;    }}int main(){    scanf("%d%d",&n,&m);    for (int i=1;i<=n;i++){        scanf("%s",s+1);        len=strlen(s+1);        now=0;        for (int j=1;j<=len;j++){            if (!ch[now][s[j]-'a']) ch[now][s[j]-'a']=++tot;            now=ch[now][s[j]-'a'];        }        bo[now]=1;    }    for (int i=1;i<=m;i++){        scanf("%s",str+1);        len=strlen(str+1);        ans=t=0;        memset(f,0,sizeof(f));        f[0]=1;        for (int j=0;j<=len;j++){            if (t>10) break;            if (!f[j]){                t++;                continue;            }            ans=j;            t=0;            updata(j+1);        }        printf("%d\n",ans);    }}
0 0
原创粉丝点击