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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> p_ll;

template <class T> void debug(T itr1, T itr2) {
  auto now = itr1;
  while (now < itr2) {
    cout << *now << " ";
    now++;
  }
  cout << endl;
}
#define repr(i, from, to) for (ll i = (ll)from; i < (ll)to; i++)
#define all(vec) vec.begin(), vec.end()
#define rep(i, N) repr(i, 0, N)
#define per(i, N) for (int i = (int)N - 1; i >= 0; i--)

const ll MOD = pow(10, 9) + 7;
const ll LLINF = pow(2, 61) - 1;
const int INF = pow(2, 30) - 1;

vector<ll> fac;
void c_fac(int x = pow(10, 6) + 10) {
  fac.resize(x, true);
  rep(i, x) fac[i] = i ? (fac[i - 1] * i) % MOD : 1;
}
ll inv(ll a, ll m = MOD) {
  ll b = m, x = 1, y = 0;
  while (b != 0) {
    int d = a / b;
    a -= b * d;
    swap(a, b);
    x -= y * d;
    swap(x, y);
  }
  return (x + m) % m;
}
ll nck(ll n, ll k) { return fac[n] * inv(fac[k] * fac[n - k] % MOD) % MOD; }
ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  return b == 0 ? a : gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

struct Trie {
  Trie *parent;
  bool last = false;
  char chara;
  Trie *next[26];
  Trie(char c = '$', Trie *p = (Trie *)0) {
    fill_n(next, 26, (Trie *)0);
    chara = c;
    parent = p;
  }
  Trie *find(string t) {
    Trie *r = this;
    rep(i, t.size()) {
      ll pos = t[i] - 'a';
      if (!r->next[pos])
        r->next[pos] = new Trie(t[i], r);
      r = r->next[pos];
    }
    r->last = true;
    return r;
  }
};

int main() {
  ll N;
  cin >> N;
  string S[N];
  rep(i, N) {
    cin >> S[i];
    reverse(all(S[i]));
  }

  Trie tr, st[N];
  rep(i, N) st[i] = *tr.find(S[i]);

  ll result = 0;
  rep(i, N) {
    set<char> used;
    while (st[i].parent) {
      // cout << "---" << st[i].chara << " " << result << endl;
      used.insert(st[i].chara);
      st[i] = *st[i].parent;
      for (auto x : used) {
        ll pos = x - 'a';
        if (st[i].next[pos])
          result += (st[i].next[pos])->last;
      }
    }
  }

  result -= N;
  cout << result << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> p_ll;

template <class T> void debug(T itr1, T itr2) {
  auto now = itr1;
  while (now < itr2) {
    cout << *now << " ";
    now++;
  }
  cout << endl;
}
#define repr(i, from, to) for (ll i = (ll)from; i < (ll)to; i++)
#define all(vec) vec.begin(), vec.end()
#define rep(i, N) repr(i, 0, N)
#define per(i, N) for (int i = (int)N - 1; i >= 0; i--)

const ll MOD = pow(10, 9) + 7;
const ll LLINF = pow(2, 61) - 1;
const int INF = pow(2, 30) - 1;

vector<ll> fac;
void c_fac(int x = pow(10, 6) + 10) {
  fac.resize(x, true);
  rep(i, x) fac[i] = i ? (fac[i - 1] * i) % MOD : 1;
}
ll inv(ll a, ll m = MOD) {
  ll b = m, x = 1, y = 0;
  while (b != 0) {
    int d = a / b;
    a -= b * d;
    swap(a, b);
    x -= y * d;
    swap(x, y);
  }
  return (x + m) % m;
}
ll nck(ll n, ll k) { return fac[n] * inv(fac[k] * fac[n - k] % MOD) % MOD; }
ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  return b == 0 ? a : gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int main() {
  ll N;
  cin >> N;
  string S[N];
  rep(i, N) cin >> S[i];
  map<string, vector<char>> suf;
  rep(i, N) suf[S[i].substr(1)].push_back(S[i][0]);
  // for (auto x: suf) { cout << x.first << "->"; debug(all(x.second)); }

  ll result = 0;
  rep(i, N) {
    set<char> used;
    used.insert(S[i][0]);
    ll l = S[i].size();
    string ts = S[i].substr(1);
    repr(j, 1, S[i].size()) {
      used.insert(S[i][j]);
      // cout << S[i].substr(j+1) << "---" << endl;
      ts = ts.substr(1);
      if (suf.count(ts)) {
        for (auto x : suf[ts])
          result += used.count(x);
      }
    }
  }
  cout << result << endl;
  return 0;
}