fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define FILE "none"
  4.  
  5. #define int long long
  6. #define ii pair<int, int>
  7.  
  8. #define fast ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  9. #define FOR(I, L, R) for(int I = (int)L; I <= (int)R; I++)
  10. #define FOD(I, R, L) for(int I = (int)R; I >= (int)L; I--)
  11. #define FOA(I, A) for(auto &I : A)
  12.  
  13. #define all(A) A.begin(), A.end()
  14. #define first fi
  15. #define second se
  16.  
  17. const int N = 1e6 + 10;
  18. const int oo = 1e9;
  19. const int mod = 1e9 + 7;
  20.  
  21. int n, m, q;
  22. string s;
  23.  
  24. int cnt;
  25. int child[N][30];
  26. int en[N];
  27.  
  28. void build(int id, int u){
  29. if(u == m){
  30. en[id] = 1;
  31. return;
  32. }
  33. int v = s[u + 1] - 'a';
  34.  
  35. if(!child[id][v]){
  36. child[id][v] = ++cnt;
  37. }
  38.  
  39. build(child[id][v], u + 1);
  40. }
  41.  
  42. bool fnd(int id, int u){
  43. if(u == m){
  44. return en[id];
  45. }
  46. int v = s[u + 1] - 'a';
  47.  
  48. if(!child[id][v]) return 0;
  49. return fnd(child[id][v], u + 1);
  50. }
  51.  
  52. signed main(){ fast
  53. if(fopen(FILE".INP", "r")){
  54. freopen(FILE".INP", "r", stdin);
  55. freopen(FILE".OUT", "w", stdout);
  56. }
  57.  
  58. cin >> n;
  59.  
  60. FOR(i, 1, n){
  61. cin >> s;
  62. m = s.size();
  63. s = " " + s;
  64.  
  65. build(0, 0);
  66. }
  67.  
  68. cin >> q;
  69. while(q--){
  70. cin >> s;
  71. m = s.size();
  72. s = " " + s;
  73.  
  74. if(fnd(0, 0)) cout << "YES";
  75. else cout << "NO";
  76. cout << '\n';
  77. }
  78. }
Success #stdin #stdout 0.01s 5276KB
stdin
Standard input is empty
stdout
Standard output is empty