prithivMLmods/Coder-Stat huggingface dataset:
Problem p02588 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <utility>
#include <vector>
#define MAXN 200005
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
inline pi read() {
  ll x = 0;
  pi ans;
  int f = 0;
  char c = getchar();
  while (!isdigit(c) && c != '.')
    c = getchar();
  while (isdigit(c) || c == '.') {
    if (c == '.') {
      f = 1;
      c = getchar();
      continue;
    }
    if (f)
      --ans.first, --ans.second;
    x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
  }
  while (x % 2 == 0)
    x /= 2, ++ans.first;
  while (x % 5 == 0)
    x /= 5, ++ans.second;
  return ans;
}
pi a[MAXN];
int s[205][205];
const int N = 10;
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    a[i] = read();
  for (int i = 1; i <= n; i++)
    ++s[a[i].first + N][a[i].second + N];
  for (int i = 0; i <= 2 * N; i++)
    for (int j = 0; j <= 2 * N; j++) {
      if (i == 0 && j == 0)
        continue;
      if (i == 0)
        s[i][j] += s[i][j - 1];
      else if (j == 0)
        s[i][j] += s[i - 1][j];
      else
        s[i][j] += s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1];
    }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    int x = N - a[i].first, y = N - a[i].second;
    ans +=
        s[2 * N][2 * N] - s[2 * N][y - 1] - s[x - 1][2 * N] + s[x - 1][y - 1];
  }
  for (int i = 1; i <= n; i++)
    if (a[i].first >= 0 && a[i].second >= 0)
      --ans;
  ans >>= 1;
  cout << ans;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <utility>
#include <vector>
#define MAXN 200005
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
inline pi read() {
  ll x = 0;
  pi ans;
  int f = 0;
  char c = getchar();
  while (!isdigit(c) && c != '.')
    c = getchar();
  while (isdigit(c) || c == '.') {
    if (c == '.') {
      f = 1;
      c = getchar();
      continue;
    }
    if (f)
      --ans.first, --ans.second;
    x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
  }
  while (x % 2 == 0)
    x /= 2, ++ans.first;
  while (x % 5 == 0)
    x /= 5, ++ans.second;
  return ans;
}
pi a[MAXN];
int s[205][205];
const int N = 100;
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    a[i] = read();
  for (int i = 1; i <= n; i++)
    ++s[a[i].first + N][a[i].second + N];
  for (int i = 0; i <= 2 * N; i++)
    for (int j = 0; j <= 2 * N; j++) {
      if (i == 0 && j == 0)
        continue;
      if (i == 0)
        s[i][j] += s[i][j - 1];
      else if (j == 0)
        s[i][j] += s[i - 1][j];
      else
        s[i][j] += s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1];
    }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    int x = N - a[i].first, y = N - a[i].second;
    ans +=
        s[2 * N][2 * N] - s[2 * N][y - 1] - s[x - 1][2 * N] + s[x - 1][y - 1];
  }
  for (int i = 1; i <= n; i++)
    if (a[i].first >= 0 && a[i].second >= 0)
      --ans;
  ans >>= 1;
  cout << ans;
  return 0;
}
Problem p02588 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

pair<int, int> f(string &s) {
  int d = 0;
  ll keep = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '.') {
      d = s.size() - i - 1;
    } else {
      keep = keep * 10 + (s[i] - '0');
    }
  }

  int two = 0, five = 0;
  while (keep % 2 == 0) {
    two++;
    keep /= 2;
  }
  while (keep % 5 == 0) {
    five++;
    keep /= 5;
  }

  return make_pair(two - d, five - d);
}

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

  int n;
  cin >> n;

  vector<vector<ll>> x(21, vector<ll>(21));

  ll ans = 0;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    pair<int, int> p = f(s);
    // p.first += 10;
    // p.second += 10;
    for (int j = -p.first + 10; j < 21; j++) {
      for (int k = -p.second + 10; k < 21; k++) {
        ans += x[j][k];
      }
    }
    x[p.first + 10][p.second + 10]++;
  }

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

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

using ll = long long;

pair<int, int> f(string &s) {
  int d = 0;
  ll keep = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '.') {
      d = s.size() - i - 1;
    } else {
      keep = keep * 10 + (s[i] - '0');
    }
  }

  int two = 0, five = 0;
  while (keep % 2 == 0) {
    two++;
    keep /= 2;
  }
  while (keep % 5 == 0) {
    five++;
    keep /= 5;
  }

  two -= d;
  five -= d;
  if (two > 10)
    two = 10;
  if (five > 10)
    five = 10;

  return make_pair(two, five);
}

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

  int n;
  cin >> n;

  vector<vector<ll>> x(21, vector<ll>(21));

  ll ans = 0;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    pair<int, int> p = f(s);
    // p.first += 10;
    // p.second += 10;
    for (int j = -p.first + 10; j < 21; j++) {
      for (int k = -p.second + 10; k < 21; k++) {
        ans += x[j][k];
      }
    }
    x[p.first + 10][p.second + 10]++;
  }

  cout << ans << '\n';
}
Problem p02588 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<ll>;
using vv = vector<vi>;
// const int MOD = 1000000007;
const int MOD = 998244353;
const int INF = 1001001001;
const ll LINF = 1001001001001;

int main() {

  int n;
  cin >> n;
  vv faclist(19, vi(19, 0));

  rep(i, n) {
    string a;
    cin >> a;

    ll anum = 0;
    int dot = 0;

    rep(j, a.size()) {
      if (a[j] == '.') {
        dot = j + 1 - a.size();
      } else {
        anum *= 10;
        anum += a[j] - '0';
      }
    }
    int fac2 = dot, fac5 = dot;

    rep(j, 19) {
      if (anum % 2)
        break;
      else {
        anum /= 2;
        fac2++;
      }
    }
    rep(j, 19) {
      if (anum % 5)
        break;
      else {
        anum /= 5;
        fac5++;
      }
    }

    faclist[fac2 + 9][fac5 + 9]++;
  }

  vv acclist = faclist;

  rep(i, 19) {
    for (int j = 17; j >= 0; j--) {
      acclist[i][j] += acclist[i][j + 1];
    }
  }
  rep(j, 19) {
    for (int i = 17; i >= 0; i--) {
      acclist[i][j] += acclist[i + 1][j];
    }
  }

  ll ans = 0;

  rep(i, 19) rep(j, 19) {
    if (!faclist[i][j])
      continue;

    if (i > 8 && j > 8)
      ans += faclist[i][j] * (acclist[18 - i][18 - j] - 1);
    else
      ans += faclist[i][j] * acclist[18 - i][18 - j];
  }

  cout << ans / 2 << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<ll>;
using vv = vector<vi>;
// const int MOD = 1000000007;
const int MOD = 998244353;
const int INF = 1001001001;
const ll LINF = 1001001001001;

int main() {

  int n;
  cin >> n;
  vv faclist(19, vi(19, 0));

  rep(i, n) {
    string a;
    cin >> a;

    ll anum = 0;
    int dot = 0;

    rep(j, a.size()) {
      if (a[j] == '.') {
        dot = j + 1 - a.size();
      } else {
        anum *= 10;
        anum += a[j] - '0';
      }
    }
    int fac2 = dot, fac5 = dot;

    rep(j, 19) {
      if (anum % 2)
        break;
      else {
        anum /= 2;
        fac2++;
      }
    }
    rep(j, 19) {
      if (anum % 5)
        break;
      else {
        anum /= 5;
        fac5++;
      }
    }

    faclist[min(18, fac2 + 9)][min(18, fac5 + 9)]++;
  }

  vv acclist = faclist;

  rep(i, 19) {
    for (int j = 17; j >= 0; j--) {
      acclist[i][j] += acclist[i][j + 1];
    }
  }
  rep(j, 19) {
    for (int i = 17; i >= 0; i--) {
      acclist[i][j] += acclist[i + 1][j];
    }
  }

  ll ans = 0;

  rep(i, 19) rep(j, 19) {
    if (!faclist[i][j])
      continue;

    if (i > 8 && j > 8)
      ans += faclist[i][j] * (acclist[18 - i][18 - j] - 1);
    else
      ans += faclist[i][j] * acclist[18 - i][18 - j];
  }

  cout << ans / 2 << endl;
  return 0;
}
Problem p02588 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define pb push_back
#define pi pair<int, int>
#define l first
#define r second
#define all(x) x.begin(), x.end()
#define fori(a, b, step) for (int i = a; i < b; i += step)
#define forj(a, b, step) for (int j = a; j < b; j += step)
#define int long long

const int maxn = 1e5 + 1, mod = 1e9 + 7;

using namespace std;

int n;
int a[maxn];
int c2[maxn], c5[maxn];
int cnt[65][25];

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    string ca;
    cin >> ca;
    int dc = 9, bf = 0;
    for (int j = 0; j < ca.size(); j++) {
      if (ca[j] == '.') {
        bf = 1;
        continue;
      }
      a[i] = 10 * a[i] + ca[j] - '0';
      if (bf)
        --dc;
    }
    for (; dc; --dc, a[i] *= 10)
      ;
    for (int x = a[i]; x % 2 == 0; x /= 2, ++c2[i])
      ;
    for (int x = a[i]; x % 5 == 0; x /= 5, ++c5[i])
      ;
    ++cnt[c2[i]][c5[i]];
  }
  int ans = 0;
  for (int i = 0; i < 65; i++)
    for (int j = 0; j < 25; j++) {
      if (!cnt[i][j])
        continue;
      for (int l = max(0ll, 18 - i); l < 65; l++)
        for (int k = max(0ll, 18 - j); k < 25; k++) {
          if (!cnt[l][k])
            continue;
          if (i == l && j == k) {
            ans += cnt[i][j] * (cnt[i][j] - 1);
            continue;
          }
          ans += cnt[i][j] * cnt[l][k];
        }
    }
  cout << ans / 2;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define pb push_back
#define pi pair<int, int>
#define l first
#define r second
#define all(x) x.begin(), x.end()
#define fori(a, b, step) for (int i = a; i < b; i += step)
#define forj(a, b, step) for (int j = a; j < b; j += step)
#define int long long

const int maxn = 2e5 + 1, mod = 1e9 + 7;

using namespace std;

int n;
int a[maxn];
int c2[maxn], c5[maxn];
int cnt[65][25];

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    string ca;
    cin >> ca;
    int dc = 9, bf = 0;
    for (int j = 0; j < ca.size(); j++) {
      if (ca[j] == '.') {
        bf = 1;
        continue;
      }
      a[i] = 10 * a[i] + ca[j] - '0';
      if (bf)
        --dc;
    }
    for (; dc; --dc, a[i] *= 10)
      ;
    for (int x = a[i]; x % 2 == 0; x /= 2, ++c2[i])
      ;
    for (int x = a[i]; x % 5 == 0; x /= 5, ++c5[i])
      ;
    ++cnt[c2[i]][c5[i]];
  }
  int ans = 0;
  for (int i = 0; i < 65; i++)
    for (int j = 0; j < 25; j++) {
      if (!cnt[i][j])
        continue;
      for (int l = max(0ll, 18 - i); l < 65; l++)
        for (int k = max(0ll, 18 - j); k < 25; k++) {
          if (!cnt[l][k])
            continue;
          if (i == l && j == k) {
            ans += cnt[i][j] * (cnt[i][j] - 1);
            continue;
          }
          ans += cnt[i][j] * cnt[l][k];
        }
    }
  cout << ans / 2;
  return 0;
}
Problem p02588 (C++)

Buggy submission (status: Runtime Error):
#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 Pow(int b, int e) {
  if (e < 0)
    e += MOD - 1;
  int ret = 1;
  while (e) {
    if (e & 1)
      ret = 1LL * ret * b % MOD;
    b = 1LL * b * b % MOD;
    e >>= 1;
  }
  return ret;
}

int main() {
  int n;
  cin >> n;
  vector<string> a(n);
  vector<ll> b(n, 0);
  rep(i, n) cin >> a[i];
  rep(i, n) {
    ll u = 9;
    bool t = false;
    rep(j, a[i].size()) {
      if (t)
        u--;
      if (a[i][j] != '.') {
        b[i] *= 10;
        b[i] += a[i][j] - '0';
      } else
        t = true;
    }
    b[i] *= Pow(10, u);
  }
  vector<vector<ll>> c(20, vector<ll>(20, 0));
  ll ans = 0;
  rep(i, n) {
    int c2 = 0, c5 = 0;
    while (true) {
      if (b[i] % 2 == 0)
        c2++;
      else
        break;
      b[i] /= 2;
    }
    while (true) {
      if (b[i] % 5 == 0)
        c5++;
      else
        break;
      b[i] /= 5;
    }
    ans += c[18 - c2][18 - c5];
    rep(j, min(c2 + 1, 20)) {
      rep(k, min(c5 + 1, 20)) { c[j][k]++; }
    }
  }
  cout << ans << endl;
}


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 Pow(int b, int e) {
  if (e < 0)
    e += MOD - 1;
  int ret = 1;
  while (e) {
    if (e & 1)
      ret = 1LL * ret * b % MOD;
    b = 1LL * b * b % MOD;
    e >>= 1;
  }
  return ret;
}

int main() {
  int n;
  cin >> n;
  vector<string> a(n);
  vector<ll> b(n, 0);
  rep(i, n) cin >> a[i];
  rep(i, n) {
    ll u = 9;
    bool t = false;
    rep(j, a[i].size()) {
      if (t)
        u--;
      if (a[i][j] != '.') {
        b[i] *= 10;
        b[i] += a[i][j] - '0';
      } else
        t = true;
    }
    b[i] *= Pow(10, u);
  }
  vector<vector<ll>> c(20, vector<ll>(20, 0));
  ll ans = 0;
  rep(i, n) {
    int c2 = 0, c5 = 0;
    while (true) {
      if (b[i] % 2 == 0)
        c2++;
      else
        break;
      b[i] /= 2;
    }
    while (true) {
      if (b[i] % 5 == 0)
        c5++;
      else
        break;
      b[i] /= 5;
    }
    ans += c[max(0, 18 - c2)][max(0, 18 - c5)];
    rep(j, min(c2 + 1, 20)) {
      rep(k, min(c5 + 1, 20)) { c[j][k]++; }
    }
  }
  cout << ans << endl;
}
Problem p02588 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

int N, cnt[100][100];
char sir[100];
const int maxE = 9;
long long ans = 0;

int getExp(long long &N, int p) {
  int cnt = 0;
  while (N % p == 0)
    N /= p, cnt++;
  return cnt;
}

void add(int x, int y) {
  //    printf ("%d %d\n", x, y);
  for (int i = 2 * maxE - x; i <= 2 * maxE; i++)
    for (int j = 2 * maxE - y; j <= 2 * maxE; j++)
      ans += cnt[i][j];
  cnt[x][y]++;
}

int main() {
  // freopen ("input", "r", stdin);
  // freopen ("output", "w", stdout);

  scanf("%d\n", &N);
  for (int i = 1; i <= N; i++) {
    scanf("%s", sir + 1);
    int L = strlen(sir + 1), p = 1;
    long long curr = 0;
    while (p <= L && sir[p] != '.')
      curr = curr * 10LL + sir[p] - '0', p++;
    for (int i = p + 1; i <= L; i++)
      curr = curr * 10LL + sir[i] - '0';
    int after = (p > L ? 0 : L - p);
    assert(after <= maxE);
    int e2 = getExp(curr, 2), e5 = getExp(curr, 5);
    add(e2 + maxE - after, e5 + maxE - after);
  }
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int N, cnt[100][100];
char sir[100];
const int maxE = 9;
long long ans = 0;

int getExp(long long &N, int p) {
  int cnt = 0;
  while (N % p == 0)
    N /= p, cnt++;
  return cnt;
}

void add(int x, int y) {
  x = min(x, 2 * maxE);
  y = min(y, 2 * maxE);
  //    printf ("%d %d\n", x, y);
  for (int i = 2 * maxE - x; i <= 2 * maxE; i++)
    for (int j = 2 * maxE - y; j <= 2 * maxE; j++)
      ans += cnt[i][j];
  cnt[x][y]++;
}

int main() {
  // freopen ("input", "r", stdin);
  // freopen ("output", "w", stdout);

  scanf("%d\n", &N);
  for (int i = 1; i <= N; i++) {
    scanf("%s", sir + 1);
    int L = strlen(sir + 1), p = 1;
    long long curr = 0;
    while (p <= L && sir[p] != '.')
      curr = curr * 10LL + sir[p] - '0', p++;
    for (int i = p + 1; i <= L; i++)
      curr = curr * 10LL + sir[i] - '0';
    int after = (p > L ? 0 : L - p);
    assert(after <= maxE);
    int e2 = getExp(curr, 2), e5 = getExp(curr, 5);
    add(e2 + maxE - after, e5 + maxE - after);
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p02588 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define M_PI 3.14159265358979323846 // pi

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> VI;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> t3;
typedef tuple<ll, ll, ll, ll> t4;
typedef tuple<ll, ll, ll, ll, ll> t5;

#define rep(a, n) for (ll a = 0; a < n; a++)
#define repi(a, b, n) for (ll a = b; a < n; a++)

using namespace std;

static const ll INF = 1e15;

int main() {
  int n;
  cin >> n;
  vector<vector<ll>> grid(20, vector<ll>(20, 0));
  vector<P> ss(n);
  rep(i, n) {
    double item;
    cin >> item;
    double t = item * 1e9 + 0.1;
    ll v = (ll)t;
    int a = 0;
    while (v % 2 == 0) {
      a++;
      v /= 2;
    }
    int b = 0;
    while (v % 5 == 0) {
      b++;
      v /= 5;
    }
    a = min(20, a);
    b = min(20, b);
    grid[a][b]++;
    ss[i] = make_pair(a, b);
  }

  ll sum = 0;
  rep(i, n) {
    auto f = ss[i].first;
    auto s = ss[i].second;
    grid[f][s]--;
    for (auto f2 = max(0LL, 18 - f); f2 < 20; f2++) {
      for (auto s2 = max(0LL, 18 - s); s2 < 20; s2++) {
        sum += grid[f2][s2];
      }
    }
  }

  cout << sum << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define M_PI 3.14159265358979323846 // pi

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> VI;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> t3;
typedef tuple<ll, ll, ll, ll> t4;
typedef tuple<ll, ll, ll, ll, ll> t5;

#define rep(a, n) for (ll a = 0; a < n; a++)
#define repi(a, b, n) for (ll a = b; a < n; a++)

using namespace std;

static const ll INF = 1e15;

int main() {
  int n;
  cin >> n;
  vector<vector<ll>> grid(20, vector<ll>(20, 0));
  vector<P> ss(n);
  rep(i, n) {
    double item;
    cin >> item;
    double t = item * 1e9 + 0.1;
    ll v = (ll)t;
    int a = 0;
    while (v % 2 == 0) {
      a++;
      v /= 2;
    }
    int b = 0;
    while (v % 5 == 0) {
      b++;
      v /= 5;
    }
    a = min(19, a);
    b = min(19, b);
    grid[a][b]++;
    ss[i] = make_pair(a, b);
  }

  ll sum = 0;
  rep(i, n) {
    auto f = ss[i].first;
    auto s = ss[i].second;
    grid[f][s]--;
    for (auto f2 = max(0LL, 18 - f); f2 < 20; f2++) {
      for (auto s2 = max(0LL, 18 - s); s2 < 20; s2++) {
        sum += grid[f2][s2];
      }
    }
  }

  cout << sum << endl;
  return 0;
}
Problem p02589 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
int n;
int trieroot;
int trien;
int triep[1001000];
int trier[1001000][26];
int triec[1001000];
int triecnt[1001000];
int triercnt[1001000][26];
char s[1001000];
int pd[100100];
int newtn(int prt, int c) {
  triep[trien] = prt;
  triec[trien] = c;
  trien++;
  return trien - 1;
}
int main() {
  scanf("%d", &n);
  trien = 1;
  trieroot = newtn(0, 0);
  for (int i = 0; i < n; i++) {
    scanf("%s", s);
    int x = trieroot;
    int len = 0;
    for (len = 0; s[len]; len++)
      ;
    for (int j = len - 1; j >= 0; j--) {
      int d = s[j] - 'a';
      if (trier[x][d] == 0) {
        trier[x][d] = newtn(x, d);
      }
      x = trier[x][d];
    }
    triecnt[x]++;
    pd[i] = x;
  }
  for (int i = trien - 1; i >= 1; i--) {
    triecnt[triep[i]] += triecnt[i];
    for (int j = 0; j < 26; j++) {
      if (j == triec[i]) {
        triercnt[triep[i]][j] += triecnt[i];
      } else {
        triercnt[triep[i]][j] += triercnt[i][j];
      }
    }
  }
  long long int ans = 0;
  for (int i = 0; i < n; i++) {
    int x = pd[i];
    int y = triep[x];
    int d = triec[x];
    ans += triercnt[y][d] - 1;
  }
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <cstdio>
int n;
int trieroot;
int trien;
int triep[1001000];
int trier[1001000][26];
int triec[1001000];
int triecnt[1001000];
int triercnt[1001000][26];
char s[1001000];
int pd[200100];
int newtn(int prt, int c) {
  triep[trien] = prt;
  triec[trien] = c;
  trien++;
  return trien - 1;
}
int main() {
  scanf("%d", &n);
  trien = 1;
  trieroot = newtn(0, 0);
  for (int i = 0; i < n; i++) {
    scanf("%s", s);
    int x = trieroot;
    int len = 0;
    for (len = 0; s[len]; len++)
      ;
    for (int j = len - 1; j >= 0; j--) {
      int d = s[j] - 'a';
      if (trier[x][d] == 0) {
        trier[x][d] = newtn(x, d);
      }
      x = trier[x][d];
    }
    triecnt[x]++;
    pd[i] = x;
  }
  for (int i = trien - 1; i >= 1; i--) {
    triecnt[triep[i]] += triecnt[i];
    for (int j = 0; j < 26; j++) {
      if (j == triec[i]) {
        triercnt[triep[i]][j] += triecnt[i];
      } else {
        triercnt[triep[i]][j] += triercnt[i][j];
      }
    }
  }
  long long int ans = 0;
  for (int i = 0; i < n; i++) {
    int x = pd[i];
    int y = triep[x];
    int d = triec[x];
    ans += triercnt[y][d] - 1;
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p02589 (C++)

Buggy submission (status: 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[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);
}

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[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);
}
Problem p02589 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define LL long long
const int N = 2e5 + 5;

int n;
vector<string> v;
vector<int> c[N][26];
LL ans;

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    reverse(s.begin(), s.end());
    v.pb(s);
  }
  sort(v.begin(), v.end());

  for (int i = 0; i < n; i++)
    for (int j = 0; j < 26; j++) {
      c[i][j].resize(v[i].size());
      for (int k = v[i].size() - 1; k >= 0; k--)
        if (k != v[i].size() - 1 && c[i][j][k + 1] || v[i][k] == j + 'a')
          c[i][j][k] = 1;
        else
          c[i][j][k] = 0;
    }
  for (int i = 1; i < n; i++)
    for (int j = 0; j < 26; j++)
      for (int k = 0; k < c[i][j].size(); k++)
        if (k < c[i - 1][j].size())
          c[i][j][k] += c[i - 1][j][k];

  for (int i = 0; i < n; i++) {
    string s = v[i].substr(0, v[i].size() - 1);
    int u1 = upper_bound(v.begin(), v.end(), s) - v.begin();
    if (s.size())
      s[s.size() - 1]++;
    else
      s = "|位巨佬！";
    int u2 = lower_bound(v.begin(), v.end(), s) - v.begin();
    int last = v[i][v[i].size() - 1] - 'a';
    ans += c[u2 - 1][last][v[i].size() - 1];
    if (v[i].size() <= c[u1 - 1][last].size())
      ans -= c[u1 - 1][last][v[i].size() - 1];
  }
  cout << ans - n << '\n';

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define LL long long
const int N = 2e5 + 5;

int n;
vector<string> v;
vector<int> c[N][26];
LL ans;

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    reverse(s.begin(), s.end());
    v.pb(s);
  }
  sort(v.begin(), v.end());

  for (int i = 0; i < n; i++)
    for (int j = 0; j < 26; j++) {
      c[i][j].resize(v[i].size());
      for (int k = v[i].size() - 1; k >= 0; k--)
        if (k != v[i].size() - 1 && c[i][j][k + 1] || v[i][k] == j + 'a')
          c[i][j][k] = 1;
        else
          c[i][j][k] = 0;
    }
  for (int i = 1; i < n; i++)
    for (int j = 0; j < 26; j++)
      for (int k = 0; k < c[i][j].size(); k++)
        if (k < c[i - 1][j].size())
          c[i][j][k] += c[i - 1][j][k];

  for (int i = 0; i < n; i++) {
    string s = v[i].substr(0, v[i].size() - 1);
    int u1 = upper_bound(v.begin(), v.end(), s) - v.begin();
    if (s.size())
      s[s.size() - 1]++;
    else
      s = "|位巨佬！";
    int u2 = lower_bound(v.begin(), v.end(), s) - v.begin();
    int last = v[i][v[i].size() - 1] - 'a';
    ans += c[u2 - 1][last][v[i].size() - 1];
    if (u1 && v[i].size() <= c[u1 - 1][last].size())
      ans -= c[u1 - 1][last][v[i].size() - 1];
  }
  cout << ans - n << '\n';

  return 0;
}
Problem p02589 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define MAXN 200005
#define INF 1000000000
#define MOD 1000000007
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
string str[MAXN];
int tot = 1, n;
int trie[MAXN][26], cnt[MAXN][26];
bool ed[MAXN];
bool has[26];
void insert(string s, int rt) {
  vector<int> nodes;
  nodes.clear();
  for (int i = 0; i < (int)s.size(); i++) {
    int x = s[i] - 'a';
    if (trie[rt][x] == 0)
      trie[rt][x] = ++tot;
    nodes.push_back(rt);
    rt = trie[rt][x];
  }
  memset(has, false, sizeof(has));
  for (int i = (int)s.size() - 1; i >= 0; i--) {
    has[s[i] - 'a'] = true;
    int cur = nodes[i];
    for (int j = 0; j < 26; j++)
      if (has[j])
        cnt[cur][j]++;
  }
}
int get_ans(string s, int rt) {
  for (int i = 0; i < (int)s.size() - 1; i++) {
    int x = s[i] - 'a';
    if (trie[rt][x] == 0)
      return false;
    rt = trie[rt][x];
  }
  int x = s[s.size() - 1] - 'a';
  return cnt[rt][x];
}
map<string, int> mp;
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    cin >> str[i];
    reverse(str[i].begin(), str[i].end());
    insert(str[i], 1);
  }
  ll ans = 0;
  for (int i = 0; i < n; i++)
    ans += get_ans(str[i], 1) - 1;
  for (int i = 0; i < n; i++)
    mp[str[i]]++;
  for (auto p : mp)
    ans -= 1LL * p.S * (p.S - 1) / 2;
  printf("%lld\n", ans);
}

Fixed submission:
#include <bits/stdc++.h>
#define MAXN 2000005
#define INF 1000000000
#define MOD 1000000007
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
string str[MAXN];
int tot = 1, n;
int trie[MAXN][26], cnt[MAXN][26];
bool ed[MAXN];
bool has[26];
void insert(string s, int rt) {
  vector<int> nodes;
  nodes.clear();
  for (int i = 0; i < (int)s.size(); i++) {
    int x = s[i] - 'a';
    if (trie[rt][x] == 0)
      trie[rt][x] = ++tot;
    nodes.push_back(rt);
    rt = trie[rt][x];
  }
  memset(has, false, sizeof(has));
  for (int i = (int)s.size() - 1; i >= 0; i--) {
    has[s[i] - 'a'] = true;
    int cur = nodes[i];
    for (int j = 0; j < 26; j++)
      if (has[j])
        cnt[cur][j]++;
  }
}
int get_ans(string s, int rt) {
  for (int i = 0; i < (int)s.size() - 1; i++) {
    int x = s[i] - 'a';
    if (trie[rt][x] == 0)
      return false;
    rt = trie[rt][x];
  }
  int x = s[s.size() - 1] - 'a';
  return cnt[rt][x];
}
map<string, int> mp;
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    cin >> str[i];
    reverse(str[i].begin(), str[i].end());
    insert(str[i], 1);
  }
  ll ans = 0;
  for (int i = 0; i < n; i++)
    ans += get_ans(str[i], 1) - 1;
  for (int i = 0; i < n; i++)
    mp[str[i]]++;
  for (auto p : mp)
    ans -= 1LL * p.S * (p.S - 1) / 2;
  printf("%lld\n", ans);
}
Problem p02589 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
const int N = 2e5 + 5;
int trie[N][60];
string s[N];
int sz;
int zero = 30;
void add(int idx) {
  int root = 1;
  int z = s[idx].size();
  int cnt[33] = {};
  for (int i = 0; i < z; i++) {
    cnt[s[idx][i] - 'a']++;
  }
  for (int i = 0; i < 26; i++) {
    if (cnt[i] > 0)
      trie[root][i + zero]++;
  }
  for (int i = 0; i < z; i++) {
    if (trie[root][s[idx][i] - 'a'] == 0) {
      trie[root][s[idx][i] - 'a'] = ++sz;
    }
    root = trie[root][s[idx][i] - 'a'];
    cnt[s[idx][i] - 'a']--;
    for (int j = 0; j < 26; j++) {
      if (cnt[j] > 0)
        trie[root][j + zero]++;
    }
  }
}
ll ans = 0;
void get(int idx) {
  int root = 1;
  int z = s[idx].size();
  for (int i = 0; i < z - 1; i++) {
    root = trie[root][s[idx][i] - 'a'];
  }
  int x = s[idx][z - 1] - 'a';
  ans += trie[root][x + zero];
}
void solve() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    reverse(s[i].begin(), s[i].end());
  }
  sz = 1;
  for (int i = 1; i <= n; i++) {
    add(i);
  }
  for (int i = 1; i <= n; i++) {
    get(i);
  }
  cout << ans - n << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  //    cin>>t;
  while (t--)
    solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
const int N = 2e6 + 5;
int trie[N][60];
string s[N];
int sz;
int zero = 30;
void add(int idx) {
  int root = 1;
  int z = s[idx].size();
  int cnt[33] = {};
  for (int i = 0; i < z; i++) {
    cnt[s[idx][i] - 'a']++;
  }
  for (int i = 0; i < 26; i++) {
    if (cnt[i] > 0)
      trie[root][i + zero]++;
  }
  for (int i = 0; i < z; i++) {
    if (trie[root][s[idx][i] - 'a'] == 0) {
      trie[root][s[idx][i] - 'a'] = ++sz;
    }
    root = trie[root][s[idx][i] - 'a'];
    cnt[s[idx][i] - 'a']--;
    for (int j = 0; j < 26; j++) {
      if (cnt[j] > 0)
        trie[root][j + zero]++;
    }
  }
}
ll ans = 0;
void get(int idx) {
  int root = 1;
  int z = s[idx].size();
  for (int i = 0; i < z - 1; i++) {
    root = trie[root][s[idx][i] - 'a'];
  }
  int x = s[idx][z - 1] - 'a';
  ans += trie[root][x + zero];
}
void solve() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    reverse(s[i].begin(), s[i].end());
  }
  sz = 1;
  for (int i = 1; i <= n; i++) {
    add(i);
  }
  for (int i = 1; i <= n; i++) {
    get(i);
  }
  cout << ans - n << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  //    cin>>t;
  while (t--)
    solve();
  return 0;
}
Problem p02589 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int ALPHABET_SIZE = 26;
int glob_ans = 0;

int trie[1000001][26];
std::vector<int> v[200000];
int term[200000] = {0};
int nxt = 1;

void insert(string key, int k) {
  int i, lvl = 0;

  for (i = 0; i < key.size(); i++) {
    v[k].push_back(lvl);
    if (trie[lvl][key[i] - 'a'] == 0) {
      trie[lvl][key[i] - 'a'] = nxt++;
    }
    lvl = trie[lvl][key[i] - 'a'];
  }
  term[lvl]++;
}

int main() {
  int i, j, n;
  cin >> n;
  string arr[n];
  for (i = 0; i < n; i++)
    cin >> arr[i];

  for (i = 0; i < n; i++) {
    reverse(arr[i].begin(), arr[i].end());
    insert(arr[i], i);
  }
  int ans = 0;
  for (i = 0; i < n; i++) {
    set<int> st;
    for (int j = (int)arr[i].size() - 1; j >= 0; j--) {
      int node = v[i][j];
      st.insert(arr[i][j] - 'a');
      for (int c = 0; c < ALPHABET_SIZE; c++)
        if (st.find(c) != st.end() && term[trie[node][c]])
          ans++;
    }
  }
  cout << ans - n << endl;
}


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

const int ALPHABET_SIZE = 26;
int glob_ans = 0;

int trie[1000001][26];
std::vector<int> v[200000];
int term[1000001] = {0};
int nxt = 1;

void insert(string key, int k) {
  int i, lvl = 0;

  for (i = 0; i < key.size(); i++) {
    v[k].push_back(lvl);
    if (trie[lvl][key[i] - 'a'] == 0) {
      trie[lvl][key[i] - 'a'] = nxt++;
    }
    lvl = trie[lvl][key[i] - 'a'];
  }
  term[lvl]++;
}

int main() {
  int i, j, n;
  cin >> n;
  string arr[n];
  for (i = 0; i < n; i++)
    cin >> arr[i];

  for (i = 0; i < n; i++) {
    reverse(arr[i].begin(), arr[i].end());
    insert(arr[i], i);
  }
  int ans = 0;
  for (i = 0; i < n; i++) {
    set<int> st;
    for (int j = (int)arr[i].size() - 1; j >= 0; j--) {
      int node = v[i][j];
      st.insert(arr[i][j] - 'a');
      for (int c = 0; c < ALPHABET_SIZE; c++)
        if (st.find(c) != st.end() && term[trie[node][c]])
          ans++;
    }
  }
  cout << ans - n << endl;
}
Problem p02589 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

using namespace std;

typedef long long ll;

int n;
string s[202020];
int l[202020];
int b[202020];

int tr[1222333][26];
int tc;
int th[1222333][26];

void add(int i, int k) {
  int u = 0;
  for (int j = k; j >= 1; j--) {
    for (int h = 0; h < 26; h++) {
      if (b[j] & (1 << h))
        th[u][h]++;
    }
    int c = s[i][j] - 'a';
    if (tr[u][c] == 0)
      tr[u][c] = ++tc;
    u = tr[u][c];
  }
}

int calc(int i, int k) {
  int u = 0;
  for (int j = k; j >= 2; j--) {
    int c = s[i][j] - 'a';
    u = tr[u][c];
  }
  return th[u][s[i][1] - 'a'];
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    l[i] = s[i].size();
    s[i] = "#" + s[i];
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= l[i]; j++) {
      b[j] = b[j - 1] | (1 << (s[i][j] - 'a'));
    }
    add(i, l[i]);
  }
  ll c = 0;
  for (int i = 1; i <= n; i++) {
    // cout << i << " " << s[i] << " " << calc(i,l[i]) << "\n";
    c += calc(i, l[i]) - 1;
  }
  cout << c << "\n";
}


Fixed submission:
#include <iostream>

using namespace std;

typedef long long ll;

int n;
string s[202020];
int l[202020];
int b[1222333];

int tr[1222333][26];
int tc;
int th[1222333][26];

void add(int i, int k) {
  int u = 0;
  for (int j = k; j >= 1; j--) {
    for (int h = 0; h < 26; h++) {
      if (b[j] & (1 << h))
        th[u][h]++;
    }
    int c = s[i][j] - 'a';
    if (tr[u][c] == 0)
      tr[u][c] = ++tc;
    u = tr[u][c];
  }
}

int calc(int i, int k) {
  int u = 0;
  for (int j = k; j >= 2; j--) {
    int c = s[i][j] - 'a';
    u = tr[u][c];
  }
  return th[u][s[i][1] - 'a'];
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    l[i] = s[i].size();
    s[i] = "#" + s[i];
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= l[i]; j++) {
      b[j] = b[j - 1] | (1 << (s[i][j] - 'a'));
    }
    add(i, l[i]);
  }
  ll c = 0;
  for (int i = 1; i <= n; i++) {
    // cout << i << " " << s[i] << " " << calc(i,l[i]) << "\n";
    c += calc(i, l[i]) - 1;
  }
  cout << c << "\n";
}
Problem p02589 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>

int ch[1000010][26], pr[1000010];
int ct[1000010], ex[100010][26];
bool fn[100010];
int N;

void add(int i, char *s) {
  // printf("%d %c\n",i,*s);
  if (*s == 0) {
    fn[i] = true;
  } else if (ch[i][(*s) - 'a'] == -1) {
    ch[i][(*s) - 'a'] = N;
    for (int x = 0; x < 26; x++) {
      ch[N][x] = -1;
    }
    fn[N] = false;
    N++;
    add(N - 1, s + 1);
  } else {
    add(ch[i][(*s) - 'a'], s + 1);
  }
}

long long ans;

void count(int i) {
  ct[i] = (fn[i] ? 1 : 0);
  for (int y = 0; y < 26; y++) {
    ex[i][y] = 0;
  }
  for (int x = 0; x < 26; x++) {
    int j = ch[i][x];
    if (j == -1)
      continue;
    count(j);
    ct[i] += ct[j];
    for (int y = 0; y < 26; y++) {
      if (y == x) {
        ex[i][y] += ct[j];
      } else {
        ex[i][y] += ex[j][y];
      }
    }
  }
  for (int x = 0; x < 26; x++) {
    int j = ch[i][x];
    if (j != -1 && fn[j]) {
      ans += ex[i][x] - 1;
    }
  }
}

int main() {
  int n;
  scanf("%d", &n);
  for (int x = 0; x < 26; x++) {
    ch[0][x] = -1;
  }
  fn[0] = false;
  N = 1;
  for (int i = 0; i < n; i++) {
    static char t[1000010], s[1000010];
    scanf("%s", t);
    int l = strlen(t);
    for (int j = 0; j < l; j++) {
      s[l - j - 1] = t[j];
    }
    s[l] = 0;
    add(0, s);
  }
  ans = 0ll;
  count(0);
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <cstdio>
#include <cstring>

int ch[1000010][26], pr[1000010];
int ct[1000010], ex[1000010][26];
bool fn[1000010];
int N;

void add(int i, char *s) {
  // printf("%d %c\n",i,*s);
  if (*s == 0) {
    fn[i] = true;
  } else if (ch[i][(*s) - 'a'] == -1) {
    ch[i][(*s) - 'a'] = N;
    for (int x = 0; x < 26; x++) {
      ch[N][x] = -1;
    }
    fn[N] = false;
    N++;
    add(N - 1, s + 1);
  } else {
    add(ch[i][(*s) - 'a'], s + 1);
  }
}

long long ans;

void count(int i) {
  ct[i] = (fn[i] ? 1 : 0);
  for (int y = 0; y < 26; y++) {
    ex[i][y] = 0;
  }
  for (int x = 0; x < 26; x++) {
    int j = ch[i][x];
    if (j == -1)
      continue;
    count(j);
    ct[i] += ct[j];
    for (int y = 0; y < 26; y++) {
      if (y == x) {
        ex[i][y] += ct[j];
      } else {
        ex[i][y] += ex[j][y];
      }
    }
  }
  for (int x = 0; x < 26; x++) {
    int j = ch[i][x];
    if (j != -1 && fn[j]) {
      ans += ex[i][x] - 1;
    }
  }
}

int main() {
  int n;
  scanf("%d", &n);
  for (int x = 0; x < 26; x++) {
    ch[0][x] = -1;
  }
  fn[0] = false;
  N = 1;
  for (int i = 0; i < n; i++) {
    static char t[1000010], s[1000010];
    scanf("%s", t);
    int l = strlen(t);
    for (int j = 0; j < l; j++) {
      s[l - j - 1] = t[j];
    }
    s[l] = 0;
    add(0, s);
  }
  ans = 0ll;
  count(0);
  printf("%lld\n", ans);
  return 0;
}
Problem p02589 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
#define idfc                                                                   \
  ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
//:/
#define pb push_back
#define mp make_pair
#define nt _ll128
#define ld double
long double PI = 3.14159265;
using ll = long long;

const ll modo = 1e9 + 7;
const ll ms = 2e5 + 5;
const int inf = 1e9 + 9;

int trie[ms][26] = {0};
int ct[ms][26] = {0};
int tim = 1;
int ar[26] = {0};
string br[ms];

int query(string s) {
  int n = s.length();
  int i = 0;
  int v = 0;
  n--;
  int p = 1;
  while (i < n) {
    v = trie[v][s[i] - 97];
    if (!v) {
      return 0;
    }
    i++;
  }
  return ct[v][s[i] - 97];
  cout << s << " " << p << "\n";

  return p;
}

void add(string s) {
  int i = 0;
  int v = 0;
  int n;
  n = s.length();
  for (char e : s)
    ar[e - 97]++;
  i = 0;
  v = 0;
  int j;
  while (i < n) {
    for (j = 0; j < 26; j++) {
      if (ar[j])
        ct[v][j]++;
    }
    if (!trie[v][s[i] - 97]) {
      trie[v][s[i] - 97] = tim;
      tim++;
    }
    v = trie[v][s[i] - 97];
    ar[s[i] - 97]--;
    i++;
  }
}

int main() {
  idfc;

  int n;
  cin >> n;
  int i;
  ll ans = 0;
  string s;
  for (i = 1; i <= n; i++) {
    cin >> s;
    reverse(s.begin(), s.end());
    br[i] = s;
    add(s);
  }
  for (i = 1; i <= n; i++) {
    ans += (query(br[i]) - 1);
  }
  cout << ans;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
#define idfc                                                                   \
  ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
//:/
#define pb push_back
#define mp make_pair
#define nt _ll128
#define ld double
long double PI = 3.14159265;
using ll = long long;

const ll modo = 1e9 + 7;
const ll ms = 1e6 + 5;
const int inf = 1e9 + 9;

int trie[ms][26] = {0};
int ct[ms][26] = {0};
int tim = 1;
int ar[26] = {0};
string br[ms];

int query(string s) {
  int n = s.length();
  int i = 0;
  int v = 0;
  n--;
  int p = 1;
  while (i < n) {
    v = trie[v][s[i] - 97];
    if (!v) {
      return 0;
    }
    i++;
  }
  return ct[v][s[i] - 97];
  cout << s << " " << p << "\n";

  return p;
}

void add(string s) {
  int i = 0;
  int v = 0;
  int n;
  n = s.length();
  for (char e : s)
    ar[e - 97]++;
  i = 0;
  v = 0;
  int j;
  while (i < n) {
    for (j = 0; j < 26; j++) {
      if (ar[j])
        ct[v][j]++;
    }
    if (!trie[v][s[i] - 97]) {
      trie[v][s[i] - 97] = tim;
      tim++;
    }
    v = trie[v][s[i] - 97];
    ar[s[i] - 97]--;
    i++;
  }
}

int main() {
  idfc;

  int n;
  cin >> n;
  int i;
  ll ans = 0;
  string s;
  for (i = 1; i <= n; i++) {
    cin >> s;
    reverse(s.begin(), s.end());
    br[i] = s;
    add(s);
  }
  for (i = 1; i <= n; i++) {
    ans += (query(br[i]) - 1);
  }
  cout << ans;

  return 0;
}
Problem p02589 (C++)

Buggy submission (status: Runtime Error):
/*
  In the name of ALLAH
  Author : Raashid Anwar
*/

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

#define int int64_t
const int M1 = 998244353;
const int M2 = 1000000007;

int trie[200000][26];
int cnt[2000000][26];
int tot;

void insert(string s) {
  reverse(s.begin(), s.end());
  int n = s.size();
  int a[26] = {};
  for (int i = 0; i < n; i++)
    a[s[i] - 'a']++;
  for (int j = 0; j < 26; j++)
    cnt[0][j] += (a[j] > 0);
  int u = 0;
  for (int i = 0; i < n; i++) {
    a[s[i] - 'a']--;
    if (!trie[u][s[i] - 'a'])
      trie[u][s[i] - 'a'] = tot++;
    u = trie[u][s[i] - 'a'];
    for (int j = 0; j < 26; j++)
      cnt[u][j] += (a[j] > 0);
  }
}

int get(string s) {
  reverse(s.begin(), s.end());
  int n = s.size();
  int u = 0;
  for (int i = 0; i < n - 1; i++) {
    if (!trie[u][s[i] - 'a'])
      return 1;
    u = trie[u][s[i] - 'a'];
  }
  return cnt[u][s[n - 1] - 'a'];
}

void solve() {
  int n;
  cin >> n;
  tot = 1;
  string s[n];
  int ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    insert(s[i]);
  }
  for (int i = 0; i < n; i++)
    ans += get(s[i]) - 1;
  cout << ans << "\n";
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  solve();
}

Fixed submission:
/*
  In the name of ALLAH
  Author : Raashid Anwar
*/

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

#define int int64_t
const int M1 = 998244353;
const int M2 = 1000000007;

int trie[2000000][26];
int cnt[2000000][26];
int tot;

void insert(string s) {
  reverse(s.begin(), s.end());
  int n = s.size();
  int a[26] = {};
  for (int i = 0; i < n; i++)
    a[s[i] - 'a']++;
  for (int j = 0; j < 26; j++)
    cnt[0][j] += (a[j] > 0);
  int u = 0;
  for (int i = 0; i < n; i++) {
    a[s[i] - 'a']--;
    if (!trie[u][s[i] - 'a'])
      trie[u][s[i] - 'a'] = tot++;
    u = trie[u][s[i] - 'a'];
    for (int j = 0; j < 26; j++)
      cnt[u][j] += (a[j] > 0);
  }
}

int get(string s) {
  reverse(s.begin(), s.end());
  int n = s.size();
  int u = 0;
  for (int i = 0; i < n - 1; i++) {
    if (!trie[u][s[i] - 'a'])
      return 1;
    u = trie[u][s[i] - 'a'];
  }
  return cnt[u][s[n - 1] - 'a'];
}

void solve() {
  int n;
  cin >> n;
  tot = 1;
  string s[n];
  int ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    insert(s[i]);
  }
  for (int i = 0; i < n; i++)
    ans += get(s[i]) - 1;
  cout << ans << "\n";
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  solve();
}
Problem p02589 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;
using vi = vector<ll>;
using vvi = vector<vi>;

#define all(a) a.begin(), a.end()
#define pb push_back

void solve() {
  int n;
  cin >> n;
  vector<pair<string, char>> vec(n);
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    reverse(s.begin() + 1, s.end());
    vec[i].second = s[0];
    vec[i].first = s.substr(1, s.size() - 1);
  }
  sort(all(vec));
  ll M = 999999937;
  ll k = 45;
  vvi hash(n);
  for (int i = 0; i < n; i++) {
    hash[i] = vi(vec[i].first.size() + 1);
    for (int j = 1; j < hash[i].size(); j++) {
      hash[i][j] = hash[i][j - 1] * k + vec[i].first[j - 1];
      hash[i][j] %= M;
    }
  }

  vvi cnt(n, vi(26));
  for (int i = 0; i < n; i++) {
    for (auto c : vec[i].first) {
      cnt[i][c - 'a']++;
    }
  }

  ll res = 0;

  vi st;
  vector<pair<char, int>> query;
  for (int i = 0; i < n; i++) {
    while (!st.empty()) {
      int v = st.back();
      if (hash[v].back() == hash[i][vec[v].first.size()]) {
        break;
      } else {
        st.pop_back();
        query.pop_back();
      }
    }
    for (auto q : query) {
      if (cnt[i][q.first - 'a'] + (q.first == vec[i].second) >= q.second)
        res++;
    }
    st.push_back(i);
    query.push_back({vec[i].second, cnt[i][vec[i].second - 'a'] + 1});
  }
  cout << res << endl;
}

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

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;
using vi = vector<ll>;
using vvi = vector<vi>;

#define all(a) a.begin(), a.end()
#define pb push_back

void solve() {
  int n;
  cin >> n;
  vector<pair<string, char>> vec(n);
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    reverse(s.begin() + 1, s.end());
    vec[i].second = s[0];
    vec[i].first = s.substr(1, s.size() - 1);
  }
  sort(all(vec));
  ll M = 999999937;
  ll k = 45;
  vvi hash(n);
  for (int i = 0; i < n; i++) {
    hash[i] = vi(vec[i].first.size() + 1);
    for (int j = 1; j < hash[i].size(); j++) {
      hash[i][j] = hash[i][j - 1] * k + vec[i].first[j - 1];
      hash[i][j] %= M;
    }
  }

  vvi cnt(n, vi(26));
  for (int i = 0; i < n; i++) {
    for (auto c : vec[i].first) {
      cnt[i][c - 'a']++;
    }
  }

  ll res = 0;

  vi st;
  vector<pair<char, int>> query;
  for (int i = 0; i < n; i++) {
    while (!st.empty()) {
      int v = st.back();
      if (!(vec[i].first.size() < vec[v].first.size()) &&
          hash[v].back() == hash[i][vec[v].first.size()]) {
        break;
      } else {
        st.pop_back();
        query.pop_back();
      }
    }
    for (auto q : query) {
      if (cnt[i][q.first - 'a'] + (q.first == vec[i].second) >= q.second)
        res++;
    }
    st.push_back(i);
    query.push_back({vec[i].second, cnt[i][vec[i].second - 'a'] + 1});
  }
  cout << res << endl;
}

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