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

#include "bits/stdc++.h"
using namespace std;
// #define int long long
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define RREP(i, n) for (int i = (int)n - 1; i >= 0; --i)
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define RFOR(i, s, n) for (int i = (int)n - 1; i >= s; --i)
#define ALL(a) a.begin(), a.end()
#define IN(a, x, b) (a <= x && x < b)
template <class T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}
template <class T> inline void out(T t) { cout << t << "\\n"; }
template <class T, class... Ts> inline void out(T t, Ts... ts) {
  cout << t << " ";
  out(ts...);
}
template <class T> inline bool CHMIN(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool CHMAX(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
// constexpr int INF = 1e18;

signed main() {
  int N;
  cin >> N;
  vector<string> v(N);
  REP(i, N) { cin >> v[i]; }
  vector<vector<vector<int>>> vs;
  REP(i, N) {
    vector<vector<int>> tmp(v[i].size() + 1, vector<int>(26));
    REP(j, v[i].size()) {
      REP(k, 26) { tmp[j + 1][k] += tmp[j][k] + (v[i][j] - 'a' == k); }
    }
    vs.emplace_back(tmp);
  }
  vector<int> id(N);
  iota(ALL(id), 0);
  auto dfs = [&](auto &&f, vector<int> &a, int tail) -> long long {
    vector<int> cnt(26);
    REP(i, a.size()) {
      if (v[a[i]].size() - tail == 1) {
        ++cnt[v[a[i]][0] - 'a'];
      }
    }
    long long ret = 0;
    REP(i, a.size()) {
      REP(j, 26) {
        int t = vs[a[i]][vs[a[i]].size() - tail - 1][j] - vs[a[i]][0][j];
        if (t)
          ret += cnt[j];
      }
    }
    ret -= accumulate(ALL(cnt), 0ll);
    vector<vector<int>> next(26);
    REP(i, a.size()) {
      if (v[a[i]].size() - tail == 1)
        continue;
      next[v[a[i]][v[a[i]].size() - tail - 1] - 'a'].emplace_back(a[i]);
    }
    REP(i, 26) {
      if (next[i].size() == 0)
        continue;
      ret += f(f, next[i], tail + 1);
    }
    return ret;
  };
  out(dfs(dfs, id, 0));
}

Error: MLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#define int long long
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define RREP(i, n) for (int i = (int)n - 1; i >= 0; --i)
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define RFOR(i, s, n) for (int i = (int)n - 1; i >= s; --i)
#define ALL(a) a.begin(), a.end()
#define IN(a, x, b) (a <= x && x < b)
template <class T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  return is;
}
template <class T> inline void out(T t) { cout << t << "\\n"; }
template <class T, class... Ts> inline void out(T t, Ts... ts) {
  cout << t << " ";
  out(ts...);
}
template <class T> inline bool CHMIN(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool CHMAX(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
constexpr int INF = 1e18;

signed main() {
  int N;
  cin >> N;
  vector<string> v(N);
  REP(i, N) { cin >> v[i]; }
  vector<vector<vector<int>>> vs;
  REP(i, N) {
    vector<vector<int>> tmp(v[i].size() + 1, vector<int>(26));
    REP(j, v[i].size()) {
      REP(k, 26) { tmp[j + 1][k] += tmp[j][k] + (v[i][j] - 'a' == k); }
    }
    vs.emplace_back(tmp);
  }
  vector<int> id(N);
  iota(ALL(id), 0);
  auto dfs = [&](auto &&f, vector<int> &a, int tail) -> int {
    vector<int> cnt(26);
    REP(i, a.size()) {
      if (v[a[i]].size() - tail == 1) {
        ++cnt[v[a[i]][0] - 'a'];
      }
    }
    int ret = 0;
    REP(i, a.size()) {
      REP(j, 26) {
        int t = vs[a[i]][vs[a[i]].size() - tail - 1][j] - vs[a[i]][0][j];
        if (t)
          ret += cnt[j];
      }
    }
    ret -= accumulate(ALL(cnt), 0ll);
    vector<vector<int>> next(26);
    REP(i, a.size()) {
      if (v[a[i]].size() - tail == 1)
        continue;
      next[v[a[i]][v[a[i]].size() - tail - 1] - 'a'].emplace_back(a[i]);
    }
    REP(i, 26) {
      if (next[i].size() == 0)
        continue;
      ret += f(f, next[i], tail + 1);
    }
    return ret;
  };
  out(dfs(dfs, id, 0));
}