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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

char tx[1006][1006];
char pt[106][106];
char qt[106][106];

int decode(char c) {
  if (isupper(c))
    return c - 'A';
  if (islower(c))
    return c - 'a' + 26;
  if (isdigit(c))
    return c - '0' + 52;
  return c == '+' ? 62 : 63;
}

void rotate(int p) {
  memcpy(qt, pt, sizeof(pt));
  for (int i = 0; i < p; i++)
    for (int j = 0; j < p; j++)
      pt[i][j] = qt[p - j - 1][i];
}

int main() {
  int w, h, p;
  while (cin >> w >> h >> p, w) {
    for (int i = 0; i < h; i++) {
      string str;
      cin >> str;
      for (int j = 0; j < (w + 5) / 6; j++) {
        int tmp = decode(str[j]);
        for (int k = 0; k < 6; k++)
          tx[i][6 * j + k] = ((tmp >> (5 - k)) & 1) + '0';
      }
    }
    for (int i = 0; i < p; i++) {
      string str;
      cin >> str;
      for (int j = 0; j < (p + 5) / 6; j++) {
        int tmp = decode(str[j]);
        for (int k = 0; k < 6; k++)
          pt[i][6 * j + k] = ((tmp >> (5 - k)) & 1) + '0';
      }
    }
    if (p > w || p > h) {
      cout << 0 << endl;
      continue;
    }
    vector<vector<unsigned int>> pat(8, vector<unsigned int>(p, 0));
    for (int i = 0; i < 4; i++) {
      for (int j = 0; j < 2; j++) {
        for (int k = 0; k < p; k++) {
          for (int l = 0; l < p; l++) {
            pat[2 * i + j][k] =
                pat[2 * i + j][k] * 101 + pt[k][j == 0 ? l : p - 1 - l];
          }
        }
      }
      rotate(p);
    }
    sort(pat.begin(), pat.end());
    pat.erase(unique(pat.begin(), pat.end()), pat.end());
    unsigned int mul = 1;
    vector<int> hash(h, 0);
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < p; j++) {
        hash[i] = (hash[i] * 101 + tx[i][j]);
        if (i == 0)
          mul *= 101;
      }
    }
    int res = 0;
    for (int i = p; i <= w; i++) {
      for (int j = 0; j < pat.size(); j++) {
        for (int k = 0; k + p - 1 < h; k++) {
          bool flag = true;
          for (int l = 0; l < p; l++) {
            if (hash[k + l] != pat[j][l]) {
              flag = false;
              break;
            }
          }
          if (flag)
            res++;
        }
      }
      if (i != w)
        for (int j = 0; j < h; j++) {
          hash[j] = hash[j] * 101 - tx[j][i - p] * mul + tx[j][i];
        }
    }
    cout << res << endl;
  }
}

Error: -6
terminate called after throwing an instance of 'std::length_error'
  what():  cannot create std::vector larger than max_size()


Fixed submission:

#include <algorithm>
#include <cstring>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

char tx[1006][1006];
char pt[106][106];
char qt[106][106];

int decode(char c) {
  if (isupper(c))
    return c - 'A';
  if (islower(c))
    return c - 'a' + 26;
  if (isdigit(c))
    return c - '0' + 52;
  return c == '+' ? 62 : 63;
}

void rotate(int p) {
  memcpy(qt, pt, sizeof(pt));
  for (int i = 0; i < p; i++)
    for (int j = 0; j < p; j++)
      pt[i][j] = qt[p - j - 1][i];
}

int main() {
  int w, h, p;
  ifstream fin("H.in");
  while (fin >> w >> h >> p, w) {
    for (int i = 0; i < h; i++) {
      string str;
      fin >> str;
      for (int j = 0; j < (w + 5) / 6; j++) {
        int tmp = decode(str[j]);
        for (int k = 0; k < 6; k++)
          tx[i][6 * j + k] = ((tmp >> (5 - k)) & 1) + '0';
      }
    }
    for (int i = 0; i < p; i++) {
      string str;
      fin >> str;
      for (int j = 0; j < (p + 5) / 6; j++) {
        int tmp = decode(str[j]);
        for (int k = 0; k < 6; k++)
          pt[i][6 * j + k] = ((tmp >> (5 - k)) & 1) + '0';
      }
    }
    if (p > w || p > h) {
      cout << 0 << endl;
      continue;
    }
    vector<vector<unsigned int>> pat(8, vector<unsigned int>(p, 0));
    for (int i = 0; i < 4; i++) {
      for (int j = 0; j < 2; j++) {
        for (int k = 0; k < p; k++) {
          for (int l = 0; l < p; l++) {
            pat[2 * i + j][k] =
                pat[2 * i + j][k] * 101 + pt[k][j == 0 ? l : p - 1 - l];
          }
        }
      }
      rotate(p);
    }
    sort(pat.begin(), pat.end());
    pat.erase(unique(pat.begin(), pat.end()), pat.end());
    unsigned int mul = 1;
    vector<int> hash(h, 0);
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < p; j++) {
        hash[i] = (hash[i] * 101 + tx[i][j]);
        if (i == 0)
          mul *= 101;
      }
    }
    int res = 0;
    for (int i = p; i <= w; i++) {
      for (int j = 0; j < pat.size(); j++) {
        for (int k = 0; k + p - 1 < h; k++) {
          bool flag = true;
          for (int l = 0; l < p; l++) {
            if (hash[k + l] != pat[j][l]) {
              flag = false;
              break;
            }
          }
          if (flag)
            res++;
        }
      }
      if (i != w)
        for (int j = 0; j < h; j++) {
          hash[j] = hash[j] * 101 - tx[j][i - p] * mul + tx[j][i];
        }
    }
    cout << res << endl;
  }
}