题目大意
给你一些01串 (1000个串,串长1000)
然后给你q(3000)个询问 ,每个询问给一串 ,如1__0__
由1,0,_ 组成,_ 表示可以匹配0或者1
题目分析
这个题显然不能暴力匹配
由于符号只有0和1
所以我们可以发现
如果我们要匹配1
1&1 = 1
如果我们要匹配0
1&0 = 0
如果我们要匹配_
0&1 = 0 ,0&0 =0
因此,我们就用两个01串相& 计算答案就可以了
可以用bitset加速一下
代码详解
#include <bits/stdc++.h>
using namespace std;
const int maxn =1e3+50;
char ch[maxn][maxn];
bitset<1050>s[maxn];
char k[maxn];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
s[i].reset();
scanf("%s",ch[i]);
for(int j=0;j<m;j++)
{
s[i][j] = ch[i][j]-'0';
}
}
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s",k);
bitset<1050>q;
bitset<1050>tag;
for(int j=0;j<m;j++)
{
if(k[j]=='_') q[j]=0,tag[j]=0;
else
{
if(k[j]=='0')
q[j] = 1,tag[j]=0;
else q[j] = tag[j] = 1;
}
}
int ans =0;
for(int i=1;i<=n;i++)
{
//cout<<q<<" "<<s[i]<<endl;
if((s[i]&q)==tag) ans++;
}
// int ans=0;
printf("%d\n",ans);
}
return 0;
}