Problem p00373 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

using bs = bitset<800>;
using ss = bitset<100>;

int main() {
  int W, H, M, N;
  int mat[800][800], beet[100][100];

  auto to = [&](char c) {
    if (islower(c))
      return (c - 'a' + 10);
    if (isupper(c))
      return (c - 'A' + 36);
    if (isdigit(c))
      return (c - '0' + 0);
    return (~0);
  };

  cin >> W >> H >> M >> N;
  for (int i = 0; i < H; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < W; j++) {
      mat[i][j] = to(s[j]);
    }
  }

  for (int i = 0; i < N; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < M; j++) {
      beet[i][j] = to(s[j]);
    }
  }

  ss mask = 1;
  mask <<= M - 1;

  auto matching = [&](vector<int> &latte, vector<int> &malta) {
    bs ret;
    int tail = 0;
    ss bit1[62], bit2;
    for (int i = 0; i < malta.size(); i++) {
      if (malta[i] < 0)
        for (int j = 0; j < 62; j++)
          bit1[j][i] = true;
      else
        bit1[malta[i]][i] = true;
    }
    for (int i = 0; i < W; i++) {
      bit2 <<= 1;
      bit2 |= 1;
      if (~latte[i])
        bit2 &= bit1[latte[i]];
      if ((bit2 & mask).count())
        ret[i] = true;
    }
    return (ret);
  };

  bs bits[800][100];
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < N; j++) {
      vector<int> vec1(W), vec2(M);
      for (int k = 0; k < W; k++)
        vec1[k] = mat[i][k];
      for (int k = 0; k < M; k++)
        vec2[k] = beet[j][k];
      bits[i][j] = matching(vec1, vec2);
    }
  }

  int ret = 0;
  for (int i = 0; i <= H - N; i++) {
    for (int k = 0; k < W; k++) {
      bool flag = true;
      for (int j = 0; j < N; j++) {
        flag &= bits[i + j][j][k];
      }
      ret += flag;
    }
  }

  cout << ret << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using bs = bitset<800>;

int main() {
  int W, H, M, N;
  int mat[800][800], beet[100][100];

  auto to = [&](char c) {
    if (islower(c))
      return (c - 'a' + 10);
    if (isupper(c))
      return (c - 'A' + 36);
    if (isdigit(c))
      return (c - '0' + 0);
    return (62);
  };

  cin >> W >> H >> M >> N;
  for (int i = 0; i < H; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < W; j++) {
      mat[i][j] = to(s[j]);
    }
  }

  for (int i = 0; i < N; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < M; j++) {
      beet[i][j] = to(s[j]);
    }
  }

  bitset<100> bit1[100][63];
  for (int k = 0; k < N; k++) {
    for (int i = 0; i < M; i++) {
      for (int j = 0; j < 63; j++)
        bit1[k][j] <<= 1;
      bit1[k][beet[k][i]] |= 1;
    }
  }

  auto matching = [&](vector<int> &latte, int malta) {
    bs ret;
    int tail = 0, question = 0;
    int all = bit1[malta][62].count();
    bitset<100> bit2[63];

    for (int i = 0; i <= W - M; i++) {
      while (tail - i < M) {
        for (int j = 0; j < 63; j++) {
          bit2[j] <<= 1LL;
        }
        if (latte[tail] == 62)
          ++question;
        bit2[latte[tail]] |= 1;
        ++tail;
      }
      int sum = 0;
      for (int j = 0; j < 63; j++) {
        sum += (bit1[malta][j] & bit2[j]).count();
      }
      int sz = sum + question + all - (bit1[malta][62] & bit2[62]).count() * 2;
      if (sz == M)
        ret[i] = true;
      if (latte[i] == 62)
        --question;
    }
    return (ret);
  };

  bs bits[800][100];
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < N; j++) {
      vector<int> vec1(W), vec2(M);
      for (int k = 0; k < W; k++)
        vec1[k] = mat[i][k];
      bits[i][j] = matching(vec1, j);
    }
  }

  int ret = 0;
  for (int i = 0; i <= H - N; i++) {
    for (int k = 0; k < W; k++) {
      bool flag = true;
      for (int j = 0; j < N; j++) {
        flag &= bits[i + j][j][k];
      }
      ret += flag;
    }
  }

  cout << ret << endl;
}