bhupixb

TRIE

Sep 8th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. #include "bits/stdc++.h"
  2. #include "vector"
  3. using namespace std;
  4.  
  5. const int N = 2e6 + 5;
  6.  
  7. typedef struct st{
  8. st *h[26];
  9. }node;
  10. node * get(){
  11. node *temp = new node;
  12.  
  13. for(int i = 0; i < 26; i++)
  14. temp->h[i] = NULL;
  15.  
  16. return temp;
  17. }
  18. void insert(node *head, string s){
  19. node *temp;
  20. if(!head->h[s[0]-'a']){
  21. head->h[s[0]-'a'] = get();
  22. }
  23. temp = head->h[s[0]-'a'];
  24.  
  25. for(int i = 1; i < s.size(); i++){
  26. if(!temp->h[s[i]-'a']){
  27. temp->h[s[i]-'a'] = get();
  28. }
  29. temp = temp->h[s[i]-'a'];
  30. }
  31. }
  32. bool search(node *head, string s){
  33. node * temp = head;
  34. for(int i = 0; i < s.size(); i++){
  35. if(!temp->h[s[i]-'a'])return false;
  36. temp = temp->h[s[i]-'a'];
  37. }
  38. return true;
  39. }
  40. int main(){
  41. node *head = get();
  42. int n;string s;
  43. cin >> n;
  44. while(n--){
  45. cin >> s;
  46. insert(head, s);
  47. }
  48. int q;
  49. cin >> q;
  50. while(q--){
  51. cin >> s;
  52. cout << search(head, s) << '\n';
  53. }
  54.  
  55. return 0;
  56. }
Add Comment
Please, Sign In to add comment