A. Sonya and Queries
题目大意,
就是给你几种操作,添加, 删除,查询
裸的字典树题目..
#include <algorithm>
#include <stack>
#include <istream>
#include <stdio.h>
#include <map>
#include <math.h>
#include <vector>
#include <iostream>
#include <queue>
#include <string.h>
#include <set>
#include <cstdio>
#define FR(i,n) for(int i=0;i<n;i++)
#define MAX 2005
#define mkp pair <int,int>
using namespace std;
const int maxn = 2e6+40;
typedef long long ll;
const int inf = 0x3fffffff;
void read(int &x) {
char ch; bool flag = 0;
for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
for (x = 0; isdigit(ch); x = (x << 1) + (x << 3) + ch - 48, ch = getchar());
x *= 1 - 2 * flag;
}
char temp[20],str[20];
int tot = 0;
struct Tree
{
int flag[2000000][27];
int sum[2000000];
void buildtree()
{
memset(flag[0],0,sizeof(flag[0]));
memset(sum,0,sizeof(sum));
}
void Insert(int val)
{
int fa=0;
int len = strlen(str);
for(int i=len-1;i>=0;i--)
{
int id = str[i]-'0';
id%=2;
if(!flag[fa][id])
{
flag[fa][id]=++tot;
memset(flag[tot],0,sizeof(flag[tot]));
// memset(sum[tot],0,sizeof(sum[tot]));
}
fa = flag[fa][id];
}
sum[fa]+=val;
}
int getres()
{
int fa=0;
int len = strlen(str);
for(int i=len-1;i>=0;i--)
{
int id = str[i]-'0';
id%=2;
if(!flag[fa][id])return 0;
fa=flag[fa][id];
}
return sum[fa];
}
}tree;
int main() {
int n;
read(n);
tree.buildtree();
while(n--){
char s1;
cin>>s1>>str;
int len=strlen(str);
if(len!=18){
for(int i=0;i<18-len;i++)temp[i]='0';
for(int i=18-len;i<18;i++)temp[i]=str[i-18+len];
for(int i=0;i<18;i++)str[i]=temp[i];
}
if(s1=='+'||s1=='-')
{
if(s1=='+')
tree.Insert(1);
else tree.Insert(-1);
}
else{
cout<<tree.getres()<<endl;
}
}
return 0;
}
我身后空无一人,我怎敢倒下