Problem p02589 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
//{
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORD(i, a, b) for (int i = (a); i >= (b); --i)
#define VAR(v, i) __typeof(i) v = (i)
#define all(v) (v).begin(), (v).end()
#define FORE(i, c) for (VAR(i, (c).begin()); i != (c).end(); ++i)

#define PII pair<int, int>
#define st first
#define nd second
#define pb push_back
#define lint long long int
#define VI vector<int>

#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \\
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \\
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
}
;
//{
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

#define make(x)                                                                \\
  int(x);                                                                      \\
  scanf("%d", &(x));
#define make2(x, y)                                                            \\
  int(x), (y);                                                                 \\
  scanf("%d%d", &(x), &(y));
#define make3(x, y, z)                                                         \\
  int(x), (y), (z);                                                            \\
  scanf("%d%d%d", &(x), &(y), &(z));
#define make4(x, y, z, t)                                                      \\
  int(x), (y), (z), (t);                                                       \\
  scanf("%d%d%d%d", &(x), &(y), &(z), &(t));
#define makev(v, n)                                                            \\
  VI(v);                                                                       \\
  FOR(i, 0, (n)) {                                                             \\
    make(a);                                                                   \\
    (v).pb(a);                                                                 \\
  }
#define IOS ios_base::sync_with_stdio(0)
#define HEAP priority_queue

#define read(x) scanf("%d", &(x));
#define read2(x, y) scanf("%d%d", &(x), &(y));
#define read3(x, y, z) scanf("%d%d%d", &(x), &(y), &(z));
#define read4(x, y, z, t) scanf("%d%d%d%d", &(x), &(y), &(z), &(t));
#define readv(v, n)                                                            \\
  FOR(i, 0, (n)) {                                                             \\
    make(a);                                                                   \\
    (v).pb(a);                                                                 \\
  }
#define jeb() fflush(stdout);
//}

vector<string> v[1234567];
set<int> memo[1234567];
set<int> memo2[1234567];
int mod = 1e9 + 9;
int mod2 = 1e9 + 7;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  FOR(i, 0, n) {
    string s;
    cin >> s;
    v[s.length()].pb(s);
  }
  vector<string> in;
  FOR(i, 1, 1e6) {
    if (v[i].size() != 0) {
      FOR(j, 0, v[i].size()) in.pb(v[i][j]);
    }
  }
  lint ans = 0;
  FOR(jj, 0, in.size()) {

    string s = in[jj];
    // debug() << s;
    if (s.size() == 1) {
      memo[0].insert((s[0] - 'a') + 100);
      memo2[0].insert((s[0] - 'a') + 123);
      continue;
    }
    VI ile(26, 0);
    FOR(i, 0, s.length()) ile[s[i] - 'a']++;
    int hash = 0;
    int hash2 = 0;
    FOR(i, 0, s.length()) {
      FOR(j, 0, 26)
      if (ile[j] > 0 &&
          memo[i].find((2137LL * hash + j + 100) % mod) != memo[i].end() &&
          memo2[i].find((7312LL * hash2 + j + 123) % mod) != memo2[i].end())
          ans++;
      int ii = s.length() - i - 1;
      ile[s[ii] - 'a']--;
      if (ii == 0)
        memo[s.length() - 1].insert((hash * 2137LL + (s[0] - 'a') + 100) % mod);
      if (ii == 0)
        memo2[s.length() - 1].insert((hash2 * 7312LL + (s[0] - 'a') + 123) %
                                     mod);
      hash = (hash * 2137LL + (s[ii] - 'a') + 100) % mod;
      hash2 = (hash2 * 7312LL + (s[ii] - 'a') + 123) % mod;
    }
    // debug() << ans;
  }
  printf("%lld\\n", ans);
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
//{
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORD(i, a, b) for (int i = (a); i >= (b); --i)
#define VAR(v, i) __typeof(i) v = (i)
#define all(v) (v).begin(), (v).end()
#define FORE(i, c) for (VAR(i, (c).begin()); i != (c).end(); ++i)

#define PII pair<int, int>
#define st first
#define nd second
#define pb push_back
#define lint long long int
#define VI vector<int>

#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \\
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \\
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
}
;
//{
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

#define make(x)                                                                \\
  int(x);                                                                      \\
  scanf("%d", &(x));
#define make2(x, y)                                                            \\
  int(x), (y);                                                                 \\
  scanf("%d%d", &(x), &(y));
#define make3(x, y, z)                                                         \\
  int(x), (y), (z);                                                            \\
  scanf("%d%d%d", &(x), &(y), &(z));
#define make4(x, y, z, t)                                                      \\
  int(x), (y), (z), (t);                                                       \\
  scanf("%d%d%d%d", &(x), &(y), &(z), &(t));
#define makev(v, n)                                                            \\
  VI(v);                                                                       \\
  FOR(i, 0, (n)) {                                                             \\
    make(a);                                                                   \\
    (v).pb(a);                                                                 \\
  }
#define IOS ios_base::sync_with_stdio(0)
#define HEAP priority_queue

#define read(x) scanf("%d", &(x));
#define read2(x, y) scanf("%d%d", &(x), &(y));
#define read3(x, y, z) scanf("%d%d%d", &(x), &(y), &(z));
#define read4(x, y, z, t) scanf("%d%d%d%d", &(x), &(y), &(z), &(t));
#define readv(v, n)                                                            \\
  FOR(i, 0, (n)) {                                                             \\
    make(a);                                                                   \\
    (v).pb(a);                                                                 \\
  }
#define jeb() fflush(stdout);
//}

vector<string> v[1234567];
set<int> memo[1234567][30];
int mod = 1e9 + 9;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  FOR(i, 0, n) {
    string s;
    cin >> s;
    v[s.length()].pb(s);
  }
  vector<string> in;
  FOR(i, 1, 1e6) {
    if (v[i].size() != 0) {
      FOR(j, 0, v[i].size()) in.pb(v[i][j]);
    }
  }
  lint ans = 0;
  FOR(jj, 0, in.size()) {

    string s = in[jj];
    debug() << s;
    if (s.size() == 1) {
      memo[0][s[0] - 'a'].insert(0);
      continue;
    }
    VI ile(26, 0);
    FOR(i, 0, s.length()) ile[s[i] - 'a']++;
    int hash = 0;
    FOR(i, 0, s.length()) {

      FOR(j, 0, 26)
      if (ile[j] > 0 && memo[i][j].find(hash) != memo[i][j].end()) ans++;
      int ii = s.length() - i - 1;
      ile[s[ii] - 'a']--;
      if (ii == 0)
        memo[s.length() - 1][s[0] - 'a'].insert(hash);
      hash = (hash * 2137LL + (s[ii] - 'a') + 100) % mod;
    }
    debug() << ans;
  }
  printf("%lld\\n", ans);
}