概述
字典树模板题,自己又敲了一遍,1A,开心o(* ̄▽ ̄*)ブ,代码比较美观了,思维也是清晰了许多。
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<map>
#include<cmath>
using namespace std;
typedef long long ll;
int n,m,q,p;
int ch[32],chh[32],a,b;
int rt[3000005][2],num,kk[3000005];
void add()
{
int root=0;
for(int i=0;i<=31;i++)
{
if(rt[root][ch[i]]) root=rt[root][ch[i]];
else root=rt[root][ch[i]]=++num;
kk[root]++;
}
}
void del()
{
int root=0;
for(int i=0;i<=31;i++)
{
if(rt[root][ch[i]]) root=rt[root][ch[i]];
else return;
kk[root]--;
}
}
void query()
{
int ans=0;
int root=0;
for(int i=0;i<=31;i++)
{
if(chh[i]==0)
{
if(rt[root][ch[i]]) root=rt[root][ch[i]];
else break;
}
else
{
ans+=kk[rt[root][ch[i]]];
if(rt[root][!ch[i]]) root=rt[root][!ch[i]];
else break;
}
}
printf("%dn",ans);
return;
}
int main()
{
scanf("%d",&q);
while(q--)
{
scanf("%d",&p);
if(p==1)
{
scanf("%d",&a);
int k=0;
for(int i=31;i>=0;i--)
{
if((1<<i)&a) ch[k++]=1;
else ch[k++]=0;
}
add();
}
else if(p==2)
{
scanf("%d",&a);
int k=0;
for(int i=31;i>=0;i--)
{
if((1<<i)&a) ch[k++]=1;
else ch[k++]=0;
}
del();
}
else if(p==3)
{
scanf("%d%d",&a,&b);
int k=0;
for(int i=31;i>=0;i--)
{
if((1<<i)&a) ch[k]=1;
else ch[k]=0;
if((1<<i)&b) chh[k]=1;
else chh[k]=0;
k++;
}
query();
}
}
}
最后
以上就是淡定西牛为你收集整理的CF 817E --- Choosing The Commander(字典树)的全部内容,希望文章能够帮你解决CF 817E --- Choosing The Commander(字典树)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复