Problem p00949 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define mp make_pair
using namespace std;

const int MAXN = 1e4 + 10;
typedef long long ll;
typedef pair<ll, ll> PAIR;
ll MOD1 = 1e9 + 7, MOD2 = 1e9 + 9;

ll cal1[256], cal2[256];
ll init() {
  for (int i = 'a'; i <= 'z'; i++) {
    ll ans = 1;
    ll n = i - 'a' + 1;
    ll temp = i - 'a' + 1;
    while (n) {
      if (n & 1)
        ans *= temp, ans %= MOD1;
      temp *= temp;
      temp %= MOD1;
      n >>= 1;
    }
    cal1[i] = ans;
    ans = 1, n = i - 'a' + 1, temp = i - 'a' + 1;
    while (n) {
      if (n & 1)
        ans *= temp, ans %= MOD2;
      temp *= temp;
      temp %= MOD2;
      n >>= 1;
    }
    cal2[i] = ans;
  }
}

int main() {
  init();
  char str1[MAXN], str2[MAXN];
  scanf("%s%s", str1, str2);
  PAIR Hash1[MAXN], Hash2[MAXN];
  int Len1 = strlen(str1), Len2 = strlen(str2);
  Hash1[0].first = Hash1[0].second = Hash2[0].first = Hash2[0].second = 0;
  for (int i = 0; i < Len1; i++) {
    Hash1[i + 1].first = cal1[str1[i]];
    Hash1[i + 1].first += Hash1[i].first;
    Hash1[i + 1].first %= MOD1;
    Hash1[i + 1].second = cal2[str1[i]];
    Hash1[i + 1].second += Hash1[i].second;
    Hash1[i + 1].second %= MOD2;
  }
  for (int i = 0; i < Len2; i++) {
    Hash2[i + 1].first = cal1[str2[i]];
    Hash2[i + 1].first += Hash2[i].first;
    Hash2[i + 1].first %= MOD1;
    Hash2[i + 1].second = cal2[str2[i]];
    Hash2[i + 1].second += Hash2[i].second;
    Hash2[i + 1].second %= MOD2;
  }
  int M = min(Len1, Len2);
  int i, j, k;
  PAIR p1, p2;
  map<PAIR, bool> MAP;
  for (i = M; i > 0; i--) {
    MAP.clear();
    for (j = 0; j + i <= Len1; j++) {
      p1.first = ((Hash1[j + i].first - Hash1[j].first) % MOD1 + MOD1) % MOD1;
      p1.second =
          ((Hash1[j + i].second - Hash1[j].second) % MOD2 + MOD2) % MOD2;
      MAP[p1] = true;
    }
    for (k = 0; k + i <= Len2; k++) {

      p2.first = ((Hash2[k + i].first - Hash2[k].first) % MOD1 + MOD1) % MOD1;
      p2.second =
          ((Hash2[k + i].second - Hash2[k].second) % MOD2 + MOD2) % MOD2;
      if (MAP.count(p2)) {
        printf("%d\\n", i);
        return 0;
      }
    }
  }
  printf("0\\n");
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define mp make_pair
using namespace std;

const int MAXN = 1e4 + 10;
typedef long long ll;
typedef pair<ll, ll> PAIR;
ll MOD1 = 1e9 + 7, MOD2 = 1e9 + 9;

ll cal1[256], cal2[256];
ll init() {
  for (int i = 'a'; i <= 'z'; i++) {
    ll ans = 1;
    ll n = i - 'a' + 2;
    ll temp = i;
    while (n) {
      if (n & 1)
        ans *= temp, ans %= MOD1;
      temp *= temp;
      temp %= MOD1;
      n >>= 1;
    }
    cal1[i] = ans;
    ans = 1, n = i - 'a' + 2, temp = i;
    while (n) {
      if (n & 1)
        ans *= temp, ans %= MOD2;
      temp *= temp;
      temp %= MOD2;
      n >>= 1;
    }
    cal2[i] = ans;
  }
}

int main() {
  init();
  char str1[MAXN], str2[MAXN];
  scanf("%s%s", str1, str2);
  PAIR Hash1[MAXN], Hash2[MAXN];
  int Len1 = strlen(str1), Len2 = strlen(str2);
  Hash1[0].first = Hash1[0].second = Hash2[0].first = Hash2[0].second = 0;
  for (int i = 0; i < Len1; i++) {
    Hash1[i + 1].first = cal1[str1[i]];
    Hash1[i + 1].first += Hash1[i].first;
    Hash1[i + 1].first %= MOD1;
    Hash1[i + 1].second = cal2[str1[i]];
    Hash1[i + 1].second += Hash1[i].second;
    Hash1[i + 1].second %= MOD2;
  }
  for (int i = 0; i < Len2; i++) {
    Hash2[i + 1].first = cal1[str2[i]];
    Hash2[i + 1].first += Hash2[i].first;
    Hash2[i + 1].first %= MOD1;
    Hash2[i + 1].second = cal2[str2[i]];
    Hash2[i + 1].second += Hash2[i].second;
    Hash2[i + 1].second %= MOD2;
  }
  int M = min(Len1, Len2);
  int i, j, k;
  PAIR p1, p2;
  for (i = M; i > 0; i--) {
    for (j = 0; j + i <= Len1; j++) {
      p1.first = ((Hash1[j + i].first - Hash1[j].first) % MOD1 + MOD1) % MOD1;
      p1.second =
          ((Hash1[j + i].second - Hash1[j].second) % MOD2 + MOD2) % MOD2;
      for (k = 0; k + i <= Len2; k++) {

        p2.first = ((Hash2[k + i].first - Hash2[k].first) % MOD1 + MOD1) % MOD1;
        p2.second =
            ((Hash2[k + i].second - Hash2[k].second) % MOD2 + MOD2) % MOD2;
        if (p1 == p2) {
          printf("%d\\n", i);
          return 0;
        }
      }
    }
  }
  printf("0\\n");
  return 0;
}