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

#include <bits/stdc++.h>

using namespace std;

string temp =
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

int main() {
  int W, H, P;
  bool bit[1005][1005];
  bool cur[8][105][105];

  __int128 mask, slow[1005], obj[8][105];

  while (cin >> W >> H >> P, W) {
    for (int i = 0; i < H; i++) {
      string S;
      cin >> S;
      for (int j = 0; j < S.size(); j++) {
        auto bb = temp.find(S[j]);
        for (int k = 0; k < 6; k++) {
          if (j * 6 + k >= W)
            break;
          bit[i][j * 6 + k] = (bb >> (5 - k)) & 1;
        }
      }
    }

    for (int i = 0; i < P; i++) {
      string S;
      cin >> S;
      for (int j = 0; j < S.size(); j++) {
        auto bb = temp.find(S[j]);
        for (int k = 0; k < 6; k++) {
          if (j * 6 + k >= P)
            break;
          cur[0][i][j * 6 + k] = (bb >> (5 - k)) & 1;
        }
      }
    }

    for (int i = 0; i < P; i++) {
      for (int j = 0; j < P; j++) {
        cur[4][i][j] = cur[0][j][i];
      }
    }

    for (int i = 0; i < 8; i++) {
      if (i % 4 == 0)
        continue;
      for (int j = 0; j < P; j++) {
        for (int k = 0; k < P; k++) {
          cur[i][j][k] = cur[i - 1][k][P - j - 1];
        }
      }
    }

    mask = ((__int128)1 << P) - 1;
    for (int i = 0; i < 8; i++) {
      for (int j = 0; j < P; j++) {
        obj[i][j] = 0;
        for (int k = 0; k < P; k++) {
          obj[i][j] <<= 1;
          obj[i][j] |= cur[i][j][k];
        }
      }
    }

    for (int i = 0; i < H; i++) {
      slow[i] = 0;
      for (int j = 0; j < P; j++) {
        slow[i] <<= 1;
        slow[i] |= bit[i][j];
      }
    }

    int ret = 0;

    for (int j = 0; j <= W - P; j++) {
      for (int i = 0; i <= H - P; i++) {
        for (int k = 0; k < 8; k++) {
          bool rematch = true;
          for (int l = 0; l < P; l++) {
            rematch &= slow[i + l] == obj[k][l];
          }
          if (rematch) {
            ++ret;
            break;
          }
        }
      }
      for (int i = 0; i < H; i++) {
        slow[i] = (slow[i] << 1 | bit[i][j + P]) & mask;
      }
    }
    cout << ret << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

string temp =
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

int main() {
  int W, H, P;
  bitset<1000> bit[1000], cur[8][100], nest;

  while (cin >> W >> H >> P, W) {
    for (int i = 0; i < H; i++) {
      string S;
      cin >> S;
      for (int j = 0; j < S.size(); j++) {
        auto bb = temp.find(S[j]);
        for (int k = 0; k < 6; k++) {
          if (j * 6 + k >= W)
            break;
          bit[i][j * 6 + k] = (bb >> (5 - k)) & 1;
        }
      }
    }
    for (int i = 0; i < 100; i++) {
      nest[i] = i < P;
    }

    for (int i = 0; i < P; i++) {
      string S;
      cin >> S;
      for (int j = 0; j < S.size(); j++) {
        auto bb = temp.find(S[j]);
        for (int k = 0; k < 6; k++) {
          if (j * 6 + k >= P)
            break;
          cur[0][i][j * 6 + k] = (bb >> (5 - k)) & 1;
        }
      }
    }

    for (int i = 0; i < P; i++) {
      for (int j = 0; j < P; j++) {
        cur[4][i][j] = cur[0][j][i];
      }
    }

    for (int i = 0; i < 8; i++) {
      if (i % 4 == 0)
        continue;
      for (int j = 0; j < P; j++) {
        for (int k = 0; k < P; k++) {
          cur[i][j][k] = cur[i - 1][k][P - j - 1];
        }
      }
    }

    int ret = 0;
    for (int i = 0; i <= H - P; i++) {
      for (int j = 0; j <= W - P; j++) {
        for (int k = 0; k < 8; k++) {

          bool rematch = true;
          for (int l = 0; l < P; l++) {
            rematch &= cur[k][l] == ((bit[i + l] >> j) & nest);
            if (!rematch)
              break;
          }

          if (rematch) {
            ++ret;
            break;
          }
        }
      }
    }
    cout << ret << endl;
  }
}