fork download
  1. #include <bits/stdc++.h>
  2. #define pb push_back
  3. #define pii pair<int,int>
  4. #define fi first
  5. #define int long long
  6. #define se second
  7. #define ios ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
  8. #define TXT "test"
  9. #define freo if(fopen(TXT".inp","r")){freopen(TXT".inp","r",stdin); freopen(TXT".out","w",stdout);}
  10.  
  11. using namespace std;
  12.  
  13. const int MXN = 5e6 + 5;
  14.  
  15. int n,type;
  16. int trie[MXN][26],cnt=1;
  17. bool en[MXN];
  18. void add(string s)
  19. {
  20. int node=0;
  21. for(char c:s)
  22. {
  23. int ch=c-'a';
  24. if(trie[node][ch]==0)
  25. trie[node][ch]=cnt++;
  26. node=trie[node][ch];
  27. }
  28. en[node]=1;
  29. }
  30. bool findx(string s)
  31. {
  32. int node=0;
  33. for(char c:s)
  34. {
  35. int ch=c-'a';
  36. if(trie[node][ch]==0)
  37. return 0;
  38. node = trie[node][ch];
  39. }
  40. return en[node];
  41. }
  42. void solve()
  43. {
  44. cin>>n;
  45. string s;
  46. for(int i=1;i<=n;i++)
  47. {
  48. cin>>s;
  49. add(s);
  50. }
  51. int m;
  52. cin>>m;
  53. while(m--)
  54. {
  55. cin>>s;
  56. cout<<findx(s)<<"\n";
  57. }
  58. }
  59. main()
  60. {
  61. ios;
  62. solve();
  63. }
  64.  
Success #stdin #stdout 0.01s 5252KB
stdin
8
1 5
1 7
2 2
1 3
2 2
1 4
1 9
2 5
stdout
1
1