集合询问
题目 集合询问
思路分析
反过来想 把每个串变成01序列的形式
再拿01序列做询问
又是键值 计数 可以用值做下标写 也可以unordered_map写
代码实现
#include<bits/stdc++.h>
using namespace std;
unordered_map<int,int> h;
int n;
int main()
{
cin>>n;
char op[2],str[20];
while(n--){
cin>>op>>str;
int x=0;
for(int i=0;str[i];i++)
x=x*2+((str[i]-'0')&1);
if(*op=='+')
h[x]++;
else if(*op=='-')
h[x]--;
else
cout<<h[x]<<endl;
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
const int N=1<<18;
int cnt[N];
int n;
int main()
{
cin>>n;
char op[2],str[20];
while(n--){
cin>>op>>str;
int x=0;
for(int i=0;str[i];i++)
x=x*2+((str[i]-'0')&1);
if(*op=='+')
cnt[x]++;
else if(*op=='-')
cnt[x]--;
else
cout<<cnt[x]<<endl;
}
return 0;
}
💬 评论