Advertisement
apl-mhd

TriesDatastructure

Jul 11th, 2017
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <set>
  6. #include <functional>
  7. #include <queue>
  8. #include <utility>
  9. #include <list>
  10. #include <climits>
  11. #include <string>
  12. #include <cstring>
  13. #define P printf("\n");
  14.  
  15. using namespace std;
  16.  
  17. struct node{
  18.    
  19.     int id=10;
  20.     bool mark;
  21.    
  22.     struct node *next[26];
  23.    
  24.     node(){
  25.        
  26.         mark=false;
  27.         for(int i=0; i<25; i++)
  28.             next[i]=NULL;
  29.        
  30.         }
  31.        
  32.     };
  33.    
  34.    
  35.     typedef node node;
  36.  
  37.    
  38.     void insert(node *cur, string word){
  39.        
  40.         int len = word.length();
  41.        
  42.         for(int i=0; i<len; i++){
  43.            
  44.             int id = word[i] -'a';
  45.            
  46.             if(cur->next[id]==NULL){
  47.                
  48.                 cur->next[id] = new node();
  49.                
  50.                 }
  51.             cur = cur->next[id];
  52.            
  53.             }
  54.            
  55.             cur->mark=1;
  56.        
  57.        
  58.         }
  59.        
  60. bool check(node *temp, string word){
  61.    
  62.     int len = word.size();
  63.    
  64.     for(int i=0; i<len; i++){
  65.        
  66.         int id= word[i]-'a';
  67.        
  68.        
  69.         if(temp->next[id]== NULL){
  70.    
  71.             return false;
  72.         }
  73.         temp=temp->next[id];
  74.        
  75.                    
  76.     }
  77.        
  78.     return true;
  79.    
  80.     }
  81.    
  82. int main(int argc, char **argv)
  83. {
  84.    
  85.     node *root = new node();
  86.    
  87.     node *cur = root;
  88.    
  89.     string word;
  90.    
  91.     cin>>word;
  92.    
  93.  
  94.    
  95.     insert(cur,word);
  96.    
  97.     cout<<check(root,"bangladesh");
  98.    
  99.    
  100.    
  101.    
  102.    
  103.    
  104.    
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement