Codeforces Problem 713A Sonya and Queries (dictionary tree) __acm

Source: Internet
Author: User

This article can use the directory features yo ↑ (click above [+])

Game links →codeforces Round #371 (Div. 2)

codeforces Problem 713A Sonya and Queries

accept:0 submit:0
Time Limit:1 second Memory limit:256 megabytes
Problem Description

Today Sonya learned about long integers and invited all her friends to share the fun. Sonya has a initially empty multiset with integers. Friends give her t queries, each of one of the following type:
+ ai-add non-negative integer ai to the multiset. Note, this she has a multiset, thus there may is many occurrences of the same integer. -Ai-delete A single occurrence of non-negative integer AI from the multiset. It ' s guaranteed, that there was at least one AI in the multiset. ? S-count the number of integers in the multiset (with repetitions), the 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 to the pattern. If the is shorter than this integer, it's supplemented with 0-s from the left. Similarly, if the the integer is shorter than the pattern its decimal notation are supplemented with the 0-s to the left.

For example, if it is S = 010, than integers, 2212, X and 414 match, while integers 3, D 1030 do not.

Input

The ' The ' input contains an integer t (1≤t≤100)-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 spaces and an integer AI (0≤ai < 1018) given without leading zero ES (unless it ' s 0). If ci equals '? ' then it's followed by a spaces and a sequence of zeroes and onse, giving the pattern of length no more tha n 18.

It's guaranteed that there would be in least one query of type '? '.

It's guaranteed that any time some an integer is removed from the multiset, there'll be at least one occurrence of this int Eger 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.
Sample Input 12
+ 1
+ 241
? 1
+ 361
-241
? 0101
+ 101
? 101
-101
? 101
+ 4000
? 0
4
+ 200
+ 200
-200
? 0 Sample Output 2
1
2
1
1
1 Hint

Consider the integers matching the patterns from the queries type. Queries are numbered in the order they appear in the input.

1.1 and 241.

2.361.

3. and 361.

4.361.

5.4000.
Problem Idea

Ideas for solving problems:

Meaning

T-operation, each operation has the following three kinds of types:

1. + A adds a non-negative integer A to the multiset, allowing the same number to appear

2.-A nonnegative integer A is subtracted from the multiset, which guarantees that the nonnegative integer a multiset exists when performing this operation

3.? s asks how many numbers in the Multiset match the pattern string s (matching definition: In the pattern string, ' 0 ' means that the bit is even, ' 1 ' means the bit is odd)

That is, a number from the right to the left, the parity of each digit is consistent with the pattern string (when the length is inconsistent, in front of 0), called the two match

Number of outputs that match the pattern string s

Type
Dictionary Tree

Analysis

In three operations, it is clear that the starting point in the third operation

How to match the pattern string s is the key to this problem

Because the pattern string s is only ' 0 ' and ' 1 ', you can think of a binary tree

And this kind of match can be solved by dictionary tree.

For each inserted number, because only the parity of each digit is what we want to use

So, we can save only its parity in the dictionary tree

For example, sample 1





The general process is like this, hoping to help you understand

"Time Complexity && optimization"
O (18t)

Topic link →codeforces Problem 713A Sonya and Queries Source Code

/*sherlock and Watson and adler*/#pragma comment (linker, "/stack:1024000000,1024000000") #include <stdio.h> # include<string.h> #include <stdlib.h> #include <queue> #include <stack> #include <math.h > #include <vector> #include <map> #include <set> #include <bitset> #include <cmath> include<complex> #include <string> #include <algorithm> #include <iostream> #define EPS 1e-9 #
Define LL long #define PI acos ( -1.0) #define BITNUM (a) __builtin_popcount (a) using namespace Std;
const int N = 2;
const int M = 2;
const int inf = 1000000007;
const int mod = 1000000007; struct Tree {int a[m],t;}
S[1000005];
Char Ch[n];
int p;
    void init (int x) {s[x].t=0;
memset (s[x].a,-1,sizeof (S[X].A));
    } void Buildtree (__int64 x) {int i,k=0;
            for (i=0;i<18;i++) {if (s[k].a[x%2]==-1) {s[k].a[x%2]=p;
        Init (p++);
        } k=s[k].a[x%2];
    s[k].t++;    x/=10;
    } void DeleteTree (__int64 x) {int i,k=0;
        for (i=0;i<18;i++) {k=s[k].a[x%2];
        s[k].t--;
    x/=10;
    } void Query (__int64 x) {int i,k=0;
            for (i=0;i<18;i++) {if (s[k].a[x%2]==-1) {puts ("0");
        return;
        } k=s[k].a[x%2];
    x/=10;
printf ("%d\n", s[k].t);
    int main () {int t;
    __int64 x;
    P=0;init (p++);
    scanf ("%d", &t);
        while (t--) {scanf ("%s%i64d", ch,&x);
        if (ch[0]== ' + ') buildtree (x);
        else if (ch[0]== '-') deletetree (x);
    else query (x);
return 0; }
Rookie Grow up Remember

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.