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

#pragma GCC optimize("Ofast")
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <complex>   // complex
#include <cstdint>   // int64_t, int*_t
#include <cstdio>    // printf
#include <deque>     // deque
#include <functional>
#include <iomanip>  // setprecision
#include <iostream> // cout, endl, cin
#include <map>      // map
#include <math.h>
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
#define enld '\\n'
using namespace std;
using ll = long long;
using P = pair<int, int>;
constexpr ll INF = 1e18;
constexpr int inf = 1e9;
constexpr ll mod = 1000000007;
constexpr ll mod2 = 998244353;
const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
// ---------------------------------------------------------------------------

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  unordered_map<string, int> mp;
  vector<string> S(N);
  map<int, int> num;
  int mn = inf;
  for (int i = 0; i < N; i++) {
    cin >> S[i];
    reverse(S[i].begin(), S[i].end());
    mp[S[i]]++;
    num[S[i].size()]++;
    chmin(mn, int(S[i].size()));
  }
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    vector<bool> alpha(26, false);
    bool fst = true;
    while (S[i].size()) {
      alpha[S[i].back() - 'a'] = true;
      S[i].pop_back();
      if (fst) {
        fst = false;
        continue;
      }
      if (S[i].size() + 1 < mn)
        break;
      for (int k = 0; k < 26; k++) {
        if (alpha[k]) {
          S[i] += k + 'a';
          // cout << now << "\\n";
          if (num.find(S[i].size()) == num.end()) {
            S[i].pop_back();
            continue;
          }
          if (mp.find(S[i]) != mp.end()) {
            ans += mp[S[i]];
          }
          S[i].pop_back();
        }
      }
    }
  }
  cout << ans << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <complex>   // complex
#include <cstdint>   // int64_t, int*_t
#include <cstdio>    // printf
#include <deque>     // deque
#include <functional>
#include <iomanip>  // setprecision
#include <iostream> // cout, endl, cin
#include <map>      // map
#include <math.h>
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
#define enld '\\n'
using namespace std;
using ll = long long;
using P = pair<int, int>;
constexpr ll INF = 1e18;
constexpr int inf = 1e9;
constexpr ll mod = 1000000007;
constexpr ll mod2 = 998244353;
const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
// ---------------------------------------------------------------------------

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  map<string, int> mp;
  vector<string> S(N);
  map<int, int> num;
  int mn = inf;
  for (int i = 0; i < N; i++) {
    cin >> S[i];
    reverse(S[i].begin(), S[i].end());
    mp[S[i]]++;
    num[S[i].size()]++;
    chmin(mn, int(S[i].size()));
  }
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    map<int, int> alpha;
    bool fst = true;
    while (S[i].size()) {
      alpha[S[i].back() - 'a']++;
      S[i].pop_back();
      if (fst) {
        fst = false;
        continue;
      }
      if (S[i].size() + 1 < mn)
        break;
      for (int k = 0; k < 26; k++) {
        if (alpha[k]) {
          S[i] += k + 'a';
          // cout << now << "\\n";
          if (num.find(S[i].size()) == num.end()) {
            S[i].pop_back();
            continue;
          }
          if (mp.find(S[i]) != mp.end()) {
            ans += mp[S[i]];
          }
          S[i].pop_back();
        }
      }
    }
  }
  cout << ans << "\\n";
  return 0;
}