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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

int main(int argc, char **argv) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(12);

  int n;
  cin >> n;
  vector<string> S;
  for (int i = 0; i < n; ++i) {
    string s;
    cin >> s;
    S.push_back(s);
  }

  auto cmp = [&](const string &l, const string &r) {
    if (l.length() == r.length()) {
      return l < r;
    }
    return l.length() < r.length();
  };
  sort(S.begin(), S.end(), cmp);

  const long long P = 1e9 + 7;
  const long long Q = 941083981;
  set<pair<int, int>> M;
  vector<int> cnts(26, 0);
  long long res = 0;

  set<string> M2;
  for (auto &s : S) {
    for (auto c : s) {
      ++cnts[c - 'a'];
    }
    for (char ch = 'a'; ch <= 'z'; ++ch) {
      if (cnts[ch - 'a'] > 0) {
        string t;
        t += ch;
        if (M2.count(t) > 0) {
          ++res;
        }
      }
    }
    int n = s.length();
    // string t;
    long long H1 = 0, H2 = 0;
    for (int i = n - 1; i >= 0; --i) {
      // t += s[i];
      --cnts[s[i] - 'a'];
      H1 = ((H1 * 347) + s[i]) % P;
      H2 = ((H2 * 259) + s[i]) % Q;

      for (char ch = 'a'; ch <= 'z'; ++ch) {
        if (cnts[ch - 'a'] > 0) {
          // t += ch;
          auto H11 = ((H1 * 347) + ch) % P;
          auto H22 = ((H2 * 259) + ch) % Q;
          if (M.count(make_pair(H11, H22)) > 0) {
            ++res;
          }
          // t.pop_back();
        }
      }
    }

    M.insert(make_pair(H1, H2));
    M2.insert(s);
  }

  cout << res << '\\n';

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

int main(int argc, char **argv) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(12);

  int n;
  cin >> n;
  vector<string> S;
  for (int i = 0; i < n; ++i) {
    string s;
    cin >> s;
    S.push_back(s);
  }

  auto cmp = [&](const string &l, const string &r) {
    if (l.length() == r.length()) {
      return l < r;
    }
    return l.length() < r.length();
  };
  sort(S.begin(), S.end(), cmp);

  unordered_set<string> M;
  M.reserve(n + 12);
  vector<int> cnts(26, 0);
  long long res = 0;
  for (auto &s : S) {
    for (auto c : s) {
      ++cnts[c - 'a'];
    }
    for (char ch = 'a'; ch <= 'z'; ++ch) {
      if (cnts[ch - 'a'] > 0) {
        string t;
        t += ch;
        if (M.count(t) > 0) {
          ++res;
        }
      }
    }
    int n = s.length();
    string t;
    for (int i = n - 1; i >= 0; --i) {
      t += s[i];
      --cnts[s[i] - 'a'];
      for (char ch = 'a'; ch <= 'z'; ++ch) {
        if (cnts[ch - 'a'] > 0) {
          t += ch;
          if (M.count(t)) {
            ++res;
          }
          t.pop_back();
        }
      }
    }

    M.insert(t);
  }

  cout << res << '\\n';

  return 0;
}