Problem p00891 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <set>
using namespace std;
typedef unsigned long long ull;
int n, m, p;
char code[1010][1010];
bool ma[1010][1010];
bool pattern[1010][1010];

ull ha[1010][1010], tmp[1010][1010];
ull mh[1010][1010];
void compute_hash(bool a[1010][1010], int n, int m) {
  const ull B1 = 9983;
  const ull B2 = 100000007;
  ull t1 = 1;
  for (int j = 0; j < p; j++)
    t1 *= B1;

  for (int i = 0; i < n; i++) {
    ull e = 0;
    for (int j = 0; j < p; j++)
      e = e * B1 + a[i][j];

    for (int j = 0; j + p <= m; j++) {
      tmp[i][j] = e;
      if (j + p < m)
        e = e * B1 - t1 * a[i][j] + a[i][j + p];
    }
  }

  ull t2 = 1;
  for (int i = 0; i < p; i++)
    t2 *= B2;

  for (int j = 0; j + p <= m; j++) {
    ull e = 0;
    for (int i = 0; i < p; i++)
      e = e * B2 + tmp[i][j];

    for (int i = 0; i + p <= n; i++) {
      ha[i][j] = e;
      if (i + p < n)
        e = e * B2 - t2 * tmp[i][j] + tmp[i + p][j];
    }
  }
}

int getbit(char ch) {
  if (ch >= 'A' && ch <= 'Z')
    return 0 + ch - 'A';
  if (ch >= 'a' && ch <= 'z')
    return 26 + ch - 'a';
  if (ch >= '0' && ch <= '9')
    return 52 + ch - '0';
  if (ch == '+')
    return 62;
  return 63;
}

void decode(bool mat[1010][1010], int h, int w) {
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < (w + 5) / 6; j++) {
      int bit = getbit(code[i][j]);
      for (int k = 0; k < 6; k++) {
        mat[i][j * 6 + k] = ((bit >> (5 - k)) & 1) == 1;
      }
    }
  }
}

void init() {
  for (int i = 0; i < n; i++)
    scanf("%s", code[i]);
  decode(ma, n, m);
  for (int i = 0; i < p; i++)
    scanf("%s", code[i]);
  decode(pattern, p, p);
}

bool tm[1010][1010];
void rotate() {
  for (int i = 0; i < p; i++) {
    for (int j = 0; j < p; j++) {
      tm[i][j] = pattern[j][p - 1 - i];
    }
  }
  for (int i = 0; i < p; i++)
    for (int j = 0; j < p; j++)
      pattern[i][j] = tm[i][j];
}
void mirror() {
  for (int i = 0; i < p; i++) {
    for (int j = 0; j < p; j++) {
      tm[i][j] = pattern[i][p - 1 - j];
    }
  }
  for (int i = 0; i < p; i++)
    for (int j = 0; j < p; j++)
      pattern[i][j] = tm[i][j];
}

set<ull> seen;

void solve() {
  seen.clear();
  int ans = 0;
  compute_hash(pattern, p, p);
  ull hp = ha[0][0];
  seen.insert(hp);
  compute_hash(ma, n, m);
  for (int i = 0; i + p <= n; i++) {
    for (int j = 0; j + p <= m; j++) {
      if (ha[i][j] == hp)
        ans++;
      mh[i][j] = ha[i][j];
    }
  }
  for (int k = 0; k < 7; k++) {
    if (k == 3)
      mirror();
    else
      rotate();
    compute_hash(pattern, p, p);
    hp = ha[0][0];
    if (seen.find(hp) != seen.end())
      continue;
    seen.insert(hp);
    for (int i = 0; i + p <= n; i++) {
      for (int j = 0; j + p <= m; j++) {
        if (mh[i][j] == hp)
          ans++;
      }
    }
  }
  printf("%d\\n", ans);
}

int main() {
  for (;;) {
    scanf("%d%d%d", &m, &n, &p);
    if (n == 0 && m == 0 && p == 0)
      break;
    init();
    solve();
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define Max 2505
#define FI first
#define SE second
#define ll long long
#define PI acos(-1.0)
#define inf 0x3fffffff
#define LL(x) (x << 1)
#define bug puts("here")
#define PII pair<int, int>
#define RR(x) (x << 1 | 1)
#define mp(a, b) make_pair(a, b)
#define mem(a, b) memset(a, b, sizeof(a))
#define REP(i, s, t) for (int i = (s); i <= (t); ++i)

using namespace std;
int w, h, p;
string hs[111], ps[111];
int mp[1111][1111];
int pt[1111][1111];
vector<vector<int>> next;
map<char, int> MP;

void init() {
  for (int i = 0; i < 26; i++)
    MP[i + 'A'] = i;
  for (int i = 26; i < 52; i++)
    MP[i - 26 + 'a'] = i;
  for (int i = 52; i < 62; i++)
    MP[i - 52 + '0'] = i;
  MP['+'] = 62;
  MP['-'] = 63;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < (w + 5) / 6; j++) {
      mp[i][j] = 0;
    }
  }
  for (int i = 0; i < p; i++) {
    for (int j = 0; j < (p + 5) / 6; j++) {
      pt[i][j] = 0;
    }
  }
  for (int i = 0; i < h; i++) {
    cin >> hs[i];
    for (int j = 0; j < hs[i].length(); j++) {
      int a = MP[hs[i][j]];
      for (int k = 0; k < 6; k++) {
        int res = (a & (1 << k));
        mp[i][k * 6 + k] = res;
      }
    }
  }
  for (int i = 0; i < p; i++) {
    cin >> ps[i];
    for (int j = 0; j < ps[i].length(); j++) {
      int a = MP[ps[i][j]];
      for (int k = 0; k < 6; k++) {
        int res = (a & (1 << k));
        pt[i][k * 6 + k] = res;
      }
    }
  }
}
vector<int> match(int m[], int n) {
  vector<int> mnext(0, n);
  for (int i = 1; i < n; i++) {
    int j = i;
    while (j > 0) {
      j = mnext[j];
      if (m[j] == m[i]) {
        mnext[i] = j + 1;
        break;
      }
    }
  }
  return mnext;
}
void rotate() {}
void initNext(int index) {}
void solve() {}
int main() {
  while (cin >> w >> h >> p, w && h && p) {
    init();
    solve();
  }
  return 0;
}