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

#include <bits/stdc++.h>

#if MYDEBUG
#include "lib/cp_debug.hpp"
#else
#define DBG(...) ;
#endif
#if __cplusplus <= 201402L
template <typename T> T gcd(T a, T b) {
  return ((a % b == 0) ? b : gcd(b, a % b));
}
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#endif
using LL = long long;
constexpr LL LINF = 334ll << 53;
constexpr int INF = 15 << 26;
constexpr LL MOD = 1E9 + 7;

namespace Problem {
using namespace std;

class Solver {
public:
  int n;
  vector<string> a;
  vector<vector<int>> cnt;
  map<string, vector<int>> memo;
  Solver(LL n) : n(n), a(n), cnt(n, vector<int>(26)){};

  void solve() {
    for (int i = 0; i < n; ++i) {
      cin >> a[i];
      reverse(a[i].begin(), a[i].end());
    }
    sort(a.begin(), a.end(), [&](string x, string y) -> bool {
      return x.length() < y.length() || (x.length() == y.length() && x < y);
    });
    DBG(a)

    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < a[i].length(); ++j) {
        cnt[i][a[i][j] - 'a']++;
      }
    }
    LL ans = 0;
    for (int i = 0; i < a.size(); ++i) {
      string tmp = "";
      for (int j = 0; j < (int)a[i].length() - 1; ++j) {
        DBG(tmp, memo.count(tmp))
        if (memo.count(tmp) == 0) {
          tmp += a[i][j];
          cnt[i][a[i][j] - 'a']--;
          continue;
        }
        auto M = memo[tmp];
        DBG(tmp, M)
        for (int k = 0; k < 26; ++k) {
          if (cnt[i][k] > 0) {
            ans += M[k];
          }
        }
        tmp += a[i][j];
        cnt[i][a[i][j] - 'a']--;
      }
      string b = a[i];
      b.pop_back();
      if (memo.count(b) == 0) {
        memo[b] = vector<int>(26);
      }
      memo[b][a[i].back() - 'a']++;
      DBG(ans)
      DBG(memo)
    }
    cout << ans << endl;
  }
}; // namespace Problem

} // namespace Problem

int main() {
  std::cin.tie(0);
  std::ios_base::sync_with_stdio(false);
  // std::cout << std::fixed << std::setprecision(12);
  long long n = 0;
  std::cin >> n;

  Problem::Solver sol(n);
  sol.solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#if MYDEBUG
#include "lib/cp_debug.hpp"
#else
#define DBG(...) ;
#endif
#if __cplusplus <= 201402L
template <typename T> T gcd(T a, T b) {
  return ((a % b == 0) ? b : gcd(b, a % b));
}
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#endif
using LL = long long;
constexpr LL LINF = 334ll << 53;
constexpr int INF = 15 << 26;
constexpr LL MOD = 1E9 + 7;

namespace Problem {
using namespace std;

class Solver {
public:
  int n;
  vector<string> a;
  vector<vector<int>> cnt;
  map<string, map<char, int>> memo;
  Solver(LL n) : n(n), a(n), cnt(n, vector<int>(26)){};

  void solve() {
    for (int i = 0; i < n; ++i) {
      cin >> a[i];
      reverse(a[i].begin(), a[i].end());
    }
    sort(a.begin(), a.end(), [&](string x, string y) -> bool {
      return x.length() < y.length() || (x.length() == y.length() && x < y);
    });
    DBG(a)

    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < a[i].length(); ++j) {
        cnt[i][a[i][j] - 'a']++;
      }
    }
    LL ans = 0;
    for (int i = 0; i < a.size(); ++i) {
      string tmp = "";
      for (int j = 0; j < (int)a[i].length() - 1; ++j) {
        auto M = memo[tmp];
        DBG(tmp, M)
        for (auto &&p : M) {
          DBG(p, cnt[i][p.first - 'a'])
          if (cnt[i][p.first - 'a'] > 0) {
            ans += p.second;
          }
        }
        tmp += a[i][j];
        cnt[i][a[i][j] - 'a']--;
      }
      string b = a[i];
      b.pop_back();
      memo[b][a[i].back()]++;
      DBG(ans)
      DBG(memo)
    }
    cout << ans << endl;
  }
}; // namespace Problem

} // namespace Problem

int main() {
  std::cin.tie(0);
  std::ios_base::sync_with_stdio(false);
  // std::cout << std::fixed << std::setprecision(12);
  long long n = 0;
  std::cin >> n;

  Problem::Solver sol(n);
  sol.solve();
  return 0;
}