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

#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

typedef unsigned long long ull;

const ull X = 100001;
const ull Y = 9973;
const int MAX = 1005;

ull hash1[MAX][MAX];
ull hash2[MAX][MAX];

void hashCalc(char T[MAX][MAX], int H, int W, int R, int C) {
  // x???X???C???
  ull x = 1;
  for (int i = 0; i < C; i++) {
    x *= X;
  }

  ull y = 1;
  for (int i = 0; i < R; i++) {
    y *= Y;
  }

  for (int i = 0; i < H; i++) {
    ull h = 0;
    for (int j = 0; j < C; j++) {
      h = h * X + T[i][j];
    }
    for (int k = 0; k + C <= W; k++) {
      hash1[i][k] = h;
      h = h * X + T[i][k + C] - T[i][k] * x;
    }
  }

  for (int j = 0; j + C <= W; j++) {
    ull h = 0;
    for (int i = 0; i < R; i++) {
      h = h * Y + hash1[i][j];
    }
    for (int k = 0; k + R <= H; k++) {
      hash2[k][j] = h;
      h = h * Y + hash1[k + R][j] - hash1[k][j] * y;
    }
  }
}

int main() {
  string s, t;

  int H, W;
  cin >> H >> W;
  char T[MAX][MAX];
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> T[i][j];
    }
  }

  int R, C;
  cin >> R >> C;
  char P[R][C];
  for (int i = 0; i < R; i++) {
    for (int j = 0; j < C; j++) {
      cin >> P[i][j];
    }
  }

  ull tmp[R];

  hashCalc(T, H, W, R, C);

  for (int i = 0; i < R; i++) {
    ull h = 0;
    for (int j = 0; j < C; j++) {
      h = h * X + P[i][j];
    }
    tmp[i] = h;
  }

  ull hs = 0;
  for (int i = 0; i < R; i++) {
    hs = hs * Y + tmp[i];
  }

  for (int i = 0; i + R <= H; i++) {
    for (int j = 0; j + C <= W; j++) {
      if (hash2[i][j] == hs) {
        cout << i << " " << j << endl;
      }
    }
  }
  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

typedef unsigned long long ull;

const ull X = 3;
const ull Y = 5;
const int MAX = 105;

ull hash1[MAX][MAX];
ull hash2[MAX][MAX];

void hashCalc(int T[MAX][MAX], int H, int W, int R, int C) {
  // x???X???C???
  ull x = 1;
  for (int i = 0; i < C; i++) {
    x *= X;
  }

  ull y = 1;
  for (int i = 0; i < R; i++) {
    y *= Y;
  }

  for (int i = 0; i < H; i++) {
    ull h = 0;
    for (int j = 0; j < C; j++) {
      h = h * X + T[i][j];
    }
    for (int k = 0; k + C <= W; k++) {
      hash1[i][k] = h;
      h = h * X + T[i][k + C] - T[i][k] * x;
    }
  }

  for (int j = 0; j + C <= W; j++) {
    ull h = 0;
    for (int i = 0; i < R; i++) {
      h = h * Y + hash1[i][j];
    }
    for (int k = 0; k + R <= H; k++) {
      hash2[k][j] = h;
      h = h * Y + hash1[k + R][j] - hash1[k][j] * y;
    }
  }
}

int main() {
  int H, W;
  cin >> H >> W;
  int T[MAX][MAX];
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> T[i][j];
    }
  }

  int R, C;
  cin >> R >> C;
  int P[R][C];
  for (int i = 0; i < R; i++) {
    for (int j = 0; j < C; j++) {
      cin >> P[i][j];
    }
  }

  ull tmp[R];

  hashCalc(T, H, W, R, C);

  for (int i = 0; i < R; i++) {
    ull h = 0;
    for (int j = 0; j < C; j++) {
      h = h * X + P[i][j];
    }
    tmp[i] = h;
  }

  ull hs = 0;
  for (int i = 0; i < R; i++) {
    hs = hs * Y + tmp[i];
  }

  for (int i = 0; i + R <= H; i++) {
    for (int j = 0; j + C <= W; j++) {
      if (hash2[i][j] == hs) {
        cout << i << " " << j << endl;
      }
    }
  }
  return 0;
}