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

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

const int N = 2e5 + 10, M = 2e6 + 10;

char str[M];
int L[N], R[N];

namespace Trie {
int ch[M][26], sz;
int val[M];

void insert(char *str, int len) {
  int u = 0;
  for (int i = 0; i < len; i++) {
    int c = str[i] - 'a';
    if (!ch[u][c])
      ch[u][c] = ++sz;
    u = ch[u][c];
  }
  val[u]++;
}
} // namespace Trie

int n;

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    scanf("%s", str + R[i - 1] + 1);
    int len = strlen(str + R[i - 1] + 1);
    L[i] = R[i - 1] + 1;
    R[i] = L[i] + len - 1;
    reverse(str + L[i], str + R[i] + 1);
    Trie::insert(str + L[i], len);
  }

  LL Ans = 0;
  for (int i = 1; i <= n; i++) {
    static int cnt[27];
    memset(cnt, 0, sizeof(cnt));
    for (int j = L[i]; j <= R[i]; j++)
      cnt[str[j] - 'a']++;
    int u = 0;
    for (int j = L[i]; j <= R[i]; j++) {
      for (int k = 0; k < 26; k++)
        if (cnt[k] && Trie::ch[u][k])
          Ans += Trie::val[Trie::ch[u][k]];
      cnt[str[j] - 'a']--;
      u = Trie::ch[u][str[j] - 'a'];
    }
  }
  cout << Ans - n << endl;

  return 0;
}
/*
3
abcxyx
cyx
abc
1
*/
/*
6
b
a
abc
c
d
ab
5
*/

Error: -11

Fixed submission:

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

const int N = 1e5 + 10;

char str[N];
int L[N], R[N];

namespace Trie {
const int SIZE = 1e6 + 10;
int ch[SIZE][26], sz;
int val[SIZE];

void insert(char *str, int len) {
  int u = 0;
  for (int i = 0; i < len; i++) {
    int c = str[i] - 'a';
    if (!ch[u][c])
      ch[u][c] = ++sz;
    u = ch[u][c];
  }
  val[u]++;
}
} // namespace Trie

int n;

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    scanf("%s", str + R[i - 1] + 1);
    int len = strlen(str + R[i - 1] + 1);
    L[i] = R[i - 1] + 1;
    R[i] = L[i] + len - 1;
    reverse(str + L[i], str + R[i] + 1);
    Trie::insert(str + L[i], len);
  }

  LL Ans = 0;
  for (int i = 1; i <= n; i++) {
    static int cnt[27];
    memset(cnt, 0, sizeof(cnt));
    for (int j = L[i]; j <= R[i]; j++)
      cnt[str[j] - 'a']++;
    int u = 0;
    for (int j = L[i]; j <= R[i]; j++) {
      for (int k = 0; k < 26; k++)
        if (cnt[k] && Trie::ch[u][k])
          Ans += Trie::val[Trie::ch[u][k]];
      cnt[str[j] - 'a']--;
      u = Trie::ch[u][str[j] - 'a'];
    }
  }
  cout << Ans - n << endl;

  return 0;
}
/*
3
abcxyx
cyx
abc
1
*/
/*
6
b
a
abc
c
d
ab
5
*/
Problem p02589 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 1000010;
ll n;
ll ans;
ll trie[N][26], quick[N][26], tot = 1;
ll sum[N], cnt[26]; // 子树和
char s[N];
string t[200010];
map<string, int> mp;
void get_ans(ll p, ll len) {
  if (len == 1) {
    // 可以不走当前字符
    ans += quick[p][s[len] - 'a'];
    return;
  } else {
    // 不可不走当前字符
    if (trie[p][s[len] - 'a']) {
      get_ans(trie[p][s[len] - 'a'], len - 1);
    }
  }
}
void insert(int p, int len) {
  sum[p]++;
  if (!len)
    return;
  int k = s[len] - 'a';
  if (!trie[p][k]) {
    trie[p][k] = ++tot;
  }
  insert(trie[p][k], len - 1);
  cnt[k]++;
  for (int i = 0; i < 26; i++) {
    if (cnt[i]) {
      quick[p][i]++;
    }
  }
}
int main() {
  scanf("%lld", &n);
  for (ll i = 1; i <= n; i++) {
    cin >> t[i];
    for (int j = 0; j < t[i].length(); j++) {
      s[j + 1] = t[i][j];
    }
    ll len = t[i].length();
    get_ans(1, len);
    memset(cnt, 0, sizeof(cnt));
    insert(1, len);
  }
  memset(trie, 0, sizeof(trie));
  memset(quick, 0, sizeof(quick));
  memset(sum, 0, sizeof(sum));
  tot = 1;
  for (int i = n; i >= 1; i--) {
    for (int j = 0; j < t[i].length(); j++) {
      s[j + 1] = t[i][j];
    }
    ans -= mp[t[i]];
    ll len = t[i].length();
    get_ans(1, len);
    memset(cnt, 0, sizeof(cnt));
    insert(1, len);
    mp[t[i]]++;
  }
  printf("%lld", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N = 1000010;
ll n;
ll ans;
ll trie[N][26], tot = 1;
ll sum[N]; // 子树和
char s[N];
string t[200010];
map<string, int> mp;
void get_ans(ll p, ll len) {
  if (!len) {
    ans += sum[p];
    return;
  }
  if (len == 1) {
    // 可以不走当前字符
    for (ll i = 0; i < 26; i++) {
      if (trie[p][i]) {
        if (i == s[len] - 'a')
          get_ans(trie[p][i], len - 1);
        else
          get_ans(trie[p][i], len);
      }
    }
  } else {
    // 不可不走当前字符
    if (trie[p][s[len] - 'a']) {
      get_ans(trie[p][s[len] - 'a'], len - 1);
    }
  }
}
int main() {
  scanf("%lld", &n);
  for (ll i = 1; i <= n; i++) {
    cin >> t[i];
    for (int j = 0; j < t[i].length(); j++) {
      s[j + 1] = t[i][j];
    }
    ll p = 1, len = t[i].length();
    get_ans(1, len);
    for (ll j = len; j >= 1; j--) {
      ll k = s[j] - 'a';
      sum[p]++;
      if (!trie[p][k])
        trie[p][k] = ++tot;
      p = trie[p][k];
    }
    sum[p]++;
    //		cout << ans << endl;
  }
  memset(trie, 0, sizeof(trie));
  memset(sum, 0, sizeof(sum));
  tot = 1;
  for (int i = n; i >= 1; i--) {
    for (int j = 0; j < t[i].length(); j++) {
      s[j + 1] = t[i][j];
    }
    ans -= mp[t[i]];
    ll p = 1, len = t[i].length();
    get_ans(1, len);
    for (ll j = len; j >= 1; j--) {
      ll k = s[j] - 'a';
      sum[p]++;
      if (!trie[p][k])
        trie[p][k] = ++tot;
      p = trie[p][k];
    }
    sum[p]++;
    mp[t[i]]++;
  }
  printf("%lld", ans);
  return 0;
}
Problem p02589 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
int n;
#define Maxn 200010
#define V 1000010
char ch[V];
int len;
char *str[Maxn];
int L[Maxn];
int *sum[26][Maxn];
struct Node {
  int son[26], sum;
} tree[V];
int root = 0, cnt = 0;
void Insert(int &k, int at) {
  if (!k)
    k = ++cnt;
  if (at == len + 1) {
    tree[k].sum++;
    return;
  }
  Insert(tree[k].son[ch[at] - 'a'], at + 1);
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%s", ch + 1);
    len = strlen(ch + 1);
    L[i] = len;
    str[i] = new char[L[i] + 1];
    for (int j = 1; j <= L[i]; ++j)
      str[i][j] = ch[j];
    for (int j = 0; j < 26; ++j) {
      sum[j][i] = new int[L[i] + 1];
      sum[j][i][0] = 0;
      for (int k = 1; k <= L[i]; ++k) {
        sum[j][i][k] = sum[j][i][k - 1];
        if ((ch[k] - 'a') == j)
          sum[j][i][k]++;
      }
    }
    reverse(ch + 1, ch + len + 1);
    Insert(root, 1);
  }
  ll Ans = -n;
  for (int i = 1; i <= n; ++i) {
    int k = root;
    for (int j = L[i]; j >= 1; --j) {
      for (int z = 0; z < 26; ++z)
        if (sum[z][i][j])
          Ans += tree[tree[k].son[z]].sum;
      k = tree[k].son[str[i][j] - 'a'];
    }
  }
  printf("%lld\\n", Ans);
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
int n;
#define Maxn 200010
#define V 1000010
char ch[Maxn];
int len;
char *str[Maxn];
int L[Maxn];
int *sum[26][Maxn];
struct Node {
  int son[26], sum;
} tree[V];
int root = 0, cnt = 0;
void Insert(int &k, int at) {
  if (!k)
    k = ++cnt;
  if (at == len + 1) {
    tree[k].sum++;
    return;
  }
  Insert(tree[k].son[ch[at] - 'a'], at + 1);
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%s", ch + 1);
    len = strlen(ch + 1);
    L[i] = len;
    str[i] = new char[L[i] + 1];
    for (int j = 1; j <= L[i]; ++j)
      str[i][j] = ch[j];
    for (int j = 0; j < 26; ++j) {
      sum[j][i] = new int[L[i] + 1];
      sum[j][i][0] = 0;
      for (int k = 1; k <= L[i]; ++k) {
        sum[j][i][k] = sum[j][i][k - 1];
        if ((ch[k] - 'a') == j)
          sum[j][i][k]++;
      }
    }
    reverse(ch + 1, ch + len + 1);
    Insert(root, 1);
  }
  ll Ans = -n;
  for (int i = 1; i <= n; ++i) {
    int k = root;
    for (int j = L[i]; j >= 1; --j) {
      for (int z = 0; z < 26; ++z)
        if (sum[z][i][j])
          Ans += tree[tree[k].son[z]].sum;
      k = tree[k].son[str[i][j] - 'a'];
    }
  }
  printf("%lld\\n", Ans);
}
Problem p02589 (C++) — buggy submission, verdict: 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 (!(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();
}

Error: 0

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

#include <bits/stdc++.h>
#define ll unsigned long long
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
using namespace std;
const int N = 2e5 + 10;
const int P = 27;
const int mod1 = 19260817;
const int mod2 = 1e7 + 9;
string s[N];
int n, cnt[33], vis1[mod1], vis2[mod2];
unordered_map<ll, int> alp;
ll ans = 0;
bool cmp(string a, string b) { return a.size() > b.size(); }
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    cin >> s[i];
  for (int i = 1; i <= n; i++) {
    ll hs = 0;
    for (int j = s[i].size() - 1; j >= 0; j--)
      hs = hs * P + s[i][j] - 'a' + 1;
    alp[hs]++, vis1[hs % mod1] = 1, vis2[hs % mod2] = 1;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= 26; j++)
      cnt[j] = 0;
    int m = s[i].size();
    ll hs = 0;
    for (int j = 0; j < m; j++)
      cnt[s[i][j] - 'a' + 1]++;
    for (int k = 1; k <= 26; k++)
      if (cnt[k] && vis1[(hs * P + k) % mod1] && vis2[(hs * P + k) % mod2])
        ans += alp[hs * P + k];
    for (int j = m - 1; j >= 0; j--) {
      hs = hs * P + s[i][j] - 'a' + 1;
      cnt[s[i][j] - 'a' + 1]--;
      for (int k = 1; k <= 26; k++)
        if (cnt[k] && vis1[(hs * P + k) % mod1] && vis2[(hs * P + k) % mod2])
          ans += alp[hs * P + k];
    }
  }
  printf("%lld\\n", ans - n);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pii pair<int, int>
using namespace std;
const int N = 2e5 + 10;
const int P = 27;
const int mod = 19260817;
string s[N];
int n, cnt[33];
unordered_map<ll, int> alp;
ll ans = 0;
bool cmp(string a, string b) { return a.size() > b.size(); }
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    cin >> s[i];
  for (int i = 1; i <= n; i++) {
    ll hs = 0;
    for (int j = s[i].size() - 1; j >= 0; j--)
      hs = hs * P + s[i][j] - 'a' + 1;
    alp[hs]++;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= 26; j++)
      cnt[j] = 0;
    int m = s[i].size();
    ll hs = 0;
    for (int j = 0; j < m; j++)
      cnt[s[i][j] - 'a' + 1]++;
    for (int k = 1; k <= 26; k++)
      if (cnt[k])
        ans += alp[hs * P + k];
    for (int j = m - 1; j >= 0; j--) {
      hs = hs * P + s[i][j] - 'a' + 1;
      cnt[s[i][j] - 'a' + 1]--;
      for (int k = 1; k <= 26; k++)
        if (cnt[k])
          ans += alp[hs * P + k];
    }
  }
  printf("%lld\\n", ans - n);
  return 0;
}
Problem p02589 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
const int N = 1000005;
int nxt;
bool is_end[N];
int child[N][26];

int main() {
  int n;
  cin >> n;
  vector<string> v;
  nxt = 1;
  vector<vector<int>> paths;
  paths.resize(n);
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    v.pb(s);

    int node = 0;
    reverse(s.begin(), s.end());
    for (char ch : s) {

      paths[i].pb(node);
      if (child[node][ch - 'a'] == 0) {

        child[node][ch - 'a'] = nxt;
        nxt++;
      }
      node = child[node][ch - 'a'];
    }
    is_end[node] = 1;
  }

  int ans = 0;
  for (int i = 0; i < n; i++) {
    string s = v[i];
    set<int> suf;
    for (int j = s.size() - 1; j >= 0; j--) {
      suf.insert(s[s.size() - j - 1] - 'a');
      int node = paths[i][j];

      for (auto ch : suf) {
        ans += is_end[child[node][ch]];
      }
    }
  }
  cout << ans - n;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#pragma optimize("-O3")
#define pb push_back
#define f first
#define s second

#define endl '\\n'
const long long int N = 1000000007;
const int p1 = 31;
const int p2 = 37;
bool pre[1000005];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;

  pair<int, string> arr[n];
  for (int i = 0; i < n; i++) {
    string st;
    cin >> st;
    arr[i] = {st.size(), st};
  }
  sort(arr, arr + n);

  long long int ans = 0;
  map<pair<long long int, long long int>, int> m;

  for (int i = 0; i < n; i++) {
    int val = 0;
    string st = arr[i].s;
    long long int ha1 = 0;
    long long int ha2 = 0;
    map<pair<long long int, long long int>, bool> don;
    int cnt[26];
    for (int i = 0; i < 26; i++) {
      cnt[i] = 0;
    }
    for (int i = 0; i < st.size(); i++) {
      cnt[st[i] - 'a']++;
    }
    for (int len = 0; len <= st.size() - 1; len++) {
      if (!don[{ha1, ha2}]) {
        don[{ha1, ha2}] = 1;
        val += m[{ha1, ha2}];
      }
      if (pre[len + 1] == 0) {
        cnt[(st[st.size() - 1 - len] - 'a')]--;
        ha1 = (ha1 * p1 + (st[st.size() - 1 - len] - 'a' + 1));
        if (ha1 > N) {
          ha1 = ha1 % N;
        }
        ha2 = (ha2 * p2 + (st[st.size() - 1 - len] - 'a' + 1));
        if (ha2 > N) {
          ha2 = ha2 % N;
        }
        continue;
      }
      long long int ha11 = (ha1 * p1) % N;
      long long int ha21 = (ha2 * p2) % N;
      for (int i = 0; i < 26; i++) {
        ha11++;
        ha21++;
        if (cnt[i] != 0) {

          if (don[{ha11, ha21}]) {
            continue;
          } else {
            don[{ha11, ha21}] = 1;
          }
          val += m[{ha11, ha21}];
        }
      }
      cnt[(st[st.size() - 1 - len] - 'a')]--;
      ha1 = (ha1 * p1 + (st[st.size() - 1 - len] - 'a' + 1));
      if (ha1 > N) {
        ha1 = ha1 % N;
      }
      ha2 = (ha2 * p2 + (st[st.size() - 1 - len] - 'a' + 1)) % N;
      if (ha2 > N) {
        ha2 = ha2 % N;
      }
    }
    ans += val;
    m[{ha1, ha2}]++;
    pre[arr[i].f] = 1;
  }
  cout << ans << endl;
  return 0;
}
Problem p02589 (C++) — buggy submission, verdict: 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[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();
}

Error: -11

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


Error: -11

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