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

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
ull base = 1000000007;
string i2b(int n) {
  string buf;
  for (int i = 5; i >= 0; i--) {
    if (n >> i & 1)
      buf += "1";
    else
      buf += "0";
  }
  return buf;
}
int s2i(char c) {
  if ('A' <= c && c <= 'Z')
    return (c - 'A');
  if ('a' <= c && c <= 'z')
    return (c - 'a' + 26);
  if ('0' <= c && c <= '9')
    return (c - '0' + 52);
  if (c == '+')
    return 62;
  if (c == '/')
    return 63;
  return -1;
}
int main() {
  int w, h, p;
  while (cin >> w >> h >> p, w || h || p) {
    string st[1005];
    int i, j, k;
    for (i = 0; i < h; i++)
      cin >> st[i];
    string wa[105];
    for (i = 0; i < p; i++)
      cin >> wa[i];

    string co[1005];
    for (i = 0; i < h; i++) {
      for (j = 0; j < st[i].size(); j++)
        co[i] += i2b(s2i(st[i][j]));
      co[i] = co[i].substr(0, w);
      // cout << co[i] <<  endl;
    }

    ull ha[1005][1005] = {{}};
    ull xx;
    for (i = 0; i < h; i++) {
      xx = 1;
      for (k = p - 1; k >= 0; k--) {
        if (co[i][k] == '1')
          ha[i][0] += xx;
        if (k != 0)
          xx *= base;
      }
      for (j = 1; j + p <= w; j++) {
        ha[i][j] = (ha[i][j - 1] - (co[i][j - 1] - '0') * xx) * base +
                   (co[i][j + p - 1] - '0');
      }
    }
    // cout << "x" << endl;

    string wal[105][8];
    for (i = 0; i < p; i++) {
      for (j = 0; j < wa[i].size(); j++) {
        wal[i][0] += i2b(s2i(wa[i][j]));
      }
      wal[i][0] = wal[i][0].substr(0, p);
    }

    for (k = 1; k < 4; k++) {
      for (i = 0; i < p; i++) {
        for (j = p - 1; j >= 0; j--) {
          wal[i][k] += wal[j][k - 1][i];
        }
      }
    }

    for (k = 0; k < 4; k++) {
      for (i = 0; i < p; i++) {
        for (j = p - 1; j >= 0; j--) {
          wal[i][k + 4] += wal[i][k][j];
        }
      }
    }
    bool same[8] = {};
    for (i = 0; i < 8; i++) {
      for (j = i + 1; j < 8; j++) {
        if (same[i])
          break;
        same[i] = true;
        for (k = 0; k < p; k++) {
          if (wal[k][i] != wal[k][j])
            same[i] = false;
        }
      }
    }

    // cout << "y" << endl;

    ull has[105][8] = {{}};
    int x, y;
    ull ans = 0;
    for (k = 0; k < 8; k++) {
      if (same[k])
        continue;
      // cout << endl;
      // for(i=0;i<p;i++) cout << wal[i][k] << endl;
      for (i = 0; i < p; i++) {
        xx = 1;
        for (j = p - 1; j >= 0; j--) {
          if (wal[i][k][j] == '1')
            has[i][k] += xx;
          xx *= base;
        }
        // cout << has[i][k] << endl;
      }
      // cout << k << endl;
      for (i = 0; i + p <= h; i++) {
        for (j = 0; j + p <= w; j++) {
          // cout << ans <<":"<<i <<"/"<<j<<endl;
          // cout << ha[i][j] << " " << has[i][k] << endl;
          bool f = true;
          for (y = 0; y < p; y++) {
            // cout << ha[i+y][j] << " " << has[i+y][k] << endl;
            if (ha[i + y][j] != has[y][k]) {
              // cout << ha[i+y][j] << " " << has[y][k] << endl;
              // cout << ans <<":"<<i+y <<"/"<<j<<endl;
              f = false;
              break;
            }
          }
          /*
          for(x=0;x<p;x++){
            for(y=0;y<p;y++){
              if(co[i+y][j+x]!=wal[y][k][x]){
                f=false;
                break;
              }
            }
            if(!f) break;
          }
          */
          if (f) {
            ans++;
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
ull base = 1000000007;
string i2b(int n) {
  string buf;
  for (int i = 5; i >= 0; i--) {
    if (n >> i & 1)
      buf += "1";
    else
      buf += "0";
  }
  return buf;
}
int s2i(char c) {
  if ('A' <= c && c <= 'Z')
    return (c - 'A');
  if ('a' <= c && c <= 'z')
    return (c - 'a' + 26);
  if ('0' <= c && c <= '9')
    return (c - '0' + 52);
  if (c == '+')
    return 62;
  if (c == '/')
    return 63;
  return -1;
}
int main() {
  int w, h, p;
  while (cin >> w >> h >> p, w || h || p) {
    string st[1005];
    int i, j, k;
    for (i = 0; i < h; i++)
      cin >> st[i];
    string wa[105];
    for (i = 0; i < p; i++)
      cin >> wa[i];

    string co[1005];
    for (i = 0; i < h; i++) {
      for (j = 0; j < st[i].size(); j++)
        co[i] += i2b(s2i(st[i][j]));
      co[i] = co[i].substr(0, w);
      // cout << co[i] <<  endl;
    }

    ull ha[1005][1005] = {{}};
    ull xx;
    for (i = 0; i < h; i++) {
      xx = 1;
      for (k = p - 1; k >= 0; k--) {
        if (co[i][k] == '1')
          ha[i][0] += xx;
        if (k != 0)
          xx *= base;
      }
      for (j = 1; j + p <= w; j++) {
        ha[i][j] = (ha[i][j - 1] - (co[i][j - 1] - '0') * xx) * base +
                   (co[i][j + p - 1] - '0');
      }
    }

    string wal[105][8];
    for (i = 0; i < p; i++) {
      for (j = 0; j < wa[i].size(); j++) {
        wal[i][0] += i2b(s2i(wa[i][j]));
      }
      wal[i][0] = wal[i][0].substr(0, p);
    }

    for (k = 1; k < 4; k++) {
      for (i = 0; i < p; i++) {
        for (j = p - 1; j >= 0; j--) {
          wal[i][k] += wal[j][k - 1][i];
        }
      }
    }

    for (k = 0; k < 4; k++) {
      for (i = 0; i < p; i++) {
        for (j = p - 1; j >= 0; j--) {
          wal[i][k + 4] += wal[i][k][j];
        }
      }
    }
    bool same[8] = {};
    for (i = 0; i < 8; i++) {
      for (j = i + 1; j < 8; j++) {
        if (same[i])
          break;
        same[i] = true;
        for (k = 0; k < p; k++) {
          if (wal[k][i] != wal[k][j])
            same[i] = false;
        }
      }
    }

    ull has[105][8] = {{}};
    int x, y;
    int ans = 0;
    for (k = 0; k < 8; k++) {
      if (same[k])
        continue;
      // cout << endl;
      // for(i=0;i<p;i++) cout << wal[i][k] << endl;
      for (i = 0; i < p; i++) {
        xx = 1;
        for (j = p - 1; j >= 0; j--) {
          if (wal[i][k][j] == '1')
            has[i][k] += xx;
          xx *= base;
        }
      }

      for (i = 0; i + p <= h; i++) {
        for (j = 0; j + p <= w; j++) {
          bool f = true;
          ;
          for (y = 0; y < p; y++) {
            // cout << ha[i+y][j] << " " << has[i+y][k] << endl;
            if (ha[i + y][j] != has[i + y][k]) {
              f = false;
              break;
            }
          }
          /*
          for(x=0;x<p;x++){
            for(y=0;y<p;y++){
              if(co[i+y][j+x]!=wal[y][k][x]){
                f=false;
                break;
              }
            }
            if(!f) break;
          }
          */
          if (f)
            ans++;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}