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

#include <bits/stdc++.h>

#if DEBUG
template <typename T> void debug(const T &value) { std::cerr << value; }
template <typename T, typename... Ts>
void debug(const T &value, const Ts &...args) {
  std::cerr << value << ", ";
  debug(args...);
}
#define dbg(...)                                                               \\
  do {                                                                         \\
    cerr << #__VA_ARGS__ << ": ";                                              \\
    debug(__VA_ARGS__);                                                        \\
    cerr << " (L" << __LINE__ << ")\\n";                                        \\
  } while (0)
#else
#define dbg(...)
#endif

void read_from_cin() {}
template <typename T, typename... Ts>
void read_from_cin(T &value, Ts &...args) {
  std::cin >> value;
  read_from_cin(args...);
}
#define rd(type, ...)                                                          \\
  type __VA_ARGS__;                                                            \\
  read_from_cin(__VA_ARGS__);

template <typename T> void write_to_cout(const T &value) {
  std::cout << value << '\\n';
}
template <typename T, typename... Ts>
void write_to_cout(const T &value, const Ts &...args) {
  std::cout << value << ' ';
  write_to_cout(args...);
}
#define wt(...) write_to_cout(__VA_ARGS__);

#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

using ll = long long;

template <typename T> using V = std::vector<T>;
template <typename T> using VV = V<V<T>>;

using namespace std;

struct Trie {
  array<int, 26> value = {};
  array<Trie *, 26> children = {};
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  rd(int, n);

  V<string> s(n);
  rep(i, n) {
    cin >> s[i];
    reverse(all(s[i]));
  }

  Trie trie;
  int nonempty = 0;
  function<bitset<26>(Trie &, const string &, int)> add_trie =
      [&](Trie &t, const string &si, int i) {
        bitset<26> have;
        if (i < si.size()) {
          if (t.children[si[i] - 'a'] == NULL) {
            t.children[si[i] - 'a'] = new Trie;
          }
          Trie &c = *t.children[si[i] - 'a'];
          have |= add_trie(c, si, i + 1);
          have.set((si[i] - 'a'));
        }
        if (si.size() - i >= 2) {
          rep(i, 26) if (have.test(i))++ t.value[i];
        }
        return have;
      };
  for (const auto &si : s) {
    dbg(si.substr(0, 10), nonempty);
    add_trie(trie, si, 0);
  }
  ll ans = 0;
  for (const auto &si : s) {
    const Trie *t = &trie;
    rep(i, si.size() - 1) { t = t->children[si[i] - 'a']; }
    ans += t->value[si.back() - 'a'];
  }
  wt(ans);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#if DEBUG
template <typename T> void debug(const T &value) { std::cerr << value; }
template <typename T, typename... Ts>
void debug(const T &value, const Ts &...args) {
  std::cerr << value << ", ";
  debug(args...);
}
#define dbg(...)                                                               \\
  do {                                                                         \\
    cerr << #__VA_ARGS__ << ": ";                                              \\
    debug(__VA_ARGS__);                                                        \\
    cerr << " (L" << __LINE__ << ")\\n";                                        \\
  } while (0)
#else
#define dbg(...)
#endif

void read_from_cin() {}
template <typename T, typename... Ts>
void read_from_cin(T &value, Ts &...args) {
  std::cin >> value;
  read_from_cin(args...);
}
#define rd(type, ...)                                                          \\
  type __VA_ARGS__;                                                            \\
  read_from_cin(__VA_ARGS__);

template <typename T> void write_to_cout(const T &value) {
  std::cout << value << '\\n';
}
template <typename T, typename... Ts>
void write_to_cout(const T &value, const Ts &...args) {
  std::cout << value << ' ';
  write_to_cout(args...);
}
#define wt(...) write_to_cout(__VA_ARGS__);

#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

using ll = long long;

template <typename T> using V = std::vector<T>;
template <typename T> using VV = V<V<T>>;

using namespace std;

struct Trie {
  int value;
  vector<Trie> children;
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  rd(int, n);

  V<string> s(n);
  rep(i, n) {
    cin >> s[i];
    reverse(all(s[i]));
  }

  // b
  // a
  // cba
  // c
  // d
  // ba

  ll ans = 0;
  for (char alpha = 'a'; alpha <= 'z'; ++alpha) {
    Trie trie;
    trie.value = 0;
    function<bool(Trie &, const string &, int)> add_trie =
        [&](Trie &t, const string &si, int i) {
          bool have = false;
          if (i < si.size()) {
            if (t.children.empty()) {
              t.children.resize(26);
              rep(i, 26) t.children[i].value = 0;
            }
            Trie &c = t.children[si[i] - 'a'];
            if (add_trie(c, si, i + 1)) {
              have = true;
            }
            if (si[i] == alpha)
              have = true;
          }
          if (have && si.size() - i >= 2) {
            if (alpha == 'b')
              dbg(si);
            ++t.value;
          }
          return have;
        };
    for (const auto &si : s)
      add_trie(trie, si, 0);
    for (const auto &si : s) {
      if (si.back() == alpha) {
        const Trie *t = &trie;
        rep(i, si.size() - 1) {
          assert(!t->children.empty());
          t = &t->children[si[i] - 'a'];
        }
        dbg(si, t->value);
        ans += t->value;
      }
    }
  }
  wt(ans);
}