Advertisement
adnan_dipta89

Trie

Oct 17th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.51 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. /*
  6.  * Complete the contacts function below.
  7.  */
  8. class Trie
  9. {
  10. public:
  11.     bool endmark;
  12.     Trie *next[26];
  13.     Trie()
  14.     {
  15.         endmark = false;
  16.         for(int i = 0; i < 26; i++)
  17.             next[i] = NULL;
  18.     }
  19.     void insert(string s);
  20.     int findMatch(string s);
  21.     void _count(Trie *curr, int* cnt);
  22.     bool _search(string s);
  23.  };
  24. void Trie::_count(Trie *curr, int* cnt)
  25. {
  26.     if(curr == NULL)
  27.         return;
  28.     if(curr->endmark)
  29.     {
  30.         //cout << curr->endmark << endl;
  31.         (*cnt)++;
  32.     }
  33.     for(int i = 0; i < 25; i++)
  34.     {
  35.         _count(curr->next[i],cnt);
  36.     }
  37. }
  38. void Trie::insert(string s)
  39. {
  40.     Trie *curr = this;
  41.     for(int i = 0; i < s.size(); i++)
  42.     {
  43.         int id = s[i]-'a';
  44.         if(curr->next[id] == NULL)
  45.             curr->next[id] = new Trie();
  46.         curr = curr->next[id];
  47.     }
  48.     curr->endmark = true;
  49. }
  50. bool Trie::_search(string s)
  51. {
  52.     Trie *curr = this;
  53.     for(int i = 0; i < s.size(); i++)
  54.     {
  55.         int id = s[i]-'a';
  56.         if(curr->next[id] == NULL)
  57.             return false;
  58.         curr = curr->next[id];
  59.     }
  60.     return curr->endmark;
  61. }
  62. int Trie::findMatch(string s)
  63. {
  64.     int cnt = 0;
  65.     Trie *curr = this;
  66.     for(int i = 0; i < s.size(); i++)
  67.     {
  68.         int id = s[i]-'a';
  69.         if(curr->next[id] == NULL)
  70.             return 0;
  71.         curr = curr->next[id];
  72.     }
  73.     _count(curr,&cnt);
  74.     return cnt;
  75. }
  76. vector<int> contacts(vector<vector<string>> queries) {
  77.     /*
  78.      * Write your code here.
  79.      */
  80.     Trie *t = new Trie();
  81.     vector<int> result;
  82.     for(int i = 0; i < queries.size(); i++)
  83.     {
  84.         string s1, s2;
  85.         s1 = queries[i][0];
  86.         s2 = queries[i][1];
  87.         if(s1 == "add")
  88.         {
  89.             t->insert(s2);
  90.         }
  91.         else if(s1 == "find")
  92.         {
  93.             int cnt = t->findMatch(s2);
  94.             result.push_back(cnt);
  95.         }
  96.         else if(s1 == "search")
  97.         {
  98.             cout << t->_search(s2) << endl;
  99.         }
  100.     }
  101.     return result;
  102. }
  103.  
  104. int main()
  105. {
  106.     vector<vector<string>> queries;
  107.     int n;
  108.     cin >> n;
  109.     while(n--)
  110.     {
  111.         string s1, s2;
  112.         vector<string> a;
  113.         cin >> s1 >> s2;
  114.         a.push_back(s1);
  115.         a.push_back(s2);
  116.         queries.push_back(a);
  117.     }
  118.     vector<int> result = contacts(queries);
  119.     for(int i = 0; i < result.size(); i++)
  120.     {
  121.         cout << result[i] << endl;
  122.     }
  123.     return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement