1. 程式人生 > >CodeForces 713A|Sonya and Queries|字典樹|沒方法

CodeForces 713A|Sonya and Queries|字典樹|沒方法

題目大意

給出一些數字,詢問一些滿足對應位為偶數(0表示)或奇數(1表示)的數字的個數。
比如010格式的數字可以有818,52,98,2212等。

題目翻譯

今天Sonya學到了長整數並且邀請了她的朋友分享喜悅之情。Sonya一開始有一個空的存整數的多重集合。朋友們給她t(1t100000)個詢問,每一個詢問有以下幾種型別:
1. + ai – 新增一個非負整數ai(0ai1018)到多重集合中。注意,她有一個多重集合,故集合中1個整數可以出現多次。
2. - ai – 刪掉一個在多重集合中已經有的非負整數ai。保證該整數存在。
3. ? s(length(s)18) – 求多重集合中特定整數的個數(重複的也要算),特定整數指的是由0和1組成的模板s,在s中,0表示偶數,1表示奇數。一個整數x匹配模板s僅當x從右數起的第i位數字符合模板s從右數起的第i位。如果模板s比x短,可以在s前加一些0。類似的,如果x比s短,也可以在x前加一些0。保證至少有一個‘?’型詢問。

比如:如果模板s=010,那麼92,2122,50,414都是匹配s的,而3,110,25,1030不匹配。

法1

首先各位數具體為多少並沒有什麼關係,所以全都模2,將長度擴充套件到18方便位數不相等的查詢。
然後扔到字典樹裡查詢就好啦。

法2

9.25
後來又覺得自己好蠢。。
開個陣列不就好了。。字典樹什麼的根本不需要啊。

沒方法法

#include <cstdio>
typedef long long ll;
const int N = 262144;
int c[N];
int main() {
    int t, i, p; ll d; char op;
    scanf
("%d", &t); while (t--) { for (op = getchar(); op == '\n'; op = getchar()); scanf("%I64d", &d); for (i = p = 0; i < 18; ++i) p |= ((d % 10) & 1) << i, d /= 10; switch(op) { case '+': ++c[p]; break; case '-': --c[p]; break
; case '?': printf("%d\n", c[p]); break; } } return 0; }

字典樹法

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 500005;
int cnt = 0;
int c[N][2], val[N];
void modify(char *s, int d) {
    int i, p = 0, x, l = strlen(s);
    reverse(s, s + l);
    for (; l < 19; ++l) s[l] = '0';
    for (i = 0; i < l; ++i) {
        x = (s[i] - '0') & 1;
        if (!c[p][x]) c[p][x] = ++cnt;
        p = c[p][x]; val[p] += d;
    }
}
int query(char *s) {
    int i, b = 0, j, p = 0, x, l = strlen(s);
    reverse(s, s + l);
    for (; l < 19; ++l) s[l] = '0';
    for (i = 0; i < l; ++i) {
        x = (s[i] - '0') & 1;
        if (!c[p][x]) return 0;
        p = c[p][x];
    }
    return val[p];
}
char valid_char() {
    char ch = getchar();
    while (ch == ' ' || ch == '\n') ch = getchar();
    return ch;
}
char d[N];
int main() {
    int t; char op;
    scanf("%d", &t);
    while (t--) {
        op = valid_char(); scanf("%s", d);
        switch(op) {
        case '+': modify(d, 1); break;
        case '-': modify(d, -1); break;
        case '?': printf("%d\n", query(d)); break;
        }
    }
    return 0;
}

A. Sonya and Queries

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 queries, each of one of the following type:

 +  ai — add non-negative integer ai to the multiset. Note, that she has a multiset, thus there may be many occurrences of the same integer.
 -  ai — delete a single occurrence of non-negative integer ai from the multiset. It’s guaranteed, that there is at least one ai in the multiset.
? 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 with 0-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 do not.

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

Note

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

1 and 241.
361.
101 and 361.
361.
4000.

相關推薦

CodeForces 713A|Sonya and Queries|字典|方法

題目大意 給出一些數字,詢問一些滿足對應位為偶數(0表示)或奇數(1表示)的數字的個數。 比如010格式的數字可以有818,52,98,2212等。 題目翻譯 今天Sonya學到了長整數並且邀請了她的朋友分享喜悅之情。Sonya一開始有一個空的存整數

Codeforces Problem 713A Sonya and Queries(字典)

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 he

Codeforces Round #482 (Div. 2)D. Kuro and GCD and XOR and SUM+字典

添加 get else push_back const con 節點 http fin 題目鏈接:D. Kuro and GCD and XOR and SUM 題意:兩種操作:第一種給數組添加一個數,第二種輸入x,k,s,要求從數組中找到一個數v,要求k能整除gcd(k

[CodeForces - 678F] Lena and Queries 線段維護凸包

d+ operator size 平面 oid == pen txt stdin    大致題意:       給出三種操作         1、往平面點集中添加一個點         2、刪除第i次添加的點         3、給出一個q,詢問平面點集中的q*x+y的最大

Codeforces 948D Perfect Security(字典)

out force 開始 problem codeforce int get 字典 數量 題目鏈接:Perfect Security 題意:給出N個數代表密碼,再給出N個數代表key。現在要將key組排序,使key組和密碼組的亦或所形成的組字典序最小。 題解:要使密碼組裏面

Choosing The Commander CodeForces - 817E (01字典+思維)

integer 異或運算 got from != rac pri 尊重 org As you might remember from the previous round, Vova is currently playing a strategic game known a

Codeforces 145E Lucky Queries 線段

hang max sign style ces ref 線段 unsigned lazy Lucky Queries 感覺是很簡單的區間合並, 但是好像我寫的比較麻煩。 #include<bits/stdc++.h> #define LL long l

Codeforces 514C. Watto and Mechanism解題報告(字典

test names cin 單詞 turn void can include 傳送門 傳送門 題意:給你一個字典和一些詢問,問你對於每個詢問的字符串必須更改一個字符,能否得到字典中的某一個單詞。 思路:先構造一顆字典樹,然後搜一遍就行了,要註意strlen不能每次都用,常

Codeforces 842D Vitya and Strange Lesson【逆向思維+字典查詢亦或最小值】

D. Vitya and Strange Lesson time limit per test 2 seconds memory limit per test 256 megabytes input standard input output

字典3道水題)codeforces 665E&282E&514C

eps trie sub amp ret sea 動態 應該 signed 665E 題意: 給一個數列和一個整數k,求這個數列中異或起來大於等於k的子串數量。 分析: 其實只要維護一個維護前綴和就行了,把前綴和加到字典樹裏,然後遞歸search一下,註意需要剪枝,

codechef Xor Queries (可持久化字典)

names truct codec eee one root opened mes main 題目鏈接:codechef Xor Queries 題意: 題解: 一棵可持久化字典樹就行了。 1 #include<bits/stdc++.h> 2 #de

CodeForces - 816B Karen and Coffee (線段的區間插入+單點查詢)

one 代碼 recipe http contain att amp next splay To stay woke and attentive during classes, Karen needs some coffee! Karen, a coffee afic

Educational Codeforces Round 37 (Rated for Div. 2)F. SUM and REPLACE+線段

namespace ted amp return Education span num sign define 題目鏈接:F. SUM and REPLACE 題意:給一個數組,兩種操作,第一種把[L,R]的數變成這個數的因子個數(這個是log級別的下降),第二種求[L,

Codeforces Round #470 (Div 2) B 數學 C 二分+狀數組 D 字典

fin -i insert 數組 字典 main esp ace blog Codeforces Round #470 B. Primal Sport 數學題,對 x2 和 x1 分解質因子即可。 #include<bits/stdc++.h>

Codeforces Round #393 (Div. 2) (8VC Venture Cup 2017 - Final Round Div. 2 Edition) E - Nikita and stack 線段好題

long long 但是 make div 計算 space its void lse http://codeforces.com/contest/760/problem/E 題目大意:現在對棧有m個操作,但是順序是亂的,現在每輸入一個操作要求你輸出當前的棧頂, 註意,

A .Gaby And Addition (Gym - 101466A + 字典

png tac root cstring size bit cos 更新 typedef 題目鏈接:http://codeforces.com/gym/101466/problem/A 題目: 題意:   給你n個數,重定義兩個數之間的加法不進位,求這些數

codeforces CF920F SUM and REPLACE 線段 線性篩約數

arrow 替換 input void set 範圍 HERE lld 數據 $ \Rightarrow $ 戳我進CF原題 F. SUM and REPLACE time limit per test: 2 seconds memory limit per test:

Berland and the Shortest Paths CodeForces - 1005F(最短路

pac mes ber lan short nbsp ont fin ini 最短路樹就是用bfs走一遍就可以了 d[v] = d[u] + 1 表示v是u的前驅邊 然後遍歷每個結點 存下它的前驅邊 再用dfs遍歷每個結點 依次取每個結點的某個前驅邊即可 #inc

codeforces CF718C Sasha and Array 線段維護矩陣

read ctu his memory long long ORC efi end complex $ \Rightarrow $ 戳我進CF原題 C. Underground Lab time limit per test: 1 second memory limit

A - Gaby And Addition Gym - 101466A --字典 ,暴力+貪心

題目連結 :http://codeforces.com/gym/101466/problem/A A. Gaby And Addition time limit per test 6.0 s memory limit per test 1024 MB input stan