BotByte

trie.cpp

May 17th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. /* Trie */
  2. /* Author : M. A. Rafsan Mazumder */
  3.  
  4. #include <bits/stdc++.h>
  5.  
  6. using namespace std;
  7.  
  8. struct trie{
  9.     map<char, trie*> children;
  10. };
  11.  
  12.  
  13. bool noPrefixInsert(trie *t, string &word)
  14. {
  15.     trie* curr = t;
  16.     for(int i=0; i<word.length(); i++){
  17.         if(curr->children['$']) return false;
  18.  
  19.         if(!curr->children[word[i]]) curr->children[word[i]] = new trie;
  20.  
  21.         curr = curr->children[word[i]];
  22.     }
  23.  
  24.     if(curr->children.size() > 0) return false;
  25.  
  26.     curr->children['$'] = new trie;
  27.     return true;
  28. }
  29.  
  30. int main()
  31. {
  32.     int n;
  33.     scanf("%d", &n);
  34.  
  35.     trie* dictionary = new trie;
  36.  
  37.     for(int i=0; i<n; i++){
  38.         string word;
  39.         cin >> word;
  40.  
  41.         if(!noPrefixInsert(dictionary, word)){
  42.             cout << "BAD SET" << endl << word;
  43.             return 0;
  44.         }
  45.     }
  46.     cout << "GOOD SET";
  47. }
Advertisement
Add Comment
Please, Sign In to add comment