Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iostream>
- #include <array>
- #include <vector>
- #include <numeric>
- #include <random>
- #include <chrono>
- #include <set>
- #include <cassert>
- using namespace std;
- // #define int long long
- #pragma comment(linker,"/STACK:1000000000,1000000000")
- const long long INF = 1e9 + 7;
- const int MAXN = 3e5 + 100;
- const int N = 1e5 + 10;
- struct vertex {
- int next[4];
- int terminal = false;
- } v[MAXN];
- int root = 0, top = 1;
- inline void add(const string& s) {
- int cur = root;
- for (auto& i: s) {
- int x = i - 'a';
- if (v[cur].next[x] == 0) {
- v[cur].next[x] = top++;
- }
- cur = v[cur].next[x];
- }
- v[cur].terminal = true;
- }
- string test;
- int pos;
- inline bool dfs(int ver, bool was) {
- if (pos == test.size()) {
- return was && v[ver].terminal;
- }
- for (int i = 0; i < 3; ++i) {
- if (v[ver].next[i] != 0) {
- bool res = false;
- if (char(i + 'a') == test[pos]) {
- ++pos;
- res = dfs(v[ver].next[i], was);
- --pos;
- } else {
- if (!was) {
- ++pos;
- res = dfs(v[ver].next[i], true);
- --pos;
- }
- }
- if (res) {
- return true;
- }
- }
- }
- return false;
- }
- signed main() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- int n, m;
- cin >> n >> m;
- for (int i = 0; i < n; ++i) {
- string s;
- cin >> s;
- add(s);
- }
- for (int i = 0; i < m; ++i) {
- cin >> test;
- pos = 0;
- cout << (dfs(root, false) ? "YES\n" : "NO\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment