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

// #include <bits/stdc++.h>
// #define SIZE 300005
// #define MOD 1000000007LL
// #define INF 1 << 30
// #define LLINF 1LL << 60
// #define REP(i,n) for(int i=0;i<n;i++)
// #define FOR(i,a,b) for(int i=a;i<=b;i++)
// #define DOWN(i,b,a) for(int i=b;i>=a;i--)
// #define SET(a,c) memset(a,c,sizeof a)
// #define BIT(i,j) ((i)>>(j))&1
// #define ALL(o) (o).begin(), (o).end()
// #define ERASE(o) (o).erase(unique((o).begin(),(o).end()), (o).end())
// #define SQ(x) ((x)*(x))
// using namespace std;
// typedef long long ll;
// typedef pair<ll,ll> Pll;
// typedef pair<int, int> Pii;
// typedef pair<double, double> Pdd;
// typedef complex<double> dcomplex;
// template<typename T> inline void
// priv(vector<T>a){REP(i,a.size()){cerr<<a[i]<<((i==a.size()-1)?"\\n":" ");}} ll
// gcd(ll a,ll b){int c=max(a,b);int d=min(a,b);return c==0||d==0?c:gcd(c%d,d);}
// ll lcm(ll a,ll b){return a==0||b==0?0:a*b/gcd(a,b);}
// ll fact(ll a){ll b=1;FOR(i,1,a)b*=i;return b;}

#include <bits/stdc++.h>
#define MOD 1000000007LL
#define C1 401
#define C2 397
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = a; i <= b; i++)

typedef long long ll;

using namespace std;

int h, w, r, c;

vector<vector<ll>> genHash(const vector<string> v, int xs, int ys) {
  vector<vector<ll>> h1(xs, vector<ll>(ys, 0));
  vector<vector<ll>> h2(xs, vector<ll>(ys, 0));
  ll cn1 = 1, cn2 = 1;
  REP(i, c) cn1 = (cn1 * C1) % MOD;
  REP(i, r) cn2 = (cn2 * C2) % MOD;
  REP(i, xs) {
    ll h = 0;
    REP(j, c) h = (h * C1 + v[i][j]) % MOD;
    REP(j, ys - c + 1) {
      h1[i][j] = h;
      if (j + c < ys)
        h = (h * C1 - v[i][j] * cn1 + v[i][j + c]) % MOD;
      h = (h < 0) ? h + MOD : h;
    }
  }
  REP(j, ys) {
    ll h = 0;
    REP(i, r) h = (h * C2 + h1[i][j]) % MOD;
    REP(i, xs - r + 1) {
      h2[i][j] = h;
      if (i + r < xs)
        h = (h * C2 - h1[i][j] * cn2 + h1[i + r][j]) % MOD;
      h = (h < 0) ? h + MOD : h;
    }
  }
  return h2;
}

int main() {
  cin >> h >> w;
  vector<string> a(h);
  REP(i, h) cin >> a[i];
  cin >> r >> c;
  vector<string> b(r);
  REP(i, r) cin >> b[i];
  if (h < r || w < c)
    return 0;
  auto f = genHash(a, h, w);
  auto p = genHash(b, r, c);
  REP(i, h - r + 1)
  REP(j, w - c + 1) if (f[i][j] == p[0][0]) printf("%d %d\\n", i, j);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define MOD 1000000007LL
#define C1 401
#define C2 397
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = a; i <= b; i++)

typedef long long ll;

using namespace std;

int h, w, r, c;

vector<vector<ll>> genHash(const vector<string> v, int xs, int ys) {
  vector<vector<ll>> h1(xs, vector<ll>(ys, 0));
  vector<vector<ll>> h2(xs, vector<ll>(ys, 0));
  ll cn1 = 1, cn2 = 1;
  REP(i, c) cn1 = (cn1 * C1) % MOD;
  REP(i, r) cn2 = (cn2 * C2) % MOD;
  REP(i, xs) {
    ll h = 0;
    REP(j, c) h = (h * C1 + v[i][j]) % MOD;
    REP(j, ys - c + 1) {
      h1[i][j] = h;
      if (j + c < ys)
        h = (h * C1 - v[i][j] * cn1 + v[i][j + c]) % MOD;
      h = (h < 0) ? h + MOD : h;
    }
  }
  REP(j, ys) {
    ll h = 0;
    REP(i, r) h = (h * C2 + h1[i][j]) % MOD;
    REP(i, xs - r + 1) {
      h2[i][j] = h;
      if (i + r < xs)
        h = (h * C2 - h1[i][j] * cn2 + h1[i + r][j]) % MOD;
      h = (h < 0) ? h + MOD : h;
    }
  }
  return h2;
}

int main() {
  cin >> h >> w;
  vector<string> a(h);
  REP(i, h) cin >> a[i];
  cin >> r >> c;
  vector<string> b(r);
  REP(i, r) cin >> b[i];
  auto f = genHash(a, h, w);
  auto p = genHash(b, r, c);
  REP(i, h - r + 1)
  REP(j, w - c + 1) if (f[i][j] == p[0][0]) printf("%d %d\\n", i, j);
  return 0;
}