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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using WGraph = vector<vector<pair<int, ll>>>;

template <class T> inline bool chmax(T &a, const T &b) {
  if (b > a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

constexpr int dx[4] = {-1, 0, 0, 1}, dy[4] = {0, -1, 1, 0};
constexpr int INF = 1e9;
constexpr int MOD = 1e9 + 7;
constexpr long long LINF = 1e18;
constexpr double EPS = 1e-10;
constexpr double PI = M_PI;

void solve() {
  int N;
  cin >> N;
  vector<string> S(N);
  for (int i = 0; i < N; ++i)
    cin >> S[i];

  for (int i = 0; i < N; ++i)
    reverse(S[i].begin(), S[i].end());
  sort(S.begin(), S.end(),
       [](auto a, auto b) { return (int)a.size() > (int)b.size(); });

  map<string, vector<int>> mp;
  ll res = 0;
  int cur = (int)S.front().size();
  for (int i = 0; i < N; ++i) {
    int len = (int)S[i].size();
    if (cur >= len) {
      int dif = cur - len + 1;
      map<string, vector<int>> tmp;
      for (auto iter = mp.begin(); iter != mp.end(); ++iter) {
        string sub = iter->first.substr(0, iter->first.size() - dif);
        for (int j = 0; j < dif; ++j)
          iter->second[*(iter->first.end() - dif + j) - 'a'] =
              iter->second.back();
        if ((int)tmp[sub].size() == 0) {
          tmp[sub] = iter->second;
        } else {
          vector<int> &v = tmp[sub];
          for (int j = 0; j < 27; ++j)
            v[j] += iter->second[j];
        }
      }
      mp.swap(tmp);
      cur -= dif;
    }
    string sub = S[i].substr(0, len - 1);
    int pla = S[i].back() - 'a';
    if (mp[sub].size() == 0)
      mp[sub].assign(27, 0);
    else
      res += mp[sub][pla];
    vector<int> &v = mp[sub];
    ++v[pla];
    ++v.back();
  }

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);

  solve();

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using WGraph = vector<vector<pair<int, ll>>>;

template <class T> inline bool chmax(T &a, const T &b) {
  if (b > a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

constexpr int dx[4] = {-1, 0, 0, 1}, dy[4] = {0, -1, 1, 0};
constexpr int INF = 1e9;
constexpr int MOD = 1e9 + 7;
constexpr long long LINF = 1e18;
constexpr double EPS = 1e-10;
constexpr double PI = M_PI;

void solve() {
  int N;
  cin >> N;
  vector<string> S(N);
  for (int i = 0; i < N; ++i)
    cin >> S[i];

  for (int i = 0; i < N; ++i)
    reverse(S[i].begin(), S[i].end());
  sort(S.begin(), S.end(),
       [](auto a, auto b) { return (int)a.size() > (int)b.size(); });
  map<string, vector<int>> mp;
  ll res = 0;
  int cur = (int)S.front().size();
  for (int i = 0; i < N; ++i) {
    int len = (int)S[i].size();
    while (cur >= len) {
      map<string, vector<int>> tmp;
      for (auto iter = mp.begin(); iter != mp.end(); ++iter) {
        string s = iter->first;
        vector<int> v = iter->second;
        string sub = s.substr(0, s.size() - 1);
        int pla = s.back() - 'a';
        v[pla] = v[26];
        if ((int)tmp[sub].size() == 0)
          tmp[sub] = v;
        else {
          vector<int> &vv = tmp[sub];
          for (int j = 0; j < 27; ++j)
            vv[j] += v[j];
        }
      }
      mp.swap(tmp);
      --cur;
    }
    string sub = S[i].substr(0, len - 1);
    int pla = S[i].back() - 'a';
    if (mp[sub].size() == 0)
      mp[sub].assign(27, 0);
    else
      res += mp[sub][pla];
    ++mp[sub][pla];
    ++mp[sub][26];
  }

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);

  solve();

  return 0;
}