Untitled
user_5668965
c_cpp
9 days ago
2.2 kB
1
Indexable
Never
#include<bits/stdc++.h> using namespace std; #define int long long int #define fastio ios_base::sync_with_stdio(false);cin.tie(nullptr); cout.tie(nullptr); #define endl '\n' struct bitTrie{ const int max_bit = 18 ; int root = 0 ; struct Node { vector<int> next_node; int cnt = 0; Node() { next_node.resize(2); // 0 or 1 fill(begin(next_node), end(next_node), -1); } }; vector<Node> tree; bitTrie(){ tree.emplace_back(); } void add(int val){ int node = root; tree[node].cnt++; for(int i=max_bit;i>0;i--){ int temp = val%10; val /= 10; int bit = (temp%2); if(tree[node].next_node[bit]==-1){ tree[node].next_node[bit] = tree.size(); tree.emplace_back(); } node = tree[node].next_node[bit]; tree[node].cnt++; } } void del(int val){ int node = root; tree[node].cnt--; for(int i=max_bit;i>0;i--){ int temp = val%10; val /= 10; int bit = (temp%2); node = tree[node].next_node[bit]; tree[node].cnt--; } } int query(string s){ reverse(s.begin(), s.end()); while(s.size() != max_bit){ s.push_back('0'); } int node = root; for(int i=0; i<max_bit; i++){ int id = s[i]-'0'; if(tree[node].next_node[id] == -1){ return 0; } node = tree[node].next_node[id]; } return tree[node].cnt; } }; int32_t main(){ fastio int t; cin>>t; bitTrie bt = bitTrie(); while(t--){ char c; cin>>c; if(c=='+'){ int x;cin>>x; bt.add(x); } else if(c=='-'){ int x;cin>>x; bt.del(x); } else{ string s;cin>>s; cout<<bt.query(s)<<endl; } } return 0; }
Leave a Comment