Advertisement
Jasir

Trie (static array)

Jun 14th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 KB | None | 0 0
  1.  
  2. int nnode, root, node[2][mx*55], isword[mx*55], cnt[2][mx*55];
  3. int ara[mx], cua[mx];
  4.  
  5. void inittrie(){
  6.     root = 0;
  7.     nnode = 0;
  8.     for(int i=0;i<2;i++){
  9.         node[i][root] = -1;
  10.         cnt[i][root] = 0;
  11.     }
  12.     memset(isword, 0, sizeof isword);
  13. }
  14.  
  15. void inserttrie(int a){
  16.     int now = root;
  17.     for(int i=54;i>=0;i--){
  18.         if(node[(bool)((1LL<<i)&a)][now] == -1){
  19.             node[(bool)((1LL<<i)&a)][now] = ++nnode;
  20.             for(int j=0;j<2;j++){
  21.                 node[j][nnode] = -1;
  22.                 cnt[j][nnode] = 0;
  23.             }
  24.         }
  25.         cnt[(bool)((1LL<<i)&a)][now]++;
  26.         now = node[(bool)((1LL<<i)&a)][now];
  27.     }
  28.     isword[now] = 1;
  29. }
  30.  
  31. void deltrie(int a){
  32.     int now = root;
  33.     for(int i=54;i>=0;i--){
  34.         cnt[(bool)((1LL<<i)&a)][now]--;
  35.         now = node[(bool)((1LL<<i)&a)][now];
  36.     }
  37. }
  38.  
  39. int findtrie(int a){
  40.     int ans = 0;
  41.     int now = root;
  42.     for(int i=54;i>=0;i--){
  43.         int b = (bool)((1LL<<i)&a);
  44.         if(cnt[1-b][now]==0){
  45.             now = node[b][now];
  46.             if(b) ans |= (1LL<<i);
  47.         }
  48.         else{
  49.             now = node[1-b][now];
  50.             if(1-b) ans |= (1LL<<i);
  51.         }
  52.     }
  53.     return ans;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement