zoukankan      html  css  js  c++  java
  • Codeforces Round #371 (Div. 2) C. Sonya and Queries

    C. Sonya and Queries
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard 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 queries, each of one of the following type:

    1.  +  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.
    2.  -  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.
    3. ? 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. 1 and 241.
    2. 361.
    3. 101 and 361.
    4. 361.
    5. 4000.    
      /*
      给你一个容器你可以向里添加元素,也可以删除里面已有的元素
      询问:输入一个二进制数1表示奇数0表示偶数,返回对应的数的个数,所有询问都是从右边开始,位数不够用0补充
      
      字典树,插入01数,删除的地方只需要将对应字段末尾减1就可以
      
      */
      #include <bits/stdc++.h>
      using namespace std;
      const int N = 5000000+10;
      struct Trie
      {
          int ch[N][3];
          int sz;
          int val[N];
          void Init()
          {
              sz=1;
              memset(ch,0,sizeof ch);
              memset(val,0,sizeof val);
          }
          /*
          0是偶数  1是奇数
          */
          void Insert(char *num,int op)//插异或
          {
              //cout<<"num="<<num<<endl;
              int u=0;
              for(int i=20;i>=strlen(num);i--)
              {
                  if(ch[u][0]==0)///无该儿子
                      ch[u][0]=sz++;
                  u=ch[u][0];
              }
              for(int i=0;i<strlen(num);i++)
              {
                  int c=(num[i]-'0')%2;
                  //cout<<"cur="<<cur<<" i="<<i<<" c="<<c<<endl;
                  if(ch[u][c]==0)///无该儿子
                      ch[u][c]=sz++;
                  u=ch[u][c];
              }
              val[u]+=op;//表示当前结点的单词数
          }
      
          int Find(char *num)
          {
              int u=0;
              int cur=0;
              for(int i=20;i>=strlen(num);i--)
              {
                  if(ch[u][0]==0)///无该儿子
                      return 0;
                  u=ch[u][0];
                  cur+=val[u];
              }
              for(int i=0;i<strlen(num);i++)
              {
                  int c=(num[i]-'0')%2;
                  //cout<<"cur="<<cur<<" i="<<i<<" c="<<c<<endl;
                  if(ch[u][c]==0)///无该儿子
                      return 0;
                  u=ch[u][c];
                  cur+=val[u];
              }
              return cur;
          }
      };
      Trie trie;
      int main()
      {
          //freopen("C:\Users\acer\Desktop\in.txt","r",stdin);
          trie.Init();
          int q;
          scanf("%d",&q);
          getchar();
          while(q--)
          {
              char s[2];
              char x[20];
              scanf("%s%s",s,x);
              //cout<<"x="<<x<<endl;
              if(s[0]=='+')
                  trie.Insert(x,1);
              if(s[0]=='?')
                  printf("%d
      ",trie.Find(x));
              if(s[0]=='-')
                  trie.Insert(x,-1);
          }
          return 0;
      }
      /*
      最后附上大神代码......大神的世界没有知识点只有乱搞
      */
      
      #include <bits/stdc++.h>
      using namespace std;
      int cnt[1<<18];
      int main()
      {
          int t;
          scanf("%d",&t);
          while(t--)
          {
              char ty[5],buf[25];
              scanf("%s%s",ty,buf);
              int tmp=0;
              for(int i=0;buf[i];i++)
                  tmp=tmp*2+(buf[i]-'0')%2;
              if(*ty=='?')printf("%d
      ",cnt[tmp]);
              else cnt[tmp]+=(*ty=='+' ? 1 : -1);
          }
          return 0;
      }
  • 相关阅读:
    find 以及linux 和windows 文件互传
    27 关于Object类 -- equals()
    26 super关键字
    25 访问修饰符限定词
    24 继承 子类中方法重写
    23 static 关键字
    22 包(package)来解决不同包下同名文件的冲突问题
    21 封装(get,set方法)
    20 带参构造方法(this关键字)
    19 无参构造方法
  • 原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/6031497.html
Copyright © 2011-2022 走看看