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

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
// #define mp make_pair
#define pii pair<int, int>
using namespace std;
void read(int &x) {
  x = 0;
  char c = getchar();
  int p = 1;
  for (; c < 48; c = getchar())
    if (c == '-')
      p = -1;
  for (; c > 47; c = getchar())
    x = (x << 1) + (x << 3) + (c ^ 48);
  x *= p;
}
void read(ll &x) {
  x = 0;
  char c = getchar();
  int p = 1;
  for (; c < 48; c = getchar())
    if (c == '-')
      p = -1;
  for (; c > 47; c = getchar())
    x = (x << 1) + (x << 3) + (c ^ 48);
  x *= p;
}
void Min(int &x, int y) {
  if (x > y)
    x = y;
}
void Min(ll &x, ll y) {
  if (x > y)
    x = y;
}
void Max(int &x, int y) {
  if (x < y)
    x = y;
}
void Max(ll &x, ll y) {
  if (x < y)
    x = y;
}

#define M 200005

int mp[M * 5][26];
int len[M], n, id[M], tot, to[M * 5][26];
string s[M];
bool cmp(int x, int y) { return len[x] < len[y]; }
int cnt[26];
int main() {
  //	freopen("1.in","r",stdin);
  read(n);
  int i, j, k, l;
  for (i = 1; i <= n; i++) {
    cin >> s[i];
    len[i] = s[i].length();
    id[i] = i;
  }
  ll res = 0;
  sort(id + 1, id + n + 1, cmp);
  int p;
  for (k = 1; k <= n; k++) {
    i = id[k];
    for (j = 0; j < len[i]; j++)
      cnt[s[i][j] - 'a']++;
    p = 0;
    for (l = 0; l < 26; l++)
      if (cnt[l]) {
        res += mp[p][l];
      }
    for (j = len[i] - 1; j >= 1; j--) {
      if (!to[p][s[i][j] - 'a']) {
        to[p][s[i][j] - 'a'] = ++tot;
      }
      p = to[p][s[i][j] - 'a'];
      cnt[s[i][j] - 'a']--;
      for (l = 0; l < 26; l++)
        if (cnt[l]) {
          res += mp[p][l];
        }
    }
    mp[p][s[i][j] - 'a']++;
    cnt[s[i][j] - 'a']--;
  }
  printf("%lld\\n", res);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
// #define mp make_pair
#define pii pair<int, int>
using namespace std;
void read(int &x) {
  x = 0;
  char c = getchar();
  int p = 1;
  for (; c < 48; c = getchar())
    if (c == '-')
      p = -1;
  for (; c > 47; c = getchar())
    x = (x << 1) + (x << 3) + (c ^ 48);
  x *= p;
}
void read(ll &x) {
  x = 0;
  char c = getchar();
  int p = 1;
  for (; c < 48; c = getchar())
    if (c == '-')
      p = -1;
  for (; c > 47; c = getchar())
    x = (x << 1) + (x << 3) + (c ^ 48);
  x *= p;
}
void Min(int &x, int y) {
  if (x > y)
    x = y;
}
void Min(ll &x, ll y) {
  if (x > y)
    x = y;
}
void Max(int &x, int y) {
  if (x < y)
    x = y;
}
void Max(ll &x, ll y) {
  if (x < y)
    x = y;
}

#define M 200005
map<string, int> mp[26];
int len[M], n, id[M];
string s[M];

bool cmp(int x, int y) { return len[x] < len[y]; }
int cnt[26];
int main() {
  //	freopen("1.in","r",stdin);
  read(n);
  int i, j, k, l;
  for (i = 1; i <= n; i++) {
    cin >> s[i];
    len[i] = s[i].length();
    id[i] = i;
  }
  ll res = 0;
  sort(id + 1, id + n + 1, cmp);
  string a;
  for (k = 1; k <= n; k++) {
    i = id[k];
    for (j = 0; j < len[i]; j++)
      cnt[s[i][j] - 'a']++;
    a = "";
    for (l = 0; l < 26; l++)
      if (cnt[l]) {
        res += mp[l][a];
      }
    for (j = len[i] - 1; j >= 1; j--) {
      a = a + s[i][j];
      cnt[s[i][j] - 'a']--;
      for (l = 0; l < 26; l++)
        if (cnt[l]) {
          res += mp[l][a];
        }
    }
    mp[s[i][j] - 'a'][a]++;
    cnt[s[i][j] - 'a']--;
  }
  printf("%lld\\n", res);
  return 0;
}