富豪凯匹配串 (异或)

链接: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个文本串中有多少个与当前询问的串匹配。

输入

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

输出

2
3

说明

第一次询问:有101101,101111与1011_1匹配
第二次询问:有101101, 100110, 101111与1__1__匹配

析:一开始我是把每个一行字符串分成了一些32位的数,这样长度为1000,最多只能32个数,然后在查询时,只要把原来数与要查询的数一一比较,就可以知道该串是不是符合,复杂度大约是 O(q*n*30),跑了170ms,后来使用了bitset这个更加方便,一开始没想到,才运行了110ms。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
// #define sz size()
#define be begin()
#define ed end()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
// #define all 1,n,1
#define FOR(i,n,x)  for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.in", "r", stdin)
#define freopenw freopen("out.out", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1000 + 7;
const int maxm = 1e6 + 10;
const int mod = 1e9 + 7;
const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
  return r >= 0 && r < n && c >= 0 && c < m;
}
inline int readInt(){ int x;  scanf("%d", &x);  return x; }


bitset<maxn> a[maxn], b, c;
char t[maxn];

int query(){
  int ans = 0;
  for(int i = 0; i < n; ++i)
    if((a[i]&b) == c)  ++ans;
  return ans;
}

int main(){
  scanf("%d %d", &n, &m);
  for(int i = 0; i < n; ++i){
    scanf("%s", t);
    for(int j = 0; j < m; ++j)  a[i][j] = t[j] - '0';
  }
  int q;  scanf("%d", &q);
  while(q--){
    scanf("%s", t);
    for(int i = 0; i < m; ++i)
      if(t[i] == '0')  b[i] = 1, c[i] = 0;
      else if(t[i] == '1')  b[i] = c[i] = 1;
      else b[i] = c[i] = 0;
    printf("%d
", query());
  }
  return 0;
}

  

原文地址:https://www.cnblogs.com/dwtfukgv/p/11772128.html