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

#include "bits/stdc++.h"

using namespace std;

using ll = long long;
using ld = long double;
using P = pair<int, int>;
constexpr ld EPS = 1e-12;
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr int MOD = 1e9 + 7;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string s1, s2;
  cin >> s1 >> s2;
  int n1 = s1.length(), n2 = s2.length();
  vector<vector<int>> sum1(n1, vector<int>(26, 0));
  vector<vector<int>> sum2(n2, vector<int>(26, 0));
  for (int i = 0; i < n1; i++) {
    if (i != 0) {
      for (int j = 0; j < 26; j++) {
        sum1[i][j] = sum1[i - 1][j];
      }
    }
    sum1[i][s1[i] - 'a']++;
  }
  for (int i = 0; i < n2; i++) {
    if (i != 0) {
      for (int j = 0; j < 26; j++) {
        sum2[i][j] = sum2[i - 1][j];
      }
    }
    sum2[i][s2[i] - 'a']++;
  }
  int res = 0;
  for (int len = min(n1, n2); len > 0; len--) {
    vector<ll> hash(n1 - len + 1);
    for (int i = 0; i < n1 - len + 1; i++) {
      ll tmp = 0;
      for (int k = 0; k < 26; k++) {
        tmp *= MOD;
        if (i == 0) {
          tmp += sum1[i + len - 1][k];
        } else {
          tmp += sum1[i + len - 1][k] - sum1[i - 1][k];
        }
      }
      hash[i] = tmp;
    }
    sort(hash.begin(), hash.end());
    for (int i = 0; i < n2 - len + 1; i++) {
      ll tmp = 0;
      for (int k = 0; k < 26; k++) {
        tmp *= MOD;
        if (i == 0) {
          tmp += sum2[i + len - 1][k];
        } else {
          tmp += sum2[i + len - 1][k] - sum2[i - 1][k];
        }
      }
      auto itr = lower_bound(hash.begin(), hash.end(), tmp);
      if (itr == hash.end())
        continue;
      if (*itr != tmp)
        continue;
      res = len;
      break;
    }
    if (res != 0)
      break;
  }
  cout << res << endl;
}


Error: -11

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

using ll = long long;
using ld = long double;
using P = pair<int, int>;
constexpr ld EPS = 1e-12;
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr int MOD = 1e9 + 7;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string s1, s2;
  cin >> s1 >> s2;
  int n1 = s1.length(), n2 = s2.length();
  vector<vector<int>> sum1(n1, vector<int>(26, 0));
  vector<vector<int>> sum2(n2, vector<int>(26, 0));
  for (int i = 0; i < n1; i++) {
    if (i != 0) {
      for (int j = 0; j < 26; j++) {
        sum1[i][j] = sum1[i - 1][j];
      }
    }
    sum1[i][s1[i] - 'a']++;
  }
  for (int i = 0; i < n2; i++) {
    if (i != 0) {
      for (int j = 0; j < 26; j++) {
        sum2[i][j] = sum2[i - 1][j];
      }
    }
    sum2[i][s2[i] - 'a']++;
  }
  int res = 0;
  for (int len = min(n1, n2); len > 0; len--) {
    vector<int> hash(n1 - len + 1);
    for (int i = 0; i < n1 - len + 1; i++) {
      int tmp = 0;
      for (int k = 0; k < 26; k++) {
        tmp *= MOD;
        if (i == 0) {
          tmp += sum1[i + len - 1][k];
        } else {
          tmp += sum1[i + len - 1][k] - sum1[i - 1][k];
        }
      }
      hash[i] = tmp;
    }
    sort(hash.begin(), hash.end());
    for (int i = 0; i < n2; i++) {
      int tmp = 0;
      for (int k = 0; k < 26; k++) {
        tmp *= MOD;
        if (i == 0) {
          tmp += sum2[i + len - 1][k];
        } else {
          tmp += sum2[i + len - 1][k] - sum2[i - 1][k];
        }
      }
      auto itr = lower_bound(hash.begin(), hash.end(), tmp);
      if (itr == hash.end())
        continue;
      if (*itr != tmp)
        continue;
      res = len;
      break;
    }
    if (res != 0)
      break;
  }
  cout << res << endl;
}