A. Sonya and Queries
time limit per test
memory limit per test
input
output
Today Sonya learned about long integers and invited all her friends to share the fun. Sonya has an initially empty multiset with integers. Friends give her t
- + ai — add non-negative integer ai
- - ai — delete a single occurrence of non-negative integer ai from the multiset. It's guaranteed, that there is at least one ai
- ? s — count the number of integers in the multiset (with repetitions) that match some pattern s consisting of 0 and 1. In the pattern,0 stands for the even digits, while 1 stands for the odd. Integer x matches the pattern s, if the parity of the i-th from the right digit in decimal notation matches the i-th from the right digit of the pattern. If the pattern is shorter than this integer, it's supplemented with0-s from the left. Similarly, if the integer is shorter than the pattern its decimal notation is supplemented with the 0-s from the left.
For example, if the pattern is s = 010, than integers 92, 2212, 50 and 414 match the pattern, while integers 3, 110, 25 and 1030
Input
The first line of the input contains an integer t (1 ≤ t ≤ 100 000) — the number of operation Sonya has to perform.
Next t lines provide the descriptions of the queries in order they appear in the input file. The i-th row starts with a character ci — the type of the corresponding operation. If ci is equal to '+' or '-' then it's followed by a space and an integer ai (0 ≤ ai < 1018) given without leading zeroes (unless it's 0). If ci equals '?' then it's followed by a space and a sequence of zeroes and onse, giving the pattern of length no more than 18.
It's guaranteed that there will be at least one query of type '?'.
It's guaranteed that any time some integer is removed from the multiset, there will be at least one occurrence of this integer in it.
Output
For each query of the third type print the number of integers matching the given pattern. Each integer is counted as many times, as it appears in the multiset at this moment of time.
Examples
input
12
+ 1
+ 241
? 1
+ 361
- 241
? 0101
+ 101
? 101
- 101
? 101
+ 4000
? 0
output
2
1
2
1
1
input
4
+ 200
+ 200
- 200
? 0
output
1
把+的每一个数n化成18位01串,若n的第一位为奇数,则01串的第一位为1,以此类推.
在把插入的数放在Trie树上,并且把01结尾代表的树上的节点的cnt++;
删除时只要把01串结尾代表的节点的cnt--;
查询时只要输出对应的cnt;
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#define maxn 50005
using namespace std;
typedef long long ll;
struct Node{
int cnt;
Node *p[2];
};
Node *NewNode(){
Node *m = new Node();
memset(m, 0, sizeof(Node));
return m;
}
void Solve(Node *root, int *q, int k){
Node *h = root;
for(int i = 0; i < 18; i++){
int d = q[i];
if(h -> p[d] == NULL){
h -> p[d] = NewNode();
h = h -> p[d];
}
else
h = h -> p[d];
}
if(k == 0)
(h -> cnt)++;
else
(h -> cnt)--;
}
void Delete(Node *p){
if(p -> p[0])
Delete(p -> p[0]);
if(p -> p[1])
Delete(p -> p[1]);
free(p);
}
int Query(Node *root, int *q){
Node *h = root;
for(int i = 0; i < 18; i++){
int d = q[i];
if(h -> p[d] == NULL)
return 0;
h = h -> p[d];
}
return h -> cnt;
}
int main(){
// freopen("in.txt", "r", stdin);
int t, q[20];
char ch;
ll n;
scanf("%d ", &t);
Node *root = NewNode();
while(t--){
int cnt = 0;
scanf("%c %I64d ", &ch, &n);
memset(q, 0, sizeof(q));
while(n){
if(n % 10 % 2 == 1)
q[cnt] = 1;
n /= 10;
cnt++;
}
if(ch == '+')
Solve(root, q, 0);
else if(ch == '-')
Solve(root, q, 1);
else
printf("%d\n", Query(root, q));
}
Delete(root);
return 0;
}