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

#include <bits/stdc++.h>
#define MAX (ll)(1e6 + 7)
#define INF (ll)(1e18)
#define MOD (ll)(1e9 + 7)

using namespace std;
using ll = long long;
using cd = complex<double>;
ll md[2] = {231, 3006703054056749LL}, pw[MAX];

ll mult(ll a, ll b, bool fl) { return (a * 1LL * b) % md[fl]; }

ll add(ll a, ll b, bool fl) {
  b += md[fl], a += md[fl];
  return (a % md[fl] + b % md[fl]) % md[fl];
}

map<ll, int> m1;

int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(nullptr);
  pw[0] = 1;
  vector<pair<int, int>> vp;
  for (int i = 1; i < MAX; i++) {
    pw[i] = mult(md[0], pw[i - 1], 1);
  }
  vector<ll> vv;
  ll n;
  cin >> n;
  vector<string> vs(n);
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> vs[i];
    vp.push_back(make_pair((int)vs[i].size(), i));
  }
  sort(vp.begin(), vp.end());
  for (int y = 0; y < n; y++) {
    int i = vp[y].second, nn = vp[y].first;
    int ch[26] = {};
    for (char x : vs[i]) {
      ch[x - 'a']++;
    }
    ll h = 0;
    for (int j = 0; j < vs[i].size(); j++) {
      for (int k = 0; k < 26; k++) {
        ll r = 0;
        if (ch[k]) {
          r = add(h, mult(pw[j], k + 1, 1), 1);
          auto x = m1.find(r);
          if (x != m1.end()) {
            ans += x->second;
          }
        }
      }
      int x = vs[i][nn - j - 1] - 'a';
      h = add(h, mult(pw[j], x + 1, 1), 1);
      ch[x]--;
    }
    m1[h]++;
  }
  cout << ans << "\\n";
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define MAX (ll)(1e6 + 7)
#define INF (ll)(1e18)
#define MOD (ll)(1e9 + 7)

using namespace std;
using ll = long long;
using cd = complex<double>;
ll md[2] = {231, 3006703054056749LL}, pw[MAX];

ll mult(ll a, ll b, bool fl) { return (a * 1LL * b) % md[fl]; }

ll add(ll a, ll b, bool fl) {
  b += md[fl], a += md[fl];
  return (a % md[fl] + b % md[fl]) % md[fl];
}

map<ll, int> m1;

int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(nullptr);
  pw[0] = 1;
  for (int i = 1; i < MAX; i++) {
    pw[i] = mult(md[0], pw[i - 1], 1);
  }
  vector<ll> vv;
  ll n;
  cin >> n;
  vector<string> vs(n);
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> vs[i];
    reverse(vs[i].begin(), vs[i].end());
    int ch[26] = {};
    for (char x : vs[i]) {
      ch[x - 'a']++;
    }
    ll h = 0;
    for (int j = 0; j < vs[i].size(); j++) {
      for (int k = 0; k < 26; k++) {
        ll r = 0;
        if (ch[k]) {
          r = add(h, mult(pw[j], k + 1, 1), 1);
          vv.push_back(r);
        }
      }
      int x = vs[i][j] - 'a';
      h = add(h, mult(pw[j], x + 1, 1), 1);
      ch[x]--;
    }
    m1[h]++;
  }
  sort(vv.begin(), vv.end());
  for (auto x : vv) {
    while (m1.size() && m1.begin()->first < x) {
      m1.erase(m1.begin());
    }
    if (m1.size() && m1.begin()->first == x) {
      ans += (m1.begin()->second);
    }
  }
  cout << (ans - n) << "\\n";
}
Problem p02589 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 200005, M = 1000005;
int n;
string s[N];
struct Node {
  int col[26];
  int ch[26];
} trie[M];
int tot = 1;
void insert(const string &s) {
  static int col[26];
  int len = s.size();
  for (int i = 0; i < len; i++)
    col[s[i] - 'a']++;
  int u = 1;
  for (int i = 0; i < len; i++) {
    int c = s[i] - 'a';
    for (int j = 0; j < 26; j++)
      if (col[j])
        trie[u].col[j]++;
    if (!trie[u].ch[c])
      trie[u].ch[c] = ++tot;
    u = trie[u].ch[c];
    col[c]--;
  }
  return;
}
int query(const string &s) {
  int len = s.size();
  int u = 1;
  for (int i = 0; i < len - 1; i++)
    u = trie[u].ch[s[i] - 'a'];
  return trie[u].col[s[len - 1] - 'a'] - 1;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    reverse(s[i].begin(), s[i].end());
    insert(s[i]);
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++)
    ans += query(s[i]);
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 200005, M = 1000005;
int n;
struct Trie {
  int ch[26];
  int siz;
} trie[M];
int tot = 1;
void insert(const string &s) {
  int len = s.size();
  int u = 1;
  trie[u].siz++;
  for (int i = 0; i < len; i++) {
    int c = s[i] - 'a';
    if (!trie[u].ch[c])
      trie[u].ch[c] = ++tot;
    u = trie[u].ch[c];
    trie[u].siz++;
  }
  return;
}
int res;
void dfs(int u, int c) {
  if (!u)
    return;
  for (int i = 0; i < 26; i++)
    if (i != c)
      dfs(trie[u].ch[i], c);
  if (trie[u].ch[c])
    res += trie[trie[u].ch[c]].siz;
  return;
}
int query(const string &s) {
  int len = s.size();
  int u = 1;
  for (int i = 0; i < len - 1; i++) {
    int c = s[i] - 'a';
    if (!trie[u].ch[c])
      return 0;
    u = trie[u].ch[c];
  }
  res = -1;
  dfs(u, s[len - 1] - 'a');
  return res;
}
string s[N];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    int len = s[i].size();
    reverse(s[i].begin(), s[i].end());
    insert(s[i]);
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++)
    ans += query(s[i]);
  printf("%lld", ans);
  return 0;
}
Problem p02589 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAX = 200002;
const int MOD = 1000000007;
int mp[('z' - 'a') + 1][1000001];
signed main() {
  int n;

  cin >> n;
  string s[n], ss;
  pair<string, char> p[n];
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    reverse(s[i].begin(), s[i].end());
    char c = s[i][s[i].size() - 1];
    s[i].pop_back();
    p[i] = {s[i], c};
  }
  sort(p, p + n);
  int ans = 0;
  for (int y = 0; y <= ('z' - 'a'); y++)
    mp[y][0] = 0;
  for (int i = 0; i < n; i++) {
    int k = 0;
    s[i] = p[i].first;
    s[i].push_back(p[i].second);
    bool t[('z' - 'a') + 1] = {};
    while (i != 0 && k < s[i - 1].size() - 1 && k < s[i].size() - 1 &&
           s[i - 1][k] == s[i][k])
      k++;
    for (int j = k + 1; j < s[i].size(); j++)
      for (int y = 0; y <= ('z' - 'a'); y++)
        mp[y][j] = 0;
    for (int j = s[i].size() - 1; j >= 0; j--) {
      t[(s[i][j] - 'a')] = true;
      for (int jj = 0; jj <= ('z' - 'a'); jj++)
        if (t[jj])
          ans += mp[jj][j];
    }
    mp[(s[i][s[i].size() - 1] - 'a')][s[i].size() - 1]++;
  }
  cout << ans << endl;
  // for(int i=0;i<n;i++)cerr<<s[i]<<endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAX = 200002;
const int MOD = 1000000007;

signed main() {
  int n;
  map<pair<char, string>, int> mp;
  cin >> n;
  string s[n], ss;

  for (int i = 0; i < n; i++) {
    cin >> s[i];
    int k = s[i].size();
    reverse(s[i].begin(), s[i].end());

    ss = s[i];
    ss.pop_back();
    mp[{s[i][s[i].size() - 1], ss}]++;
  }

  int ans = 0;
  for (int i = n - 1; i >= 0; i--) {
    bool t[('z' - 'a') + 1] = {};

    while (!s[i].empty()) {
      t[(s[i][s[i].size() - 1] - 'a')] = true;
      s[i].pop_back();
      for (char j = 'a'; j <= ('z'); j++)
        if (t[(j - 'a')])
          ans += (mp.find({j, s[i]}) != mp.end());
    }
  }
  cout << ans - n << endl;
}
Problem p02589 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define finish(x) return cout << x << endl, 0
#define ll long long

int n;
ll base = 137, mod = 5729857252333;
vector<string> a;
unordered_map<ll, int> cnt;

ll add(ll hash, char c) { return (1LL * hash * base + c - 'a' + 1) % mod; }
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  a.resize(n);
  for (auto &i : a)
    cin >> i;
  sort(a.begin(), a.end(),
       [&](string &l, string &r) { return l.size() > r.size(); });
  for (auto &i : a) {
    reverse(i.begin(), i.end());
  }
  unordered_set<ll> all;
  for (auto &i : a) {
    ll h = 0;
    for (auto &j : i) {
      h = add(h, j);
    }
    all.insert(h);
  }
  ll ans = 0;
  vector<int> cs(200);
  for (auto &i : a) {
    ll h = 0;
    for (auto &j : i) {
      h = add(h, j);
    }
    ans += cnt[h];
    h = 0;
    for (auto &j : i)
      cs[j]++;
    for (int j = 0; j < (int)i.size(); j++) {
      for (char k = 'a'; k <= 'z'; k++) {
        if (cs[k] == 0)
          continue;
        ll cur = add(h, k);
        if (all.count(cur))
          cnt[cur]++;
      }
      h = add(h, i[j]);
      cs[i[j]]--;
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.2,popcnt,abm,mmx,avx")

#include <bits/stdc++.h>
using namespace std;
#define finish(x) return cout << x << endl, 0
#define ll long long

int n;
ll base = 137, mod = 5729857252333;
vector<string> a;

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
gp_hash_table<ll, int> cnt;

ll add(ll hash, char c) { return (1LL * hash * base + c - 'a' + 1) % mod; }
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  a.resize(n);
  for (auto &i : a)
    cin >> i;
  sort(a.begin(), a.end(),
       [&](string &l, string &r) { return l.size() > r.size(); });
  for (auto &i : a) {
    reverse(i.begin(), i.end());
  }
  ll ans = 0;
  vector<int> cs(200);
  for (auto &i : a) {
    ll h = 0;
    for (auto &j : i) {
      h = add(h, j);
    }
    ans += cnt[h];
    h = 0;
    for (auto &j : i)
      cs[j]++;
    for (int j = 0; j < (int)i.size(); j++) {
      for (char k = 'a'; k <= 'z'; k++) {
        if (cs[k] == 0)
          continue;
        cnt[add(h, k)]++;
      }
      h = add(h, i[j]);
      cs[i[j]]--;
    }
  }
  cout << ans << endl;
}
Problem p02589 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC optimize("Ofast")
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
// #define vi vector<int>
#define all(a) a.begin(), a.end()
typedef pair<int, int> P;
const long long mod1 = 998244353;
const long long mod2 = 924844033;
const long long base1 = 1007;
const long long base2 = 2009;
const long long inf = 1ll << 61;

struct RH {
  int hash1, hash2, bit;
};
string s[200006];
int hash1[1000006], hash2[1000006];
map<P, int> hashs;
RH koho[1000006];

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  rep(i, n) {
    cin >> s[i];
    reverse(all(s[i]));
  }

  int ci = 0;
  rep(i, n) {
    int z = s[i].size();
    rep(j, z) {
      hash1[j + 1] = hash1[j] * base1 + s[i][j];
      hash1[j + 1] %= mod1;
      hash2[j + 1] = hash2[j] * base2 + s[i][j];
      hash2[j + 1] %= mod2;
    }

    hashs[P(hash1[z], hash2[z])]++;
    int fil = 0;
    for (int j = z - 1; j >= 0; j--) {
      fil |= (1 << (s[i][j] - 'a'));
      koho[ci] = {hash1[j], hash2[j], fil};
      ci++;
    }
  }

  int ans = 0;

  rep(i, ci) {
    int h1 = koho[i].hash1, h2 = koho[i].hash2;
    rep(j, 26) {
      if (koho[i].bit & (1 << j)) {
        int i1 = h1 * base1 + j + 'a';
        int i2 = h2 * base2 + j + 'a';
        i1 %= mod1;
        i2 %= mod2;
        auto itr = hashs.find(P(i1, i2));
        if (itr != hashs.end())
          ans += itr->second;
      }
    }
  }
  cout << ans - n << '\\n';
}

Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
// #define vi vector<int>
#define all(a) a.begin(), a.end()
typedef pair<int, int> P;
const long long mod1 = 998244353;
const long long mod2 = 924844033;
const long long base1 = 1007;
const long long base2 = 2009;
const long long inf = 1ll << 61;

struct RH {
  int hash1, hash2, bit;
};
string s[200006];
int hash1[1000006], hash2[1000006];
map<P, int> hashs;
vector<RH> koho;

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int S = 0;
  int n;
  cin >> n;
  rep(i, n) {
    cin >> s[i];
    reverse(all(s[i]));
    S += s[i].size();
  }
  koho.reserve(S + 1);

  rep(i, n) {
    int z = s[i].size();
    rep(j, z) {
      hash1[j + 1] = hash1[j] * base1 + s[i][j];
      hash1[j + 1] %= mod1;
      hash2[j + 1] = hash2[j] * base2 + s[i][j];
      hash2[j + 1] %= mod2;
    }

    hashs[P(hash1[z], hash2[z])]++;
    int fil = 0;
    for (int j = z - 1; j >= 0; j--) {
      fil |= (1 << (s[i][j] - 'a'));
      koho.push_back({hash1[j], hash2[j], fil});
    }
  }

  int ans = 0;

  for (auto e : koho) {
    int h1 = e.hash1, h2 = e.hash2;
    rep(j, 26) {
      if (e.bit & (1 << j)) {
        int i1 = h1 * base1 + j + 'a';
        int i2 = h2 * base2 + j + 'a';
        i1 %= mod1;
        i2 %= mod2;
        ans += hashs[P(i1, i2)];
      }
    }
  }
  cout << ans - n << endl;
}
Problem p02589 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#include <string.h>
#include <vector>
int C = 2;
int count[1000010][30];
int next[1000010][30];
char x[1000010];
std::vector<char> V[200010];
int check[30];

int main() {
  int a;
  scanf("%d", &a);
  for (int i = 1; i <= a; i++) {
    scanf("%s", x + 1);
    int b = strlen(x + 1);
    for (int j = 1; j <= b; j++)
      V[i].push_back(x[j]);
  }
  for (int i = 1; i <= a; i++) {
    for (int j = 0; j <= 25; j++)
      check[j] = 12345678;
    for (int j = V[i].size() - 1; j >= 0; j--)
      check[V[i][j] - 'a'] = j;

    int p = 1;
    for (int j = V[i].size(); j >= 1; j--) {
      for (int k = 0; k <= 25; k++)
        if (check[k] < j)
          count[p][k]++;
      if (next[p][V[i][j - 1] - 'a'] == 0)
        next[p][V[i][j - 1] - 'a'] = C++;
      p = next[p][V[i][j - 1] - 'a'];
    }
  }

  long long int ans = 0;
  for (int i = 1; i <= a; i++) {
    int p = 1;
    for (int j = V[i].size() - 1; j >= 1; j--)
      p = next[p][V[i][j] - 'a'];
    ans += count[p][V[i][0] - 'a'];
  }
  printf("%lld", ans - a);
}

Error: WA

Fixed submission:

#include <map>
#include <stdio.h>
#include <string.h>
#include <vector>
#define MOD 99999971
std::vector<char> V[2000010];
std::map<long long int, int> count[30];
int check[30];
char x[200010];
int main() {
  int a;
  scanf("%d", &a);
  for (int i = 1; i <= a; i++) {
    scanf("%s", x + 1);
    int b = strlen(x + 1);
    for (int j = 1; j <= b; j++)
      V[i].push_back(x[j]);
  }
  for (int i = 1; i <= a; i++) {
    for (int j = 0; j <= 26; j++)
      check[j] = 12345678;
    for (int j = 0; j < V[i].size(); j++)
      if (check[V[i][j] - 'a'] == 12345678)
        check[V[i][j] - 'a'] = j;

    long long int s = 0;
    for (int k = 0; k <= 25; k++)
      if (check[k] < V[i].size())
        count[k][s]++;

    for (int j = V[i].size() - 1; j >= 0; j--) {
      s *= MOD;
      s += V[i][j];
      for (int k = 0; k <= 25; k++)
        if (check[k] < j)
          count[k][s]++;
    }
  }

  long long int ans = 0;
  for (int i = 1; i <= a; i++) {
    long long int s = 0;
    for (int j = V[i].size() - 1; j >= 1; j--) {
      s *= MOD;
      s += V[i][j];
    }
    ans += count[V[i][0] - 'a'][s];
  }

  printf("%lld", ans - a);
}
Problem p02589 (C++) — buggy submission, verdict: Runtime Error

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

struct node {
  node *nex[26];
  int tot[26];
  int sum;
  node() {
    memset(nex, 0, sizeof(nex));
    memset(tot, 0, sizeof(tot));
    sum = 0;
  }
} *root = new node;

string s[200005];

node *add(string &s) {
  node *cur = root;
  node *old = root;
  for (char c : s) {
    cur->sum++;
    int i = c - 'a';
    if (cur->nex[i] == nullptr)
      cur->nex[i] = new node;
    old = cur;
    cur = cur->nex[i];
  }
  cur->sum++;
  return old;
}

void comp(node *cur) {
  for (int i = 0; i < 26; i++) {
    if (cur->nex[i] == nullptr)
      continue;
    comp(cur->nex[i]);
    cur->tot[i] += cur->nex[i]->sum;
    for (int j = 0; j < 26; j++) {
      if (i == j)
        continue;
      cur->tot[j] += cur->nex[i]->tot[j];
    }
  }
}

int main() {
#ifdef LOCAL_EXEC
//  freopen("sample.in", "r", stdin);
#else
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
#endif

  int n;
  cin >> n;
  node *pre[200005];
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    reverse(s[i].begin(), s[i].end());
    pre[i] = add(s[i]);
  }
  comp(root);

  ll sum = 0;
  for (int i = 0; i < n; i++) {
    sum += pre[i]->tot[s[i].back() - 'a'];
  }

  cout << sum - n << '\\n';

  return 0;
}

Error: 0

Fixed submission:

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

struct node {
  node *nex[26];
  int tot[26];
  int sum;
} *root = new node;

string s[200005];

node *add(string &s) {
  node *cur = root;
  node *old = nullptr;
  for (char c : s) {
    cur->sum++;
    int i = c - 'a';
    if (cur->nex[i] == nullptr)
      cur->nex[i] = new node;
    old = cur;
    cur = cur->nex[i];
  }
  cur->sum++;
  return old;
}

void comp(node *cur) {
  for (int i = 0; i < 26; i++) {
    if (cur->nex[i] == nullptr)
      continue;
    comp(cur->nex[i]);
    cur->tot[i] += cur->nex[i]->sum;
    for (int j = 0; j < 26; j++) {
      if (i == j)
        continue;
      cur->tot[j] += cur->nex[i]->tot[j];
    }
  }
}

int main() {
#ifdef LOCAL_EXEC
//  freopen("sample.in", "r", stdin);
#else
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
#endif

  int n;
  cin >> n;
  node *pre[200005];
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    reverse(s[i].begin(), s[i].end());
    pre[i] = add(s[i]);
  }
  comp(root);

  ll sum = 0;
  for (int i = 0; i < n; i++) {
    sum += pre[i]->tot[s[i].back() - 'a'];
  }

  cout << sum - n << '\\n';

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int n, ap[1 << 20][26];
ll ans;
char buf[1 << 20];
string s[1 << 18];
struct trie {
  struct {
    int ch[26], fin, vis = -1;
  } a[1 << 21];
  int tot, rt;
  void ins(char *s, int l, int &p) {
    if (!p)
      p = ++tot;
    if (l < 0) {
      a[p].fin = 1;
      return;
    }
    ins(s - 1, l - 1, a[p].ch[*s - 'a']);
  }
} tr;
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf(" %s", buf), s[i] = buf;
    int n = s[i].size();
    tr.ins(&s[i].back(), n - 1, tr.rt);
  }
  for (int i = 0; i < n; i++) {
    int n = s[i].size();
    int p = tr.rt;
    for (int j = 0; j < 26; j++)
      ap[0][j] = j + 'a' == s[i][0];
    for (int j = 1; j < n; j++) {
      memcpy(ap[j], ap[j - 1], sizeof ap[0]);
      ap[j][s[i][j] - 'a'] = 1;
    }
    for (int j = n - 1; ~j; j--) {
      if (j)
        for (int k = 0; k < 26; k++)
          if (ap[j - 1][k]) {
            int np = tr.a[p].ch[k];
            if (!np || tr.a[np].vis == i)
              continue;
            tr.a[np].vis = i;
            if (tr.a[np].fin)
              ans++;
          }
      if (tr.a[p].vis != i) {
        tr.a[p].vis = i;
        if (tr.a[p].fin)
          ans++;
      }
      p = tr.a[p].ch[s[i][j] - 'a'];
    }
  }
  printf("%lld\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll mod = 100000000002482357;
int n, ap[1 << 20][26];
ll ans;
char buf[1 << 20];
string s[1 << 18];
unordered_set<ll> st;
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf(" %s", buf), s[i] = buf;
    int n = s[i].size();
    ll hs = 0;
    for (int j = n - 1; ~j; j--)
      hs = (hs * 37 + s[i][j] - 96) % mod;
    st.insert(hs);
  }
  for (int i = 0; i < n; i++) {
    int n = s[i].size();
    ll hs = 0;
    unordered_set<ll> vis;
    for (int j = 0; j < 26; j++)
      ap[0][j] = j + 'a' == s[i][0];
    for (int j = 1; j < n; j++) {
      memcpy(ap[j], ap[j - 1], sizeof ap[0]);
      ap[j][s[i][j] - 'a'] = 1;
    }
    for (int j = n - 1; ~j; j--) {
      if (j)
        for (int k = 0; k < 26; k++)
          if (ap[j - 1][k]) {
            ll nhs = (hs * 37 + k + 1) % mod;
            if (vis.count(nhs))
              continue;
            vis.insert(nhs);
            if (st.count(nhs))
              ans++;
          }
      if (!vis.count(hs)) {
        vis.insert(hs);
        if (st.count(hs))
          ans++;
      }
      hs = (hs * 37 + s[i][j] - 96) % mod;
    }
  }
  printf("%lld\\n", ans);
  return 0;
}
Problem p02589 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define MAXN 200005

using namespace std;
string info[MAXN];
bool comp(string a, string b) { return a.length() > b.length(); }
int child[1000010][26];
int cnt[1000010][26];
int main() {
#ifdef orange
  freopen("input.txt", "r", stdin);
#endif

  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> info[i];
  int set = 1;
  long long ans = 0;
  sort(info, info + n, comp);
  for (int i = 0; i < n; i++) {
    int curr = 0;
    for (int j = info[i].length() - 1; j >= 1; j--) {
      int next = info[i][j] - 'a';
      if (child[curr][next] == 0)
        child[curr][next] = set++;
      curr = child[curr][next];
    }
    ans += cnt[curr][info[i][0] - 'a'];
    int first[26];
    fill(first, first + 26, info[i].length());
    for (int z = 0; z < info[i].length(); z++) {
      char a = info[i][z];
      if (first[a - 'a'] == info[i].length())
        first[a - 'a'] = z;
    }
    curr = 0;
    for (int j = info[i].length() - 1; j >= 0; j--) {
      for (int z = 0; z < 26; z++) {
        if (first[z] <= j) {
          cnt[curr][z]++;
        }
      }
      int next = info[i][j] - 'a';
      if (child[curr][next] == 0)
        child[curr][next] = set++;
      curr = child[curr][next];
    }
  }
  cout << ans << "\\n";
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define MAXN 200005

using namespace std;

struct node {
  long long cnt;
  node *children[26];

public:
  node() {
    cnt = 0;
    for (int i = 0; i < 26; i++)
      children[i] = nullptr;
  }
};

string info[MAXN];

bool comp(string a, string b) { return a.length() > b.length(); }
int main() {
#ifdef orange
  freopen("input.txt", "r", stdin);
#endif
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> info[i];
  sort(info, info + n, comp);

  node *trie = new node;
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    node *curr = trie;
    for (int j = (int)info[i].length() - 1; j >= 0; j--) {
      int z = info[i][j] - 'a';
      if (curr->children[z] == nullptr)
        curr->children[z] = new node;
      curr = curr->children[z];
    }

    ans += curr->cnt;
    curr = trie;
    int first[26];
    fill(first, first + 26, info[i].length() + 1);
    for (int j = 0; j < (int)info[i].length(); j++) {
      if (first[info[i][j] - 'a'] == (int)info[i].length() + 1)
        first[info[i][j] - 'a'] = j;
    }

    for (int j = (int)info[i].length() - 1; j >= -1; j--) {
      for (int z = 0; z < 26; z++) {
        if (first[z] < j + 1) {
          node *branch = curr;
          if (branch->children[z] == nullptr)
            branch->children[z] = new node;
          branch = branch->children[z];
          branch->cnt++;
        }
      }
      if (j == -1)
        break;
      int next = info[i][j] - 'a';
      if (curr->children[next] == nullptr) {
        curr->children[next] = new node;
      }
      curr = curr->children[next];
    }
  }
  cout << ans << "\\n";
}