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

#include <algorithm>
#include <complex>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#define REP(i, m, n) for (int i = int(m); i < int(n); i++)
#define RREP(i, m, n) for (int i = int(n) - 1; i >= int(m); --i)
#define EACH(i, c) for (auto &(i) : c)
#define all(c) begin(c), end(c)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(begin(c), end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
// #define int long long

#ifdef LOCAL
#define DEBUG(s) cout << (s) << endl
#define dump(x) cerr << #x << " = " << (x) << endl
#define BR cout << endl;
#else
#define DEBUG(s)                                                               \\
  do {                                                                         \\
  } while (0)
#define dump(x)                                                                \\
  do {                                                                         \\
  } while (0)
#define BR
#endif
using namespace std;

using UI = unsigned int;
using UL = unsigned long;
using LL = long long;
using ULL = unsigned long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VS = vector<string>;
using PII = pair<int, int>;
using VP = vector<PII>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

struct Node {
  bool isend;
  unordered_map<char, Node *> m;
  Node() : isend(false) {}
};
class Trie {
public:
  Node *root;
  /** Initialize your data structure here. */
  Trie() : root(new Node()) {}

  /** Inserts a word into the trie. */
  void insert(string word) {
    Node *node = root;
    REP(i, 0, word.size()) {
      char c = word[i];
      if (node->m.count(c) == 0)
        node->m[c] = new Node();

      node = node->m[c];
    }

    node->isend = true;
  }

  /** Returns if the word is in the trie. */
  bool search(string word) {
    Node *node = root;
    for (char c : word) {
      if (node->m.count(c) == 0)
        return false;
      node = node->m[c];
    }
    return node->isend;
  }

  /** Returns if there is any word in the trie that starts with the given
   * prefix. */
  bool startsWith(string prefix) {
    Node *node = root;
    for (char c : prefix) {
      if (node->m.count(c) == 0)
        return false;
      node = node->m[c];
    }
    return true;
  }
};

void solve() {
  int n;
  cin >> n;
  VS ss(n);
  REP(i, 0, n) cin >> ss[i];
  vector<vector<int>> ds(n);
  REP(i, 0, n) {
    int p = 0;
    REP(j, 0, ss[i].size()) {
      p |= (1 << (ss[i][j] - 'a'));
      ds[i].emplace_back(p);
    }
    reverse(all(ds[i]));
  }

  REP(i, 0, n) reverse(all(ss[i]));
  Trie t;
  REP(i, 0, n) t.insert(ss[i]);

  LL cnt = 0;
  REP(i, 0, n) {
    // dump(i);
    Node *node = t.root;
    REP(j, 0, ss[i].size() - 1) {
      char c = ss[i][j];
      // if (node->isend) ++cnt;
      REP(k, 0, 26) {
        char d = char('a' + k);
        if (d == c)
          continue;
        // if (ds[i][j].count(d) == 0) continue;
        if (!(ds[i][j] & (1 << k)))
          continue;
        if (node->m.count(d) == 0)
          continue;
        if (!node->m[d]->isend)
          continue;
        ++cnt;
      }
      if (node->m.count(c) == 0)
        break;
      node = node->m[c];
      if (node->isend)
        ++cnt;
    }
    // dump(cnt);
  }
  cout << cnt << endl;
}

signed main() {
  solve();

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <complex>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

#define REP(i, m, n) for (int i = int(m); i < int(n); i++)
#define RREP(i, m, n) for (int i = int(n) - 1; i >= int(m); --i)
#define EACH(i, c) for (auto &(i) : c)
#define all(c) begin(c), end(c)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(begin(c), end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
// #define int long long

#ifdef LOCAL
#define DEBUG(s) cout << (s) << endl
#define dump(x) cerr << #x << " = " << (x) << endl
#define BR cout << endl;
#else
#define DEBUG(s)                                                               \\
  do {                                                                         \\
  } while (0)
#define dump(x)                                                                \\
  do {                                                                         \\
  } while (0)
#define BR
#endif
using namespace std;

using UI = unsigned int;
using UL = unsigned long;
using LL = long long;
using ULL = unsigned long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VS = vector<string>;
using PII = pair<int, int>;
using VP = vector<PII>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

struct Node {
  bool isend;
  unordered_map<char, Node *> m;
  Node() : isend(false) {}
};
class Trie {
public:
  Node *root;
  /** Initialize your data structure here. */
  Trie() : root(new Node()) {}

  /** Inserts a word into the trie. */
  void insert(string word) {
    Node *node = root;
    REP(i, 0, word.size()) {
      char c = word[i];
      if (node->m.count(c) == 0)
        node->m[c] = new Node();

      node = node->m[c];
    }

    node->isend = true;
  }

  /** Returns if the word is in the trie. */
  bool search(string word) {
    Node *node = root;
    for (char c : word) {
      if (node->m.count(c) == 0)
        return false;
      node = node->m[c];
    }
    return node->isend;
  }

  /** Returns if there is any word in the trie that starts with the given
   * prefix. */
  bool startsWith(string prefix) {
    Node *node = root;
    for (char c : prefix) {
      if (node->m.count(c) == 0)
        return false;
      node = node->m[c];
    }
    return true;
  }
};

void solve() {
  int n;
  cin >> n;
  VS ss(n);
  REP(i, 0, n) cin >> ss[i];
  vector<vector<set<char>>> ds(n);
  REP(i, 0, n) {
    set<char> s;
    REP(j, 0, ss[i].size()) {
      s.insert(ss[i][j]);
      ds[i].emplace_back(s);
    }
    reverse(all(ds[i]));
  }

  REP(i, 0, n) reverse(all(ss[i]));
  Trie t;
  REP(i, 0, n) t.insert(ss[i]);

  LL cnt = 0;
  REP(i, 0, n) {
    // dump(i);
    Node *node = t.root;
    REP(j, 0, ss[i].size() - 1) {
      char c = ss[i][j];
      // if (node->isend) ++cnt;
      REP(k, 0, 26) {
        char d = char('a' + k);
        if (d == c)
          continue;
        if (ds[i][j].count(d) == 0)
          continue;
        if (node->m.count(d) == 0)
          continue;
        if (!node->m[d]->isend)
          continue;
        ++cnt;
      }
      if (node->m.count(c) == 0)
        break;
      node = node->m[c];
      if (node->isend)
        ++cnt;
    }
    dump(cnt);
  }
  cout << cnt << endl;
}

signed main() {
  solve();

  return 0;
}