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

// Arayi
// #define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <complex>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define fr first
#define sc second
#define MP make_pair
#define ad push_back
#define PB push_back
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
#define lli long long int
#define y1 arayikhalatyan
#define j1 jigglypuff
#define ld long double
#define itn int
#define pir pair<int, int>
#define all(x) (x).begin(), (x).end()
#define str string
#define enl endl
#define en endl
#define cd complex<long double>
#define vcd vector<cd>
#define vii vector<int>
#define vlli vector<lli>
using namespace std;

lli gcd(lli a, lli b) { return (b == 0LL ? a : gcd(b, a % b)); }
ld dist(ld x, ld y1, ld x2, ld y2) {
  return sqrt((x - x2) * (x - x2) + (y1 - y2) * (y1 - y2));
}
lli S(lli a) { return (a * (a + 1LL)) / 2; }
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
char vow[] = {'a', 'e', 'i', 'o', 'u'};
int dx[] = {1, -1, 0, 0, -1, -1, 1, 1, 0};
int dy[] = {0, 0, 1, -1, -1, 1, -1, 1, 0};

const int N = 1e6 + 30;
const lli mod = 998244353;
const ld pi = acos(-1);
const int T = 238;
const ld e = 1e-13;

lli bp(lli a, lli b = mod - 2LL) {
  lli ret = 1;
  while (b) {
    if (b & 1)
      ret *= a, ret %= mod;
    a *= a;
    a %= mod;
    b >>= 1;
  }
  return ret;
}
ostream &operator<<(ostream &c, pir a) {
  c << a.fr << " " << a.sc;
  return c;
}
struct pt {
  lli x, y;
  int ind;
  int bl;
  bool operator==(const pt &a) { return ind == a.ind; }
  bool operator<(const pt &b) {
    if (x == b.x)
      return y < b.y;
    return x < b.x;
  }
};

int n;
int col[30];
str s[N];
lli p[N], pat;
unordered_map<lli, lli> mp[30], mp1;
pair<int, str> a[N];
int main() {
  fastio;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    a[i] = MP((int)s[i].length(), s[i]);
  }
  p[0] = 1;
  for (int i = 1; i < N; i++) {
    p[i] = (p[i - 1] * 29LL);
  }
  sort(a, a + n);
  for (int i = 0; i < n; i++) {
    s[i] = a[i].sc;
    memset(col, 0, sizeof(col));
    vii fp;
    for (auto p : s[i])
      col[p - 'a']++;
    int l = s[i].length();
    lli sm = 0;
    for (int k = 0; k < 30; k++)
      if (col[k])
        pat += mp[k][sm], fp.ad(k);
    for (int j = l - 1; j > 0; j--) {
      col[s[i][j] - 'a']--;
      if (col[s[i][j] - 'a'] == 0) {
        for (int j = 0; j < fp.size(); j++) {
          if (!col[fp[j]]) {
            swap(fp[j], fp.back());
            fp.pop_back();
          }
        }
      }
      sm += p[l - j - 1] * (lli)(s[i][j]);
      if (mp1[sm]) {
        for (auto p : fp)
          pat += mp[p][sm];
      }
    }
    mp[s[i][0] - 'a'][sm]++;
    mp1[sm]++;
  }
  cout << pat << endl;
  return 0;
}

/*

  __
*(><)*
  \\/ /
  ||/
--||
  ||
  /\\
 /  \\
/    \\
*/

Error: TLE

Fixed submission:

// Arayi
// #define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <complex>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define fr first
#define sc second
#define MP make_pair
#define ad push_back
#define PB push_back
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
#define lli long long int
#define y1 arayikhalatyan
#define j1 jigglypuff
#define ld long double
#define itn int
#define pir pair<int, int>
#define all(x) (x).begin(), (x).end()
#define str string
#define enl endl
#define en endl
#define cd complex<long double>
#define vcd vector<cd>
#define vii vector<int>
#define vlli vector<lli>
using namespace std;

lli gcd(lli a, lli b) { return (b == 0LL ? a : gcd(b, a % b)); }
ld dist(ld x, ld y1, ld x2, ld y2) {
  return sqrt((x - x2) * (x - x2) + (y1 - y2) * (y1 - y2));
}
lli S(lli a) { return (a * (a + 1LL)) / 2; }
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
char vow[] = {'a', 'e', 'i', 'o', 'u'};
int dx[] = {1, -1, 0, 0, -1, -1, 1, 1, 0};
int dy[] = {0, 0, 1, -1, -1, 1, -1, 1, 0};

const int N = 1e6 + 30;
const lli mod = 998244353;
const ld pi = acos(-1);
const int T = 238;
const ld e = 1e-13;

lli bp(lli a, lli b = mod - 2LL) {
  lli ret = 1;
  while (b) {
    if (b & 1)
      ret *= a, ret %= mod;
    a *= a;
    a %= mod;
    b >>= 1;
  }
  return ret;
}
ostream &operator<<(ostream &c, pir a) {
  c << a.fr << " " << a.sc;
  return c;
}
struct pt {
  lli x, y;
  int ind;
  int bl;
  bool operator==(const pt &a) { return ind == a.ind; }
  bool operator<(const pt &b) {
    if (x == b.x)
      return y < b.y;
    return x < b.x;
  }
};

int n;
int col[30];
str s[N];
lli p[N], pat;
unordered_map<lli, lli> mp[30];
pair<int, str> a[N];
int main() {
  fastio;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    a[i] = MP((int)s[i].length(), s[i]);
  }
  p[0] = 1;
  for (int i = 1; i < N; i++) {
    p[i] = (p[i - 1] * 29LL);
  }
  sort(a, a + n);
  for (int i = 0; i < n; i++) {
    s[i] = a[i].sc;
    memset(col, 0, sizeof(col));
    vii fp;
    for (auto p : s[i])
      col[p - 'a']++;
    int l = s[i].length();
    lli sm = 0;
    for (int k = 0; k < 30; k++)
      if (col[k])
        pat += mp[k][sm], fp.ad(k);
    for (int j = l - 1; j > 0; j--) {
      col[s[i][j] - 'a']--;
      if (col[s[i][j] - 'a'] == 0) {
        for (int j = 0; j < fp.size(); j++) {
          if (!col[fp[j]]) {
            swap(fp[j], fp.back());
            fp.pop_back();
          }
        }
      }
      sm += p[l - j - 1] * (lli)(s[i][j]);
      for (auto p : fp)
        pat += mp[p][sm];
    }
    mp[s[i][0] - 'a'][sm]++;
  }
  cout << pat << endl;
  return 0;
}

/*

  __
*(><)*
  \\/ /
  ||/
--||
  ||
  /\\
 /  \\
/    \\
*/