alexjercan/bugnet huggingface dataset:
Problem p02589 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>

using namespace std;
string arr[1000005];
char str[1000005];

int main() {
  // freopen("test.in", "r", stdin);
  //   freopen("ac.txt", "w", stdout);
  int n;
  scanf("%d", &n);
  vector<int> vec;
  for (int i = 0; i < n; ++i) {
    scanf("%s", str);
    arr[i] = string(str);
    vec.push_back(i);
  }
  sort(vec.begin(), vec.end(),
       [](int x, int y) { return arr[x].size() < arr[y].size(); });
  long long ans = 0;
  map<size_t, int> hash;
  for (int i = 0; i < n; ++i) {
    string &S = arr[vec[i]];
    int vis = 0, sz = S.size();
    size_t h = 0;
    for (int i = 0; i < sz; ++i) {
      int id = S[i] - 'a' + 1;
      if ((vis >> id) & 1)
        continue;
      vis |= (1 << id);
      h = 1;
      if (hash.find((h * 31) + id) != hash.end())
        ans += hash[(h * 31) + id];
      for (int j = sz - 1; j > i; --j) {
        h *= 31;
        h += (S[j] - 'a' + 1);
        if (hash.find((h * 31) + id) != hash.end())
          ans += hash[(h * 31) + id];
      }
    }
    h = 1;
    for (int j = sz - 1; ~j; --j) {
      h *= 31;
      h += (S[j] - 'a' + 1);
    }
    ++hash[h];
  }
  cout << ans;
}


Error: TLE

Fixed submission:

#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>

using namespace std;
string arr[1000005];
char str[1000005];

int main() {
  // freopen("test.in", "r", stdin);
  //   freopen("ac.txt", "w", stdout);
  int n;
  scanf("%d", &n);
  vector<int> vec;
  for (int i = 0; i < n; ++i) {
    scanf("%s", str);
    arr[i] = string(str);
    vec.push_back(i);
  }
  sort(vec.begin(), vec.end(),
       [](int x, int y) { return arr[x].size() < arr[y].size(); });
  long long ans = 0;
  map<size_t, int> hash;
  for (int i = 0; i < n; ++i) {
    string &S = arr[vec[i]];
    int vis = 0, sz = S.size();
    size_t h = 0;
    for (int i = 0; i < sz; ++i) {
      int id = S[i] - 'a' + 1;
      if ((vis >> id) & 1)
        continue;
      vis |= (1 << id);
      h = 1;
      ans += hash[(h * 31) + id];
      for (int j = sz - 1; j > i; --j) {
        h *= 31;
        h += (S[j] - 'a' + 1);
        ans += hash[(h * 31) + id];
      }
    }
    h = 1;
    for (int j = sz - 1; ~j; --j) {
      h *= 31;
      h += (S[j] - 'a' + 1);
    }
    ++hash[h];
  }
  cout << ans;
}
Problem p02589 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

#define MAXN 200010
#define LEN 1000010

int n, tot = 1;
int son[LEN][26], cnt[LEN], alpha[MAXN][26];
unordered_map<int, int> val[LEN];
char tmp[LEN];
string s[MAXN];
long long ans, mul;

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%s", tmp + 1);
    s[i] = (tmp + 1);
    int m = strlen(tmp + 1), now = 1, las;
    for (int j = m; j >= 1; --j) {
      las = now;
      if (!son[now][tmp[j] - 'a'])
        son[now][tmp[j] - 'a'] = ++tot;
      now = son[now][tmp[j] - 'a'];
      ++alpha[i][tmp[j] - 'a'];
    }
    ++val[las][tmp[1] - 'a'];
    ++cnt[now];
  }
  for (int i = 1; i <= n; ++i) {
    int m = s[i].length(), now = 1;
    for (auto v : val[now]) {
      if (alpha[i][v.first])
        ans += v.second;
    }
    for (int j = m - 1; j >= 0; --j) {
      now = son[now][s[i][j] - 'a'];
      --alpha[i][s[i][j] - 'a'];
      for (auto v : val[now]) {
        if (alpha[i][v.first])
          ans += v.second;
      }
    }
    mul += cnt[now];
  }
  printf("%lld\\n", ans - n);
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define MAXN 200010
#define LEN 1000010

int n, tot = 1;
int son[LEN][26], cnt[LEN], alpha[MAXN][26];
unordered_map<int, int> val[LEN];
char tmp[MAXN];
string s[MAXN];
long long ans, mul;

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%s", tmp + 1);
    s[i] = (tmp + 1);
    int m = strlen(tmp + 1), now = 1, las;
    for (int j = m; j >= 1; --j) {
      las = now;
      if (!son[now][tmp[j] - 'a'])
        son[now][tmp[j] - 'a'] = ++tot;
      now = son[now][tmp[j] - 'a'];
      ++alpha[i][tmp[j] - 'a'];
    }
    ++val[las][tmp[1] - 'a'];
    ++cnt[now];
  }
  for (int i = 1; i <= n; ++i) {
    int m = s[i].length(), now = 1;
    for (auto v : val[now]) {
      if (alpha[i][v.first])
        ans += v.second;
    }
    for (int j = m - 1; j >= 0; --j) {
      now = son[now][s[i][j] - 'a'];
      --alpha[i][s[i][j] - 'a'];
      for (auto v : val[now]) {
        if (alpha[i][v.first])
          ans += v.second;
      }
    }
    mul += cnt[now];
  }
  printf("%lld\\n", ans - n);
}
Problem p02589 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <ctime>
#include <iostream>
using namespace std;
#define _LL long long
#define rep(i, n) for (_LL i = 0; i < (_LL)(n); i++)
#define vecrep(itr, v) for (auto itr = (v).begin(); itr != (v).end(); itr++)

struct cnode {
  int index[26];
  bool last[26];

  cnode() {
    rep(i, 26) {
      index[i] = -1;
      last[i] = false;
    }
  }
};

cnode g_n[2000000];

int main() {
  int n;
  cin >> n;
  string *s = new string[n];
  pair<int, int> *sindex = new pair<int, int>[n];
  rep(i, n) {
    cin >> s[i];
    sindex[i] = make_pair(s[i].length(), i);
  }
  sort(sindex, sindex + n);
  int cindex = 1;

  map<string, set<int>> m;
  int ans = 0;

  int clist[26];

  rep(i, n) {
    string t = s[sindex[i].second];
    int l = t.length();
    rep(m, 26) clist[m] = 0;
    rep(m, l) clist[t[m] - 'a']++;
    int c = 0;
    int pos = 0;
    while (pos + 1 < l) {
      int nexti = t[l - 1 - pos] - 'a';
      rep(j, 26) {
        if (g_n[c].last[j] && clist[j] > 0)
          ans++;
      }
      clist[nexti]--;

      if (g_n[c].index[nexti] < 0) {
        g_n[c].index[nexti] = cindex++;
      }
      c = g_n[c].index[nexti];
      pos++;
    }
    g_n[c].last[t[0] - 'a'] = true;
  }
  cout << ans;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ctime>
#include <iostream>
using namespace std;
#define _LL long long
#define rep(i, n) for (_LL i = 0; i < (_LL)(n); i++)
#define vecrep(itr, v) for (auto itr = (v).begin(); itr != (v).end(); itr++)

int main() {
  int n;
  cin >> n;
  string *s = new string[n];
  pair<int, int> *sindex = new pair<int, int>[n];
  rep(i, n) {
    cin >> s[i];
    sindex[i] = make_pair(s[i].length(), i);
  }
  sort(sindex, sindex + n);

  map<string, set<int>> m;
  int ans = 0;

  bool clist[30];

  rep(i, n) {
    string t = s[sindex[i].second];
    int l = t.length();
    rep(i, 30) clist[i] = false;
    for (int j = l - 1; j >= 0; j--) {
      clist[t[l - j - 1] - 'a'] = true;
      string subs = t.substr(l - j, j);
      if (m.find(subs) != m.end()) {
        vecrep(itr, m[subs]) {
          if (clist[*itr])
            ans++;
        }
      }
    }
    m[t.substr(1, l - 1)].insert(t[0] - 'a');
  }
  cout << ans;

  return 0;
}
Problem p02589 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

#define TRACE(x) cerr << #x << " :: " << x << endl
#define _ << " " <<
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define RFOR(i, a, b) for (int i = (a); i >= (b); --i)
using ll = long long;
using ii = pair<int, int>;
using pll = pair<ll, ll>;

const int mxN = 2e5 + 5;

const int P = 103;
const int P2 = 1009;
const int P3 = 401;
const int P4 = 709;
const int Q = 1e9 + 11;
const int Q2 = 1e9 + 7;

int N;
string S[mxN];
unordered_map<ll, unordered_map<ll, array<int, 26>>> um;
int haschar[26];
pll lst[mxN];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> N;
  FOR(i, 1, N) {
    cin >> S[i];
    reverse(ALL(S[i]));
  }

  FOR(i, 1, N) {
    int roll = 0, roll2 = 0, roll3 = 0, roll4 = 0;
    int ft[26] = {0};
    RFOR(j, SZ(S[i]) - 1, 0) ft[S[i][j] - 'a']++;
    FOR(k, 0, 25) if (ft[k])++ haschar[k];
    if (SZ(S[i]) > 1) {
      FOR(j, 0, SZ(S[i]) - 1) {
        ft[S[i][j] - 'a']--;
        roll = (ll)roll * P % Q;
        roll = (roll + (S[i][j] - 'a' + 1)) % Q;

        roll2 = (ll)roll2 * P2 % Q;
        roll2 = (roll2 + (S[i][j] - 'a' + 1)) % Q;

        roll3 = (ll)roll3 * P3 % Q2;
        roll3 = (roll3 + (S[i][j] - 'a' + 1)) % Q2;

        roll4 = (ll)roll4 * P4 % Q2;
        roll4 = (roll4 + (S[i][j] - 'a' + 1)) % Q2;

        ll comb1 = (ll)roll * 1000000000 + roll2;
        ll comb2 = (ll)roll3 * 1000000000 + roll4;
        FOR(k, 0, 25) if (ft[k])++ um[comb1][comb2][k];
        if (j == SZ(S[i]) - 2)
          lst[i] = pll(comb1, comb2);
        //~ TRACE(S[i] _ i _ j _ roll);
      }
    }
  }
  ll ans = 0;
  FOR(i, 1, N) {
    if (SZ(S[i]) == 1)
      ans += haschar[S[i][0] - 'a'] - 1;
    else
      ans += um[lst[i].first][lst[i].second][S[i].back() - 'a'] - 1;
  }
  cout << ans << '\\n';
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define TRACE(x) cerr << #x << " :: " << x << endl
#define _ << " " <<
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define RFOR(i, a, b) for (int i = (a); i >= (b); --i)
using ll = long long;
using ii = pair<int, int>;

const int mxN = 2e5 + 5;

int N;
string S[mxN];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> N;
  FOR(i, 1, N) {
    cin >> S[i];
    reverse(ALL(S[i]));
  }

  ll ans = 0;
  FOR(i, 1, N) {
    FOR(j, i + 1, N) {
      string a = S[i], b = S[j];
      if (SZ(a) > SZ(b))
        swap(a, b);
      bool ok = 1;
      FOR(k, 0, SZ(a) - 2) { ok &= a[k] == b[k]; }
      bool ok2 = 0;
      FOR(k, SZ(a) - 1, SZ(b) - 1) { ok2 |= a.back() == b[k]; }
      ok &= ok2;
      ans += ok;
    }
  }
  cout << ans << '\\n';
}
Problem p02589 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ll long long
#define pp pair<ll, ll>
#define ld long double
#define all(a) (a).begin(), (a).end()
#define mk make_pair
int inf = 1000001000;
ll INF = 1e18;
ll mod = 1000000007;
ll MOD = INF;

int main() {
  int n;
  cin >> n;
  int r = inf;
  vector<string> a(n);
  vector<pair<string, string>> mm(n);
  rep(i, n) cin >> a[i];
  rep(i, n) reverse(all(a[i]));
  vector<vector<int>> e(n, vector<int>(26, -1));
  rep(i, n) {
    int w = a[i].size();
    r = min(r, w);
    string s;
    rep(j, a[i].size()) {
      if (j == a[i].size() - 1)
        mm[i] = mk(s, a[i]);
      s.push_back(a[i][j]);
    }
  }
  sort(all(mm));
  rep(i, n) a[i] = mm[i].second;
  rep(i, n) {
    string s;
    rep(j, a[i].size()) { e[i][a[i][j] - 'a'] = j; }
  }
  map<string, vector<char>> m;
  int ans = 0;
  rep(i, n) {
    string s = "*";
    rep(j, a[i].size() + 1) {
      if (j >= r - 1) {
        auto it = m.find(s);
        if (it != m.end()) {
          rep(k, m[s].size()) {
            if (e[i][m[s][k] - 'a'] >= j) {
              ans++;
            }
          }
        }
      }
      //   rep(k,26){
      //       char y='a'+k;
      //       if (e[i][k]>=j) {
      //           auto it=m.find(mk(s,y));
      //           if (it!=m.end()) ans+=m[mk(s,y)];
      //           }
      //   }
      if (j == a[i].size() - 1)
        m[s].push_back(a[i][a[i].size() - 1]);
      if (j < a[i].size())
        s.push_back(a[i][j]);
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ll long long
#define pp pair<ll, ll>
#define ld long double
#define all(a) (a).begin(), (a).end()
#define mk make_pair
int inf = 1000001000;
ll INF = 1e18;
ll mod = 1000000007;
ll MOD = INF;

int main() {
  int n;
  cin >> n;
  vector<string> a(n);
  vector<pair<string, string>> mm(n);
  rep(i, n) cin >> a[i];
  rep(i, n) reverse(all(a[i]));
  vector<vector<int>> e(n, vector<int>(26, -1));
  rep(i, n) {
    string s;
    rep(j, a[i].size()) {
      if (j == a[i].size() - 1)
        mm[i] = mk(s, a[i]);
      s.push_back(a[i][j]);
    }
  }
  sort(all(mm));
  rep(i, n) a[i] = mm[i].second;
  rep(i, n) {
    string s;
    rep(j, a[i].size()) { e[i][a[i][j] - 'a'] = j; }
  }
  map<string, vector<char>> m;
  int ans = 0;
  rep(i, n) {
    string s = "*";
    rep(j, a[i].size() + 1) {
      auto it = m.find(s);
      if (it != m.end()) {
        rep(k, m[s].size()) {
          if (e[i][m[s][k] - 'a'] >= j) {
            ans++;
          }
        }
      }
      //   rep(k,26){
      //       char y='a'+k;
      //       if (e[i][k]>=j) {
      //           auto it=m.find(mk(s,y));
      //           if (it!=m.end()) ans+=m[mk(s,y)];
      //           }
      //   }
      if (j == a[i].size() - 1)
        m[s].push_back(a[i][a[i].size() - 1]);
      if (j < a[i].size())
        s.push_back(a[i][j]);
    }
  }
  cout << ans << endl;
}
Problem p02589 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using ll = long long;

struct Node {
  Node *c[26];
  int cnt;
  Node() : c{}, cnt(0) {}
};

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int n;
  std::cin >> n;
  std::vector<std::string> g(n);
  for (auto &x : g)
    std::cin >> x;
  std::sort(g.begin(), g.end(),
            [&](auto a, auto b) { return a.size() < b.size(); });
  ll ans = 0;
  Node *root = new Node();
  for (int i = 0; i < n; ++i) {
    std::vector<int> m{0};
    for (auto c : g[i])
      m.push_back(m.back() | (1 << (c - 'a')));
    Node *ptr = root;
    for (auto c : std::string(g[i].rbegin(), g[i].rend())) {
      for (int k = 0; k < 26; ++k)
        if (((1 << k) & m.back()) && k != c - 'a' && ptr->c[k] != nullptr)
          ans += ptr->c[k]->cnt;
      if (!ptr->c[c - 'a'])
        ptr->c[c - 'a'] = new Node();
      ptr = ptr->c[c - 'a'];
      ans += ptr->cnt;
      m.pop_back();
    }
    ptr->cnt++;
  }
  std::cout << ans << '\\n';
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using ll = long long;

const int MAX = 8e6;

int children[MAX][26], cnt[MAX], it = 0;

int get_child(int s, int k) {
  auto &c = children[s][k];
  if (!c) {
    c = it++;
    if (c >= MAX)
      while (1)
        ;
  }
  return c;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int n;
  std::cin >> n;
  std::vector<std::string> g(n);
  for (auto &x : g)
    std::cin >> x;
  std::sort(g.rbegin(), g.rend(),
            [&](auto a, auto b) { return a.size() < b.size(); });
  ll ans = 0;
  int root = it++;
  for (int i = 0; i < n; ++i) {
    std::vector<int> m{0};
    for (auto c : g[i])
      m.push_back(m.back() | (1 << (c - 'a')));
    int s = root;
    for (auto c : std::string(g[i].rbegin(), g[i].rend())) {
      for (int k = 0; k < 26; ++k)
        if (((1 << k) & m.back()) && k != c - 'a')
          cnt[get_child(s, k)]++;
      s = get_child(s, c - 'a');
      cnt[s]++;
      m.pop_back();
    }
    ans += cnt[s] - 1;
  }
  std::cout << ans << '\\n';
}
Problem p02589 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define ll long long
#define maxn 2000005
#define inf 1e9
#define pb push_back
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
using namespace std;

inline int read() {
  int x = 0, w = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      w = -1;
    c = getchar();
  }
  while (c <= '9' && c >= '0') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return w == 1 ? x : -x;
}

string s[maxn];
int n, ch[maxn][26], cnt, siz[maxn][26], tmp[maxn][26];

inline void ins(int u, int x, int p) {
  if (p < 0)
    return;
  int q = s[x][p] - 'a';
  tmp[u][q]++;
  if (!ch[u][q])
    ch[u][q] = ++cnt;
  ins(ch[u][q], x, p - 1);
  rep(i, 0, 25) {
    tmp[u][i] += tmp[ch[u][q]][i];
    if (tmp[u][i])
      tmp[u][i] = 1;
    siz[u][i] += tmp[u][i];
  }
}

inline void cl(int u, int x, int p) {
  if (p < 0)
    return;
  int q = s[x][p] - 'a';
  rep(i, 0, 25) tmp[u][i] = 0;
  cl(ch[u][q], x, p - 1);
}

inline ll Q(int u, int x, int p) {
  if (p == 0)
    return siz[u][s[x][0] - 'a'] - 1;
  int q = s[x][p] - 'a';
  if (!ch[u][q])
    return 0;
  return Q(ch[u][q], x, p - 1);
}

int main() {
  n = read();
  cnt = 1;
  rep(i, 1, n) {
    cin >> s[i], ins(1, i, s[i].length() - 1);
    cl(1, i, s[i].length() - 1);
  }
  ll ans = 0;
  rep(i, 1, n) ans += Q(1, i, s[i].length() - 1);
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define ll long long
#define maxn 2000005
#define inf 1e9
#define pb push_back
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
using namespace std;

ll n, a[maxn], b[maxn], c[maxn];

inline ll read() {
  ll x = 0, w = 1, x2 = 0, base = 1e9;
  char c = getchar();
  while (c < '0' || c > '9')
    c = getchar();
  while (c <= '9' && c >= '0') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  if (c == '.') {
    while (c < '0' || c > '9')
      c = getchar();
    while (c <= '9' && c >= '0') {
      x2 = (x2 << 1) + (x2 << 3) + c - '0';
      base /= 10;
      c = getchar();
    }
    return x * 1e9 + x2 * base;
  } else
    return x * 1e9;
}

struct node {
  int a, b;
};

inline bool operator<(node a, node b) {
  if (a.a != b.a)
    return a.a < b.a;
  return a.b < b.b;
}

ll dp[55][55];

int main() {
  ll n, ans = 0;
  cin >> n;
  rep(i, 1, n) a[i] = read();
  rep(i, 1, n) {
    int c1 = 0, c2 = 0;
    while (a[i] % 2 == 0)
      a[i] /= 2, c1++;
    while (a[i] % 5 == 0)
      a[i] /= 5, c2++;
    rep(j, 18 - c1, 50) rep(k, 18 - c2, 50) ans += dp[j][k];
    dp[c1][c2]++;
  }
  cout << ans << endl;
  return 0;
}
Problem p02589 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define REP(i, start, end) for (ll i = start, i##Len = (end); i < i##Len; ++i)
#define REPR(i, start, end) for (ll i = start, i##Len = (end); i > i##Len; --i)
using ll = long long;
using namespace std;

struct Node {
  int end_count = 0;
  bool init = 0;
  vector<Node> nodes;

  void add(string &s) {
    if (s.empty())
      end_count++;
    else {
      if (!init) {
        nodes.assign(26, Node{});
        init = 1;
      }
      int idx = s.back() - 'a';
      s.pop_back();
      nodes[idx].add(s);
    }
  }
};

struct Trie {
  Node node = Node{};

  void add(string &s) { node.add(s); }
};

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

  int N;
  cin >> N;
  vector<string> a(N);
  REP(i, 0, N)
  cin >> a[i];
  sort(a.begin(), a.end(),
       [](string a, string b) { return a.size() < b.size(); });
  auto trie = Trie{};
  ll ans = 0;

  for (auto &w : a) {
    vector<int> cnt(26);
    for (auto &c : w)
      cnt[c - 'a']++;

    Node *node = &trie.node;
    REPR(i, w.size() - 1, -1) {
      if (!node->init) {
        break;
      }
      REP(j, 0, 26) {
        if (cnt[j] > 0)
          ans += node->nodes[j].end_count;
      }
      cnt[w[i] - 'a']--;
      node = &node->nodes[w[i] - 'a'];
    }

    trie.add(w);
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define REP(i, start, end) for (ll i = start, i##Len = (end); i < i##Len; ++i)
#define REPR(i, start, end) for (ll i = start, i##Len = (end); i > i##Len; --i)
using ll = long long;
using namespace std;

struct Node {
  int end_count = 0;
  bool init = 0;
  vector<Node> nodes;

  void add(string &s) {
    if (s.empty())
      end_count++;
    else {
      if (!init) {
        nodes.assign(26, Node{});
        init = 1;
      }
      int idx = s.back() - 'a';
      s.pop_back();
      nodes[idx].add(s);
    }
  }
};

struct Trie {
  Node node = Node{};

  void add(string &s) { node.add(s); }
};

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

  int N;
  cin >> N;
  vector<string> a(N);
  REP(i, 0, N)
  cin >> a[i];
  sort(a.begin(), a.end(),
       [](string a, string b) { return a.size() < b.size(); });
  auto trie = Trie{};
  ll ans = 0;

  for (auto &w : a) {
    vector<int> cnt(26);
    for (auto &c : w)
      cnt[c - 'a']++;

    auto node = trie.node;
    REPR(i, w.size() - 1, -1) {
      if (!node.init)
        break;
      REP(j, 0, 26) {
        if (cnt[j] > 0)
          ans += node.nodes[j].end_count;
      }
      cnt[w[i] - 'a']--;
      auto newnode = node.nodes[w[i] - 'a'];
      node = newnode;
    }

    trie.add(w);
  }

  cout << ans << endl;
}
Problem p02589 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;

const int N = 1e6 + 5;

int nxt[N][26], cnt[N][26], cur = 0;

void insert(string s) {
  vector<int> c(26, 0);
  int idx = 0;
  for (auto i : s)
    c[i - 'a']++;
  for (int j = 0; j < 26; j++)
    cnt[idx][j] += (c[j] > 0);
  for (auto i : s) {
    c[i - 'a']--;
    if (!nxt[idx][i - 'a']) {
      nxt[idx][i - 'a'] = ++cur;
    }
    idx = nxt[idx][i - 'a'];
    for (int j = 0; j < 26; j++)
      cnt[idx][j] += (c[j] > 0);
  }
}

int solve(string s) {
  int n = s.length(), idx = 0;
  for (int i = 0; i < n - 1; i++) {
    idx = nxt[idx][s[i] - 'a'];
  }

  return cnt[idx][s[n - 1] - 'a'] - 1;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int n;
  cin >> n;
  vector<string> s(n);
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    reverse(s[i].begin(), s[i].end());
    insert(s[i]);
  }

  ll ans = 0;
  for (int i = 0; i < n; i++) {
    ans += solve(s[i]);
  }

  cout << ans << '\\n';
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;

const int N = 2e5 + 5;

int nxt[N][26], cnt[N][26], cur = 0;

void insert(string s) {
  vector<int> c(26, 0);
  int idx = 0;
  for (auto i : s)
    c[i - 'a']++;
  for (int j = 0; j < 26; j++)
    cnt[idx][j] += (c[j] > 0);
  for (auto i : s) {
    c[i - 'a']--;
    if (!nxt[idx][i - 'a']) {
      nxt[idx][i - 'a'] = ++cur;
    }
    idx = nxt[idx][i - 'a'];
    for (int j = 0; j < 26; j++)
      cnt[idx][j] += (c[j] > 0);
  }
}

int solve(string s) {
  int n = s.length(), idx = 0;
  for (int i = 0; i < n - 1; i++) {
    idx = nxt[idx][s[i] - 'a'];
  }

  return cnt[idx][s[n - 1] - 'a'] - 1;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int n;
  cin >> n;
  vector<string> s(n);
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    reverse(s[i].begin(), s[i].end());
    insert(s[i]);
  }

  ll ans = 0;
  for (int i = 0; i < n; i++) {
    ans += solve(s[i]);
  }

  cout << ans << '\\n';
}