Advertisement
Manioc

trieee

Jun 6th, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.34 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. struct node{
  6.     bool end;
  7.     struct node* word[3];
  8. } ;
  9.  
  10. node* head;
  11.  
  12. void init(){
  13.     head = new node();
  14.     head->end = false;
  15. }
  16.  
  17. void add(string s){
  18.     node* current = head;
  19.     for(int i = 0; i < s.size(); i++){
  20.         int letra = s[i]-'a';
  21.  
  22.         if(!current->word[letra]){
  23.             current->word[letra] = new node();
  24.         }
  25.  
  26.         current = current->word[letra];
  27.     }
  28.     current->end = true;
  29. }
  30.  
  31. bool search(string s, int id){
  32.     if(head->word[id] == NULL) return false;
  33.  
  34.     node* current = head->word[id];
  35.  
  36.     //cout << id << endl;
  37.     int erros = 0;
  38.     for(int i = 0; i < s.size()-1; i++){
  39.         int letra = s[i]-'a';
  40.  
  41.         if(current->word[letra] == NULL){
  42.             if(erros) return false;
  43.  
  44.             erros++;
  45.             int flag = false;
  46.             for(int j = 0; j < 3; j++){
  47.                 if(current->word[j] != NULL){
  48.                     if(current->word[j]->word[s[i+1]-'a']){
  49.                         current = current->word[j];
  50.                         flag = true;
  51.                         break;
  52.                     }
  53.                 }
  54.             }
  55.  
  56.             if(!flag) return false;
  57.         }else{
  58.             if(current->word[letra]->word[s[i+1]-'a']) current = current->word[letra];
  59.             else{
  60.                 int flag = false;
  61.                 for(int j = 0; j < 3; j++){
  62.                     if(current->word[j] != NULL){
  63.                         if(current->word[j]->word[s[i+1]-'a']){
  64.                             current = current->word[j];
  65.                             flag = true;
  66.                             erros++;
  67.                             break;
  68.                         }
  69.                     }
  70.                 }
  71.  
  72.                 if(!flag) return false;
  73.             }
  74.         }
  75.  
  76.  
  77.     }
  78.  
  79.     if(current->word[s.size()-1] != NULL) return true;
  80.     else{
  81.         if(erros) return false;
  82.         return true;
  83.     }
  84. }
  85. int main(){
  86.     int a, b; scanf("%d %d", &a, &b);
  87.     init();
  88.     for(int i = 0; i < a; i++){
  89.         char txt[300007]; scanf("%s", txt);
  90.         add(txt);
  91.     }
  92.  
  93.     for(int i = 0; i < b; i++){
  94.         char txt[300007]; scanf("%s", txt);
  95.         if(search(txt, 0) || search(txt,1) || search(txt, 2)) printf("YES\n");
  96.         else printf("NO\n");
  97.     }
  98.     return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement