牛客练习赛53 C 富豪凯匹配串

思路:

bitset的简单题,不幸的是当时的我并不知道bitset,

C++的 bitset 在 bitset 头文件中,它是一种类似数组的结构,它的每一个元素只能是0或1,每个元素仅用1bit空间,省时省空间!

代码:

#include<iostream>
#include<stdio.h>
#include<bitset>
#include<string>
using namespace std;
typedef bitset<1000> BIT;//typedef 不能少,作用:为复杂的声明定义简单的别名 
BIT bit1[1005],bit2,bit3;
string str,str1;
int n,m,q;
int main(){
    cin>>n>>m; 
    for(int i = 1;i<=n;i++){
        cin>>str;
        bit1[i] = BIT(str);
    } 
    cin>>q;
    for(int i = 1;i<=q;i++){
        cin>>str;
        str1 = str;
        int j;
        for(j = 0;j<m;j++){
            if(str[j]=='_')
                str1[j] = str[j] = '0';
            else
                str1[j] = '1'; 
        }
        str1[j] ='\0';
        bit2 = BIT(str1);
        bit3 = BIT(str);
        int ans = 0;
        for(j = 1;j<=n;j++){
            if((bit1[j]&bit2)==bit3)
                ans++;
        }
        cout<<ans<<endl;
    }
    
    return 0;
}

 

posted @ 2019-10-21 19:31  sqsq  阅读(200)  评论(0编辑  收藏  举报