Advertisement
anoosykh95

Untitled

Jul 22nd, 2016
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.16 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define P(x,y) make_pair(x,y)
  3. using namespace std;
  4. struct node;
  5. typedef node* pnode;
  6. struct node{
  7.     int cnt;
  8.     bool isend;
  9.     pnode child[26];
  10.     node(){ cnt=isend=0; memset(child , 0 , sizeof(child)); }
  11. };
  12. class Trie{
  13.     public:
  14.     pnode head;
  15.     void Clear(pnode &it){
  16.         if(!it) return;
  17.         delete(it);
  18.         for(int j=0;j<26;j++)
  19.             Clear(it->child[j]);
  20.     }
  21.     void init(){
  22.         Clear(head);
  23.         head = new node();
  24.     }
  25.     void insert_(string str){
  26.         pnode it = head;
  27.         it->cnt++;
  28.         for(auto ch : str){
  29.             int let = ch-'a';
  30.             if(it->child[let] == NULL) it->child[let] = new node();
  31.             it = it->child[let];
  32.             it->cnt++;
  33.         }
  34.         it->isend=1;
  35.     }
  36.     bool find_(string str){
  37.         pnode it = head;
  38.         for(auto ch : str){
  39.             int let = ch - 'a';
  40.             if(it->child[let] == NULL) it->child[let] = new node();
  41.             it = it->child[let];
  42.         }
  43.         return it->isend;
  44.     }
  45. };
  46. int main(){
  47.     Trie T;
  48.     T.init();
  49.     T.insert_("asd");
  50.     if(T.find_("asd")) puts("yeah");
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement