Advertisement
Fahim_7861

trie

Aug 13th, 2021
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. typedef unsigned long long ull;
  5. typedef pair<int,int>pii;
  6. typedef pair<ll,pair<ll,ll>>plll;
  7. #define sf(a) scanf("%I64d",&a)
  8. #define pf(a) printf("%I64d\n",a)
  9. #define mem(a,b) memset(a,b,sizeof(a))
  10. #define vll(v) v.begin(),v.end()
  11. #define all(x) x.rbegin(),x.rend()
  12. #define F first
  13. #define S second
  14. #define minheap int,vector<int>,greater<int>
  15. //#define mp make_pair
  16. #define pb push_back
  17. #define pp pop_back
  18. #define BOUNDARY(i, j) ((i >= 0 && i < row) && (j >= 0 && j < column))
  19. #define ischar(x) (('a' <= x && x <= 'z') || ('A' <= x && x <= 'Z'))
  20. #define isvowel(ch) ((ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')||(ch=='A'|| ch=='E' || ch=='I'|| ch=='O'|| ch=='U'))
  21. #define fastread() (ios_base:: sync_with_stdio(false),cin.tie(NULL));
  22. #define in freopen("input.txt", "r", stdin)
  23. #define out freopen("output.txt", "w", stdout)
  24. const int Max_node = 1e6 + 10;
  25. const int Mod = 1e9 + 7;
  26. bool compare(const pair<int,int> &a, const pair<int,int> &b)
  27. {
  28. return (a.first > b.first);
  29. }
  30. ll lcm(ll a,ll b)
  31. {
  32. if(a==0 || b==0)
  33. return 0;
  34.  
  35. return a/__gcd(a,b)*b;
  36. }
  37. //___________________________________________________________________________________________________________________
  38. // CODE STARTS FROM HERE
  39. // MU_Codefighter2018
  40. //-------------------------------------------------------------------------------------------------------------------
  41.  
  42.  
  43.  
  44. int root,nnode;
  45. bool isword[Max_node];
  46. int node[Max_node][26];
  47.  
  48. void initialize()
  49. {
  50. root=0;
  51. nnode=0;
  52. for(int i=0; i<26; i++)
  53. {
  54. node[root][i]=-1;
  55. }
  56. }
  57.  
  58. void Insert(string str,int len)
  59. {
  60. int now=root,i;
  61.  
  62. for(i=0; i<len; i++)
  63. {
  64. int id=str[i]-'a';
  65.  
  66. if(node[now][id]==-1)
  67. {
  68. node[now][id]=++nnode;
  69.  
  70.  
  71. for(int j=0; j<26; j++)
  72. node[nnode][j]=-1;
  73. }
  74.  
  75. now=node[now][id];
  76. }
  77.  
  78. isword[now]=true;
  79. }
  80.  
  81.  
  82. bool Search(string str,int len)
  83. {
  84. int now=root,i;
  85.  
  86. for(i=0; i<len; i++)
  87. {
  88. int id=str[i]-'a';
  89.  
  90. if(node[now][id]==-1)return false;
  91.  
  92. now=node[now][id];
  93. }
  94.  
  95. return isword[now];
  96. }
  97.  
  98. int main()
  99. {
  100. fastread();
  101. initialize();
  102.  
  103. int n;
  104.  
  105. cin>>n;
  106.  
  107. while(n--)
  108. {
  109. string str;
  110.  
  111. cin>>str;
  112.  
  113. Insert(str,str.size());
  114. }
  115.  
  116. int q;
  117.  
  118. cin>>q;
  119.  
  120. while(q--)
  121. {
  122. string str;
  123.  
  124. cin>>str;
  125.  
  126. if(Search(str,str.size()))cout<<"YES"<<endl;
  127.  
  128. else cout<<"NO"<<endl;
  129. }
  130. }
  131.  
  132.  
  133.  
  134.  
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement