n个字符串,m个字符串询问,询问n个中,是否存在与该字符串长度相同,且只有一位不一样。
Tire树,dfs询问。
dfs(x,n,u,k) //x位,长度n,询问到的节点u,k个不一样的
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<utility>
#include<queue>
#include<set>
#include<map>
#include<math.h>
#include<string>
using namespace std;
#define inf 0x3f3f3f3f
#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:102400000,102400000")
#define ll long long
const double eps = 1e-9;
int ch[600005][3], val[600005],sz;
char p[600006];
void Trie(char *s)
{
int n = strlen(s);
int u = 0;
for (int i = 0; i < n; i++)
{
int k = s[i] - 'a';
if (!ch[u][k])
{
ch[u][k] = sz++;
}
u = ch[u][k];
}
val[u] = 1;
}
bool solve(int x, int n, int u, int k)
{
if (k > 1) return false;
if (x == n)
{
if (val[u] && k == 1) return true;
else return false;
}
int t = p[x] - 'a';
for (int i = 0; i < 3; i++)
{
if (!ch[u][i]) continue;
if (i == t)
{
if(solve(x + 1, n, ch[u][i], k)) return true;
}
else
{
if (solve(x + 1, n, ch[u][i], k + 1)) return true;
}
}
return false;
}
int main()
{
int i, j, k, n, m;
cin >> n >> m;
sz = 1;
while (n--)
{
scanf("%s",p);
Trie(p);
}
while (m--)
{
scanf("%s",p);
if (solve(0, strlen(p), 0, 0)) printf("YES\n");
else printf("NO\n");
}
}