富豪凯匹配串

题目描述:

链接:https://ac.nowcoder.com/acm/contest/1114/C
来源:牛客网

有n个长度为m的文本串,每个串只含有'0'和'1'。接下来有Q次询问,每次给出一个长度为m的字符串,且只含有'0','1'和'_'。如10_1_1。下划线可以匹配'0'或'1'。即10_1_1可以匹配101111,101101,100111,100101四种串。每次询问求出n个文本串中有多少个可以与当前询问的串匹配。

输入描述:

第一行输入n,m
接下来n行,每行输入一个长度为m的01串表示一个文本串。
第n+2行输入Q
接下来Q行,每行输入一个长度为m的字符串(只包含'0','1','_')。
1<=n,m<=1000,1<=Q<=3000。

输出描述:

对于每次询问,输出n个文本串中有多少个与当前询问的串匹配。
示例1

输入

5 6
101101
011011
100110
111000
101111
2
1011_1
1__1__

输出

2
3

状态压缩
bitset的使用

#include <bits/stdc++.h>
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath> 
#pragma GCC optimize(2)

using namespace std;
typedef long long ll;
const int N = 1e3 + 5, mod = 1e9 + 9;
int n, m, qq;
bitset<N> Map[N], p, q;
char s[N];


int main()
{
//    cin.tie(0);
//    cout.tie(0);
//    ios::sync_with_stdio(0);
    cin >> n >> m;
    for (int i = 0; i < n; ++i){
        scanf("%s", s);
        for (int j = 0; j < m; ++j)
            if (s[j] == '1') Map[i][j] = 1;
            else Map[i][j] = 0;
    }
    cin >> qq;
    while (qq--){
        scanf("%s", s);
        for (int i = 0; i < m; ++i){
            if (s[i] == '_') {
                p[i] = 0, q[i] = 0;
            } else {
                p[i] = 1;
                q[i] = s[i] == '1' ? 1 : 0;
            }
        }
        
        int ans = 0;
        for (int i = 0; i < n; ++i){
            if ((p & Map[i]) == q) {
                ans ++;
            }
        }
        
        cout << ans << endl;
    }
}
View Code

猜你喜欢

转载自www.cnblogs.com/mwh123/p/11671044.html
今日推荐