CF514C
涉及此类字符串匹配的问题,可以考虑使用 trie。
此题插入较为常规,故略过。
重点在查询。
由于题目要求恰好一个字符不同,又因为所有字符均为 a,b 和 c 中的一种,种类数小的可怜,直接暴力 dfs。
考虑构造 dfs(id, rt, tag) ,表示现在考虑字符串第
具体的看代码吧。
code
#include <bits/stdc++.h>
#define fq(i,a,b) for (int i = (a); i <= (b); i++)
#define fnq(i,a,b) for (int i = (a); i < (b); i++)
#define nfq(i,a,b) for (int i = (a); i >= (b); i--)
#define nfnq(i,a,b) for (int i = (a); i > (b); i--)
#define fqs(i,a,b,c) for (int i = (a); i <= (b); i += (c))
#define fnqs(i,a,b,c) for (int i = (a); i < (b); i += (c))
#define nfqs(i,a,b,c) for (int i = (a); i >= (b); i -= (c))
#define nfnqs(i,a,b,c) for (int i = (a); i > (b); i -= (c))
#define elif else if
using namespace std;
//#define int long long
//#define GRAPH
//const int EDGES = -1;
//const int VERTEXES = -1;
////# define EDGE_W
////typedef int W_TYPE;
//#define COMPLEX
inline int rd () {
int f = 1;
char ch = getchar ();
while (!isdigit (ch)) (ch == '-' ? (f = -1) : 0), ch = getchar ();
int num = 0;
while (isdigit (ch)) num = num * 10 + ch - '0', ch = getchar ();
return num * f;
}
#define d rd ()
inline int rd (const int modp) {
int f = 1;
char ch = getchar ();
while (!isdigit (ch)) (ch == '-' ? (f = -1) : 0), ch = getchar ();
int num = 0;
while (isdigit (ch)) num = (num * 10 + ch - '0') % modp, ch = getchar ();
return (num * f % modp + modp) % modp;
}
#ifdef GRAPH
struct Edge {
int v, nxt;
# ifdef EDGE_W
W_TYPE w;
# endif
Edge () {}
# ifdef EDGE_W
Edge (int _v, W_TYPE _w, int _nxt) {v = _v, w = _w, nxt = _nxt;}
# else
Edge (int _v, int _nxt) {v = _v, nxt = _nxt;}
# endif
} edge[EDGES];
int head[VERTEXES], ecnt;
# ifdef EDGE_W
void addedge (int u, int v, W_TYPE w) {edge[++ecnt] = Edge (v, w, head[u]); head[u] = ecnt;}
# define fe(u) for (int i = head[u], v, w; v = edge[i].v, w = edge[i].w, i; i = edge[i].nxt)
# else
void addedge (int u, int v) {edge[++ecnt] = Edge (v, head[u]); head[u] = ecnt;}
# define fe(u) for (int i = head[u], v; v = edge[i].v, i; i = edge[i].nxt)
# endif
void init () {memset (head, 0, sizeof head); ecnt = 0;}
#endif
#ifdef COMPLEX
struct cpx {
double a, b;
cpx () {a = 0, b = 0;}
cpx (double _a) {a = _a, b = 0;}
cpx (double _a, double _b) {a = _a, b = _b;}
friend cpx operator + (cpx a, cpx b) {return cpx (a.a + b.a, a.b + b.b);}
friend cpx operator - (cpx a, cpx b) {return cpx (a.a - b.a, a.b - b.b);}
friend cpx operator * (cpx a, cpx b) {return cpx (a.a * b.a - a.b * b.b, a.b * b.a + a.a * b.b);}
friend cpx operator / (cpx a, cpx b) {return cpx ((a.a * b.a + a.b * b.b) / (b.b * b.b + b.a * b.a), (a.b * b.a - a.a * b.b) / (b.b * b.b + b.a * b.a));}
friend cpx operator += (cpx &a, cpx b) {return a = a + b;}
friend cpx operator -= (cpx &a, cpx b) {return a = a - b;}
friend cpx operator *= (cpx &a, cpx b) {return a = a * b;}
friend cpx operator /= (cpx &a, cpx b) {return a = a / b;}
};
#endif
const int maxn = 1005000;
int ch[maxn][3], tag[maxn], cnt;
char s[maxn];
void insert () {
int p = 0, l = strlen (s + 1);
fq (i, 1, l) { int c = s[i];
if (!ch[p][c - 'a']) ch[p][c - 'a'] = ++cnt;
p = ch[p][c - 'a'];
} tag[p] = 1;
}
int len;
bool dfs (int i, int p, int tg) {
if (i == len + 1) return !tg && tag[p];//查到末尾,判断是否满足题意,该位置是否为一个字符串的结尾
int c = s[i] - 'a';
if (ch[p][c] && dfs (i + 1, ch[p][c], tg)) return 1;//该位不改变
if (!tg) return 0;//之前变过了,这一位变不了,失败返回0
fq (j, 0, 2) if (c != j && ch[p][j] && dfs (i + 1, ch[p][j], 0)) return 1;//这一位改变,成功则返回1
return 0;//都不成功,返回0
}
signed main () {
int n = d, m = d; while (n--) {scanf ("%s", s + 1); insert ();}
while (m--) {
scanf ("%s", s + 1); len = strlen (s + 1);
puts (dfs (1, 0, 1) ? "YES" : "NO");
}
return 0;
}