您的位置:首页 > 其它

bzoj1212: [HNOI2004]L语言

2017-04-08 15:54 323 查看
传送门

对于被匹配的串暴力建立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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: