#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull long long
const int maxn=6e5+10;
const ll mod=1e14+7;
ull seed=1331,T[maxn];
char word[maxn];
map<ull,bool>ma;
ull gethash()
{
ull res=0;
int len=strlen(word);
for(int i=0; word[i]; i++)
{
res+=word[len-i-1]*T[i];
res%=mod;
}
return res;
}
bool quer()
{
ull ha=gethash();
int len=strlen(word);
for(int i=0; word[i]; i++)
{
int w=word[i];
for(int j=‘a‘; j<=‘c‘; j++)
{
if(w==j)continue;
ull p=((j-w)*T[len-i-1]%mod+ha+mod)%mod;
if(ma[p])return 1;
}
}
return 0;
}
int main()
{
int n,m;
T[0]=1;
for(int i=1; i<maxn; i++)
{
T[i]=T[i-1]*seed%mod;
}
scanf("%d %d",&n,&m);
for(int i=1; i<=n; i++)
{
scanf("%s",word);
ma[gethash()]=1;
}
for(int i=1; i<=m; i++)
{
scanf("%s",word);
if(quer())
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
原文:https://www.cnblogs.com/carcar/p/10202050.html