alexjercan/bugnet huggingface dataset:
Problem p02589 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define rrep(i, a, b) for (int i = (a); i >= (b); --i)
#define PB push_back
#define ar2 array<int, 2>
typedef long long LL;
const LL P = 998244353;
const int N = 2e5 + 5;
mt19937 rng(time(0));

int n, last[N][26];
string s[N];
LL ans = 0;

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

  cin >> n;
  rep(i, 1, n) {
    cin >> s[i];
    int sz = s[i].size();
    rep(j, 0, sz / 2 - 1) swap(s[i][j], s[i][sz - 1 - j]);
  }
  sort(s + 1, s + n + 1);

  memset(last, 0, sizeof last);
  rep(i, 1, n) {
    int sz = s[i].size();
    rep(j, 0, sz - 1) last[i][s[i][j] - 'a'] = j + 1;
  }

  rep(i, 1, n) {
    int l = 1, r = n, sz = s[i].size();
    rep(j, 0, sz - 2) {
      int l1 = l, r1 = i;
      l = i;
      while (l1 <= r1) {
        int mid = (l1 + r1) >> 1;
        if ((int)s[mid].size() >= sz && s[mid][j] == s[i][j])
          r1 = mid - 1, l = mid;
        else
          l1 = mid + 1;
      }
      l1 = i, r1 = r;
      r = i;
      while (l1 <= r1) {
        int mid = (l1 + r1) >> 1;
        if ((int)s[mid].size() >= sz && s[mid][j] == s[i][j])
          l1 = mid + 1, r = mid;
        else
          r1 = mid - 1;
      }
    }

    char ch = s[i][sz - 1];
    rep(j, l, r) if (last[j][ch - 'a'] >= sz) {
      // cerr<<s[i]<<" "<<s[j]<<" "<<last[j][ch-'a']<<endl;
      ans++;
    }
  }
  cout << ans - n << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define rrep(i, a, b) for (int i = (a); i >= (b); --i)
typedef long long LL;
const int N = 2e5 + 5;

int n, last[N][26];
string s[N];
LL ans = 0;

int main() {
  cin >> n;
  rep(i, 1, n) {
    cin >> s[i];
    int sz = s[i].size();
    rep(j, 0, sz / 2) swap(s[i][j], s[i][sz - 1 - j]);
  }
  sort(s + 1, s + n + 1);

  memset(last, 0, sizeof last);
  rep(i, 1, n) {
    int sz = s[i].size();
    rep(j, 0, sz) last[i][s[i][j] - 'a'] = j + 1;
    // cout<<s[i]<<endl;
    // rep(j,'a','z') cout<<(char)j<<" "<<last[i][j-'a']<<endl;
  }

  rep(i, 1, n) {
    int l = 1, r = n, sz = s[i].size();
    rep(j, 0, sz - 2) {
      int l1 = l, r1 = i;
      l = i;
      while (l1 <= r1) {
        int mid = (l1 + r1) >> 1;
        if ((int)s[mid].size() >= sz && s[mid][j] == s[i][j])
          r1 = mid - 1, l = mid;
        else
          l1 = mid + 1;
      }
      l1 = i, r1 = r;
      r = i;
      while (l1 <= r1) {
        int mid = (l1 + r1) >> 1;
        if ((int)s[mid].size() >= sz && s[mid][j] == s[i][j])
          l1 = mid + 1, r = mid;
        else
          r1 = mid - 1;
      }
    }

    // cerr<<s[i]<<" "<<l<<" "<<r<<endl;
    char ch = s[i][sz - 1];
    rep(j, l, r) if (last[j][ch - 'a'] >= sz) ans++;
  }
  cout << ans - n << endl;
  return 0;
}
Problem p02589 (C++) — buggy submission, verdict: Runtime Error

#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);
}

Error: 0

Fixed submission:

#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);
}
Problem p02589 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define RREP(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define rrep(i, n) RREP(i, n - 1, 0)
#define all(v) v.begin(), v.end()
#define endk '\\n'
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
const ld eps = 1e-10;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<string> S(n);
  vector<unordered_map<string, int>> ss(1000005);
  vector<int> size(1000005);
  int mn = inf;
  rep(i, n) {
    cin >> S[i];
    reverse(all(S[i]));
    ss[S[i].size()][S[i]]++;
    size[S[i].size()]++;
    mn = min((int)S[i].size(), mn);
  }
  ll ans = 0;
  rep(i, n) {
    vector<bool> alpha(26);
    while (S[i].size() >= 0) {
      if (S[i].size() + 1 < mn)
        break;
      rep(k, 26) if (alpha[k]) {
        S[i] += char('a' + k);
        if (size[S[i].size()] &&
            ss[S[i].size()].find(S[i]) != ss[S[i].size()].end())
          ans++;
        S[i].pop_back();
      }
      if (S[i].size() == 0)
        break;
      alpha[S[i].back() - 'a'] = true;
      S[i].pop_back();
    }
  }
  ans -= n;
  cout << ans << endk;
  return 0;
}


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define RREP(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define rrep(i, n) RREP(i, n - 1, 0)
#define all(v) v.begin(), v.end()
#define endk '\\n'
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
const ld eps = 1e-10;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<string> S(n);
  unordered_map<string, int> ss;
  int mn = inf;
  rep(i, n) {
    cin >> S[i];
    reverse(all(S[i]));
    ss[S[i]]++;
    mn = min((int)S[i].size(), mn);
  }
  ll ans = 0;
  rep(i, n) {
    vector<bool> alpha(26);
    while (S[i].size() >= 0) {
      if (S[i].size() + 1 < mn)
        break;
      rep(k, 26) if (alpha[k]) {
        S[i] += char('a' + k);
        if (ss.find(S[i]) != ss.end())
          ans++;
        S[i].pop_back();
      }
      if (S[i].size() == 0)
        break;
      alpha[S[i].back() - 'a'] = true;
      S[i].pop_back();
    }
  }
  ans -= n;
  cout << ans << endk;
  return 0;
}
Problem p02589 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define RREP(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define rrep(i, n) RREP(i, n - 1, 0)
#define all(v) v.begin(), v.end()
#define endk '\\n'
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
const ld eps = 1e-10;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<string> S(n);
  unordered_map<string, int> ss;
  map<int, int> size;
  int mn = inf;
  rep(i, n) {
    cin >> S[i];
    reverse(all(S[i]));
    ss[S[i]]++;
    size[S[i].size()]++;
    mn = min((int)S[i].size(), mn);
  }
  ll ans = 0;
  rep(i, n) {
    vector<bool> alpha(26);
    while (S[i].size() >= 0) {
      if (S[i].size() + 1 < mn)
        break;
      rep(k, 26) if (alpha[k]) {
        S[i] += char('a' + k);
        if (size.find(S[i].size()) != size.end() && ss.find(S[i]) != ss.end())
          ans++;
        S[i].pop_back();
      }
      if (S[i].size() == 0)
        break;
      alpha[S[i].back() - 'a'] = true;
      S[i].pop_back();
    }
  }
  ans -= n;
  cout << ans << endk;
  return 0;
}


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define RREP(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define rrep(i, n) RREP(i, n - 1, 0)
#define all(v) v.begin(), v.end()
#define endk '\\n'
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
const ld eps = 1e-10;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<string> S(n);
  unordered_map<string, int> ss;
  vector<int> size(1000005);
  int mn = inf;
  rep(i, n) {
    cin >> S[i];
    reverse(all(S[i]));
    ss[S[i]]++;
    size[S[i].size()]++;
    mn = min((int)S[i].size(), mn);
  }
  ll ans = 0;
  rep(i, n) {
    vector<bool> alpha(26);
    while (S[i].size() >= 0) {
      if (S[i].size() + 1 < mn)
        break;
      rep(k, 26) if (alpha[k]) {
        string tmp = S[i] + char('a' + k);
        if (size[tmp.size()] && ss.find(tmp) != ss.end())
          ans++;
      }
      if (S[i].size() == 0)
        break;
      alpha[S[i].back() - 'a'] = true;
      S[i].pop_back();
    }
  }
  ans -= n;
  cout << ans << endk;
  return 0;
}
Problem p02589 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int nax = 1e6 + 5;
int trie[nax][26];
int nxt = 1, ans = 0;
vector<bool> is_end(nax);
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<string> input(n);
  vector<vector<int>> paths(n);
  int node;
  for (int i = 0; i < n; i++) {
    string &s = input[i];
    cin >> s;
    reverse(s.begin(), s.end());
    node = 0;
    for (auto ch : s) {
      paths[i].push_back(node);
      if (trie[node][ch - 'a'] == 0) {
        trie[node][ch - 'a'] = nxt++;
      }
      node = trie[node][ch - 'a'];
    }
    is_end[node] = true; // no charactor in the node
  }
  // check from prefix of each string
  for (int i = 0; i < n; i++) {
    string &s = input[i];
    set<char> prefixs;
    for (int pre = s.length() - 1; pre >= 0; pre--) {
      prefixs.insert(s[pre]);
      node = paths[i][pre];
      for (auto some_prefix : prefixs) {
        ans += is_end[trie[node][some_prefix - 'a']];
      }
    }
  }
  printf("%lld \\n", ans - n);
  return 0;
}

Error: 127
/tmp/59cdded1-1ff4-40bd-bc2c-d57894260ea1.out: error while loading shared libraries: libstdc++.so.6: failed to map segment from shared object


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int nax = 1e6 + 5;
int trie[nax][26];
int nxt = 1;
long long ans = 0;
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int n, node;
  cin >> n;
  vector<string> input(n);
  vector<vector<int>> paths(n);
  vector<bool> is_end(n);
  for (int i = 0; i < n; i++) {
    string &s = input[i];
    cin >> s;
    reverse(s.begin(), s.end());
    node = 0;
    for (auto ch : s) {
      paths[i].push_back(node);
      if (trie[node][ch - 'a'] == 0) {
        trie[node][ch - 'a'] = nxt++;
      }
      node = trie[node][ch - 'a'];
    }
    is_end[node] = true; // no charactor in the node
  }
  // check from prefix of each string
  for (int i = 0; i < n; i++) {
    string &s = input[i];
    set<char> prefixs;
    for (int pre = s.length() - 1; pre >= 0; pre--) {
      prefixs.insert(s[pre]);
      node = paths[i][pre];
      for (auto some_prefix : prefixs) {
        ans += is_end[trie[node][some_prefix - 'a']];
      }
    }
  }
  printf("%lld \\n", ans - n);
  return 0;
}
Problem p02589 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <iostream>
#define ll long long
#define ld long double
#define IO                                                                     \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using namespace std;
const int N = 1e6 + 2;
int trie[N][27], nxt, freq[N], first[30], n;
ll ans;
void solve(string &s) {
  int sz = s.size();
  for (int i = 0; i < 26; i++)
    first[i] = 1e7;
  for (int i = 0; i < sz; i++)
    first[s[i] - 'a'] = min(first[s[i] - 'a'], i);
  int node = 0;
  for (int i = sz - 1; i >= 0; i--) {
    for (int j = 0; j < 26; j++)
      if (first[j] <= i)
        ans += freq[trie[node][j]];
    node = trie[node][s[i] - 'a'];
  }
}
vector<string> v;
int main() {
  IO cin >> n;
  while (n--) {
    string s;
    cin >> s;
    int sz = s.size();
    v.push_back(s);
    int node = 0;
    for (int i = sz - 1; i >= 0; i--) {
      if (trie[node][s[i] - 'a'] == 0)
        trie[node][s[i] - 'a'] = ++nxt;
      node = trie[node][s[i] - 'a'];
    }
    freq[node]++;
    ans -= freq[node];
  }
  for (auto &i : v)
    solve(i);
  cout << ans;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#define ll long long
#define ld long double
#define IO                                                                     \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using namespace std;
const int m1 = 1e9 + 7, m2 = 1e9 + 9, p1 = 37, p2 = 47;
map<pair<int, int>, int> cnt;
int first[30], n;
ll ans;
void solve(string &s) {
  int sz = s.size();
  for (int i = 0; i < 26; i++)
    first[i] = 1e7;
  for (int i = 0; i < sz; i++)
    first[s[i] - 'a'] = min(first[s[i] - 'a'], i);
  int h1 = 0, h2 = 0;
  for (int i = sz - 1; i >= 0; i--) {
    for (int j = 0; j < 26; j++)
      if (first[j] <= i) {
        int h11 = (1LL * h1 * p1 + j + 1) % m1,
            h22 = (1LL * h2 * p2 + j + 1) % m2;
        ans += cnt[{h11, h22}];
      }
    h1 = (1LL * h1 * p1 + s[i] - 'a' + 1) % m1;
    h2 = (1LL * h2 * p2 + s[i] - 'a' + 1) % m2;
  }
}
vector<string> v;
int main() {
  IO cin >> n;
  while (n--) {
    string s;
    cin >> s;
    int sz = s.size();
    v.push_back(s);
    int h1 = 0, h2 = 0;
    for (int i = sz - 1; i >= 0; i--) {
      h1 = (1LL * h1 * p1 + s[i] - 'a' + 1) % m1;
      h2 = (1LL * h2 * p2 + s[i] - 'a' + 1) % m2;
    }
    cnt[{h1, h2}]++;
    ans -= cnt[{h1, h2}];
  }
  for (auto &i : v)
    solve(i);
  cout << ans;
}
Problem p02589 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define loop(i, r, n) for (int i = (r); i < (n); i++)
#define pb push_back
#define all(in) in.begin(), in.end()

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;
using ull = unsigned long long;
using namespace std;

const ll mod = 1007;

int main() {
  ll n;
  cin >> n;
  vector<string> s(n);
  rep(i, n) cin >> s[i];
  sort(all(s), [&](string a, string b) { return a.size() > b.size(); });

  vector<vector<ll>> sub(n, vector<ll>(26, 0));
  vector<ll> abc(26, 0);
  rep(i, n) {
    rep(j, s[i].size()) sub[i][s[i][j] - 'a']++;
    rep(j, 26) if (sub[i][j]) abc[j]++;
  }

  ll ans = 0, num = 1, save = n;
  vector<ll> hash(n, 0);
  unordered_map<ll, ll> mp;
  rep(i, 26) mp[i] = abc[i];

  while (save > 1) {
    unordered_map<ll, ll> nmp;
    rrep(i, save) {
      if (s[i].size() == num) {
        char c = s[i][0];
        ans += mp[hash[i] * 26 + (c - 'a')] - 1;
        n--;
      } else {
        char c = s[i][s[i].size() - num];
        hash[i] *= mod;
        hash[i] += c - 'a';
        sub[i][c - 'a']--;
        rep(j, 26) if (sub[i][j]) nmp[hash[i] * 26 + j]++;
      }
    }
    mp = nmp;
    save = n;
    num++;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define loop(i, r, n) for (int i = (r); i < (n); i++)
#define pb push_back
#define all(in) in.begin(), in.end()

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;
using ull = unsigned long long;
using namespace std;

const ll mod = 1e9 + 7;

int main() {
  ll n;
  cin >> n;
  vector<string> s(n);
  rep(i, n) cin >> s[i];
  sort(all(s), [&](string a, string b) { return a.size() > b.size(); });

  vector<vector<ll>> sub(n, vector<ll>(26, 0));
  vector<ll> abc(26, 0);
  rep(i, n) {
    rep(j, s[i].size()) sub[i][s[i][j] - 'a']++;
    rep(j, 26) if (sub[i][j]) abc[j]++;
  }

  ll ans = 0, num = 1, save = n;
  vector<ll> hash(n, 0);
  map<ll, ll> mp;
  rep(i, 26) mp[i] = abc[i];

  while (save > 1) {
    rrep(i, save) {
      rep(j, 26) if (sub[i][j]) mp[hash[i] * 26 + j]--;
      if (s[i].size() == num) {
        char c = s[i][0];
        ans += mp[hash[i] * 26 + (c - 'a')];
        n--;
      } else {
        char c = s[i][s[i].size() - num];
        hash[i] *= mod;
        hash[i] += c - 'a';
        sub[i][c - 'a']--;
        rep(j, 26) if (sub[i][j]) mp[hash[i] * 26 + j]++;
      }
    }
    save = n;
    num++;
  }
  cout << ans << endl;
  return 0;
}
Problem p02589 (C++) — buggy submission, verdict: Runtime Error

#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;
}


Error: -11

Fixed submission:

#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;
}
Problem p02589 (C++) — buggy submission, verdict: Runtime Error

#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";
}


Error: -11

Fixed submission:

#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";
}