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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i, m, n) for (int i = (m); i < (n); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define all(a) a.begin(), a.end()
#define rall(c) (c).rbegin(), (c).rend()
#define mp make_pair
#define endl '\\n'
#define vec vector<ll>
#define mat vector<vector<ll>>
#define fi first
#define se second
typedef long long ll;
typedef pair<ll, ll> pll;
typedef long double ld;
const ll inf = 1e9 + 7;
const ll mod = 1e4 + 7;

int MAX = 2000100;
vector<vector<int>> trie(MAX, vector<int>(26));
vector<int> leaf(MAX);

int sz = 1;
void add_string(string s) {
  int v = 0;
  for (auto c : s) {
    if (!trie[v][c - 'a']) {
      trie[v][c - 'a'] = sz;
      sz++;
    }
    v = trie[v][c - 'a'];
  }
  leaf[v] = 1;
}

signed main() {
  ll n;
  cin >> n;
  vector<string> a(n);
  rep(i, n) {
    cin >> a[i];
    reverse(all(a[i]));
    add_string(a[i]);
  }
  ll ans = 0;
  rep(i, n) {
    string s;
    s = a[i];
    vector<ll> cnt(26);
    for (auto c : s) {
      cnt[c - 'a']++;
    }
    ll v = 0;
    rep(j, s.size()) {
      rep(x, 26) {
        if (cnt[x] && trie[v][x]) {
          ans += leaf[trie[v][x]];
        }
      }
      cnt[s[j] - 'a']--;
      v = trie[v][s[j] - 'a'];
    }
    ans--;
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i, m, n) for (int i = (m); i < (n); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define all(a) a.begin(), a.end()
#define rall(c) (c).rbegin(), (c).rend()
#define mp make_pair
#define endl '\\n'
#define vec vector<ll>
#define mat vector<vector<ll>>
#define fi first
#define se second
typedef long long ll;
typedef pair<ll, ll> pll;
typedef long double ld;
const ll inf = 1e9 + 7;
const ll mod = 1e4 + 7;

int MAX = 200010;
vector<vector<int>> trie(MAX, vector<int>(26));
vector<int> leaf(MAX);

int sz = 1;
void add_string(string s) {
  int v = 0;
  for (auto c : s) {
    if (!trie[v][c - 'a']) {
      trie[v][c - 'a'] = sz;
      sz++;
    }
    v = trie[v][c - 'a'];
  }
  leaf[v] = 1;
}

signed main() {
  ll n;
  cin >> n;
  vector<string> a(n);
  rep(i, n) {
    cin >> a[i];
    reverse(all(a[i]));
    add_string(a[i]);
  }
  ll ans = 0;
  rep(i, n) {
    string s;
    s = a[i];
    vector<ll> cnt(26);
    for (auto c : s) {
      cnt[c - 'a']++;
    }
    ll v = 0;
    rep(j, s.size()) {
      rep(x, 26) {
        if (cnt[x] && trie[v][x]) {
          ans += leaf[trie[v][x]];
        }
      }
      cnt[s[j] - 'a']--;
      if (!trie[v][s[j] - 'a'])
        break;
      v = trie[v][s[j] - 'a'];
    }
    ans--;
  }
  cout << ans << endl;
}
Problem p02589 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int N;
string s[200010];
char tmp[1000010];
typedef long long LL;
bool vis[1000010][26];
int ch[1000010][26];
bool isend[1000010];
int tot = 1;
long long ans = 0;

void ins(string s) {
  int n = s.length();
  for (int i = 0; i < 26; i++)
    vis[0][i] = (s[0] - 'a' == i);
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < 26; j++) {
      vis[i][j] = (vis[i - 1][j] | (s[i] - 'a' == j));
    }
  }
  int now = 1;
  for (int i = n - 1; i >= 0; i--) {
    for (int j = 0; j < 26; j++) {
      if (ch[now][j] && isend[ch[now][j]] && vis[i][j])
        ans++;
    }
    int v = s[i] - 'a';
    if (!ch[now][v])
      ch[now][v] = ++tot;
    now = ch[now][v];
  }
  isend[now] = true;
}

int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; i++) {
    scanf("%s", tmp);
    s[i] = string(tmp);
  }
  sort(s + 1, s + N + 1,
       [&](string s1, string s2) { return s1.length() < s2.length(); });
  for (int i = 1; i <= N; i++) {
    ins(s[i]);
  }
  printf("%lld\\n", ans);
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int N;
string s[200010];
char tmp[200010];
typedef long long LL;
bool vis[1000010][26];
int ch[1000010][26];
bool isend[1000010];
int tot = 1;
long long ans = 0;

void ins(string s) {
  int n = s.length();
  for (int i = 0; i < 26; i++)
    vis[0][i] = (s[0] - 'a' == i);
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < 26; j++) {
      vis[i][j] = (vis[i - 1][j] | (s[i] - 'a' == j));
    }
  }
  int now = 1;
  for (int i = n - 1; i >= 0; i--) {
    for (int j = 0; j < 26; j++) {
      if (ch[now][j] && isend[ch[now][j]] && vis[i][j])
        ans++;
    }
    int v = s[i] - 'a';
    if (!ch[now][v])
      ch[now][v] = ++tot;
    now = ch[now][v];
  }
  isend[now] = true;
}

int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; i++) {
    scanf("%s", tmp);
    s[i] = string(tmp);
  }
  sort(s + 1, s + N + 1,
       [&](string s1, string s2) { return s1.length() < s2.length(); });
  for (int i = 1; i <= N; i++) {
    ins(s[i]);
  }
  printf("%lld\\n", ans);
}
Problem p02589 (C++) — buggy submission, verdict: 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 (u1 && v[i].size() <= c[u1 - 1][last].size())
      ans -= c[u1 - 1][last][v[i].size() - 1];
  }
  cout << ans - n << '\\n';

  return 0;
}

Error: -11

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 (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, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAX_N = 2e6 + 5;
int N;
string s[MAX_N];
int ch[MAX_N][26];
int ed[MAX_N], tot = 1;
void ins(string s) {
  int o = 1;
  for (int i = 0; i < (int)s.length(); i++) {
    int c = s[i] - 'a';
    if (!ch[o][c])
      ch[o][c] = ++tot;
    o = ch[o][c];
  }
  ed[o]++;
}
int main() {
  ios::sync_with_stdio(0);
  cin >> N;
  for (int i = 1; i <= N; i++)
    cin >> s[i];
  for (int i = 1; i <= N; i++)
    reverse(s[i].begin(), s[i].end()), ins(s[i]);
  LL ans = 0;
  for (int i = 1; i <= N; i++) {
    int o = 1, cnt = 0, buc[30];
    memset(buc, 0, sizeof(buc));
    for (int j = 0; j < (int)s[i].length(); j++)
      ++buc[s[i][j] - 'a'];
    for (int j = 0; j < (int)s[i].length(); j++) {
      for (int k = 0; k < 26; k++)
        if (buc[k] && k != s[i][j] - 'a')
          cnt += ed[ch[o][k]];
      o = ch[o][s[i][j] - 'a'], buc[s[i][j] - 'a']--, cnt += ed[o];
    }
    ans += cnt;
  }
  cout << ans - N << '\\n';
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAX_N = 2e5 + 5;
int N;
string s[MAX_N];
int ch[MAX_N][26];
int ed[MAX_N], tot = 1;
void ins(string s) {
  int o = 1;
  for (int i = 0; i < (int)s.length(); i++) {
    int c = s[i] - 'a';
    if (!ch[o][c])
      ch[o][c] = ++tot;
    o = ch[o][c];
  }
  ed[o]++;
}
int main() {
  ios::sync_with_stdio(0);
  cin >> N;
  for (int i = 1; i <= N; i++)
    cin >> s[i];
  for (int i = 1; i <= N; i++)
    reverse(s[i].begin(), s[i].end()), ins(s[i]);
  LL ans = 0;
  for (int i = 1; i <= N; i++) {
    int o = 1, cnt = 0, buc[30];
    memset(buc, 0, sizeof(buc));
    for (int j = 0; j < (int)s[i].length(); j++)
      ++buc[s[i][j] - 'a'];
    for (int j = 0; j < (int)s[i].length(); j++) {
      for (int k = 0; k < 26; k++)
        if (buc[k] && k != s[i][j] - 'a')
          cnt += ed[ch[o][k]];
      o = ch[o][s[i][j] - 'a'], buc[s[i][j] - 'a']--, cnt += ed[o];
    }
    ans += cnt;
  }
  cout << ans - N << '\\n';
  return 0;
}
Problem p02589 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repit(it, li) for (auto it = li.begin(); it != li.end(); it++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

struct ST;
int n;
vector<string> s;
vector<ST> sts(1);
struct ST {
  vector<int> e;
  vector<int> tp;
  ST() : tp(26), e(26, -1) {}
};

void STadd(string ts) {
  auto si = 0;
  for (int i = ts.size() - 1; i >= 0; i--) {
    char c = ts[i];
    int ci = c - 'a';

    if (i >= 1) {
      int nsi = sts[si].e[ci];
      if (nsi == -1) {
        sts.push_back(ST());
        nsi = sts.size() - 1;
        sts[si].e[ci] = nsi;
      }
      si = nsi;
    } else { // i==0
      sts[si].tp[ci]++;
    }
  }
}

int main() {
  cin >> n;
  s = vector<string>(n);
  rep(i, n) cin >> s[i];

  map<char, int> sm1;

  rep(i, n) {
    if (s[i].size() == 1) {
      sm1[s[i][0]]++;
    } else {
      STadd(s[i]);
    }
  }

  ll cnt = 0;
  rep(i, n) {
    vector<int> deta(26, 0);
    rep(j, s[i].size()) deta[s[i][j] - 'a']++;
    rep(k, 26) if (deta[k] > 0) cnt += sm1['a' + k];

    int si = 0;
    for (int j = s[i].size() - 1; j >= 0; j--) {
      if (j < s[i].size() - 1) {
        int nsi = sts[si].e[s[i][j + 1] - 'a'];
        if (nsi == -1)
          break;
        si = nsi;
        rep(k, 26) if (deta[k] > 0) { cnt += sts[si].tp[k]; }
      }
      deta[s[i][j] - 'a']--;
    }
  }
  cnt -= n;

  cout << cnt << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repit(it, li) for (auto it = li.begin(); it != li.end(); it++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

struct ST;
int n;
vector<string> s;
vector<ST> sts(1);
#define SNULL sts.end()
#define SLAST (sts.end() - 1)
struct ST {
  char c;
  vector<int> e;
  vector<int> tp;
  ST(int c = '?') : c(c), tp(26), e(26, -1) {}
};

void STadd(string ts) {
  auto si = 0;
  for (int i = ts.size() - 1; i >= 0; i--) {
    char c = ts[i];
    int ci = c - 'a';

    if (i >= 1) {
      int nsi = sts[si].e[ci];
      if (nsi == -1) {
        sts.push_back(ST(c));
        nsi = sts.size() - 1;
        sts[si].e[ci] = nsi;
      }
      si = nsi;
    } else { // i==0
      sts[si].tp[ci]++;
    }
  }
}

int main() {
  sts[0] = ST('R');

  cin >> n;
  s = vector<string>(n);
  rep(i, n) cin >> s[i];

  map<char, int> sm1;

  rep(i, n) {
    if (s[i].size() == 1) {
      sm1[s[i][0]]++;
    } else {
      STadd(s[i]);
    }
  }

  ll cnt = 0;
  rep(i, n) {
    vector<int> deta(26, 0);
    rep(j, s[i].size()) deta[s[i][j] - 'a']++;
    rep(k, 26) if (deta[k] > 0) cnt += sm1['a' + k];

    int si = 0;
    for (int j = s[i].size(); j >= 0; j--) {
      if (j < s[i].size() - 1) {
        int nsi = sts[si].e[s[i][j + 1] - 'a'];
        if (nsi == -1)
          break;
        si = nsi;
        rep(k, 26) if (deta[k] > 0) { cnt += sts[si].tp[k]; }
      }
      deta[s[i][j] - 'a']--;
    }
  }
  cnt -= n;

  cout << cnt << endl;

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

#include <bits/stdc++.h>
#define re register
#define ull unsigned long long
std::unordered_map<ull, int> mp[1000100];
const ull bs = 19260817;
char ss[2001000], *s1[200100];
int len[200100], pp[200100], bl[1001000];
inline bool cmp(re int a, re int b) { return len[a] < len[b]; }
int main() {
  re int n;
  scanf("%d", &n);
  s1[1] = ss;
  for (re int i = 1; i <= n; i++) {
    scanf("%s", s1[i] + 1);
    len[i] = strlen(s1[i] + 1);
    pp[i] = i;
    s1[i + 1] = s1[i] + len[i] + 3;
  }
  std::sort(pp + 1, pp + n + 1, cmp);
  re long long ans = 0;
  for (re int i = 1; i <= n; i++) {
    re int x = pp[i];
    re int b1 = 0;
    for (re int i1 = 1; i1 <= len[x]; i1++) {
      b1 |= 1 << (s1[x][i1] - 'a');
      bl[i1] = b1;
    }
    re ull hs = 0;
    for (re int i1 = len[x]; i1 >= 1; i1--) {
      re int xx = mp[len[x] - i1][hs];
      ans += __builtin_popcount(bl[i1] & xx);
      if (i1 != 1)
        hs = hs * bs + (s1[x][i1] - 'a');
    }
    mp[len[x] - 1][hs] |= 1 << (s1[x][1] - 'a');
  }
  printf("%lld\\n", ans);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define re register
#define ull unsigned long long
std::unordered_map<ull, int> mp[1000100][26];
const ull bs = 41;
char ss[2001000], *s1[200100];
int len[200100], pp[200100];
bool bl[1001000][26], b1[26];
inline bool cmp(re int a, re int b) { return len[a] < len[b]; }
int main() {
  re int n;
  scanf("%d", &n);
  s1[1] = ss;
  for (re int i = 1; i <= n; i++) {
    scanf("%s", s1[i] + 1);
    len[i] = strlen(s1[i] + 1);
    pp[i] = i;
    s1[i + 1] = s1[i] + len[i] + 3;
  }
  std::sort(pp + 1, pp + n + 1, cmp);
  re long long ans = 0;
  for (re int i = 1; i <= n; i++) {
    re int x = pp[i];
    memset(b1, 0, sizeof(b1));
    for (re int i1 = 1; i1 <= len[x]; i1++) {
      b1[s1[x][i1] - 'a'] = 1;
      memcpy(bl[i1], b1, sizeof(b1));
    }
    re ull hs = 0;
    for (re int i1 = len[x]; i1 >= 1; i1--) {
      for (re int j1 = 0; j1 < 26; j1++)
        if (bl[i1][j1]) {
          ans += mp[len[x] - i1][j1][hs];
        }
      if (i1 != 1)
        hs = hs * bs + (s1[x][i1] - 'a');
    }
    mp[len[x] - 1][s1[x][1] - 'a'][hs]++;
  }
  printf("%lld\\n", ans);
}
Problem p02589 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

struct MyTrie {
  MyTrie *next[26];
  int leaf;
  MyTrie() {
    leaf = 0;
    for (int i = 0; i < 26; i++) {
      next[i] = nullptr;
    }
  }
  ~MyTrie() {
    for (int i = 0; i < 26; i++) {
      if (next[i]) {
        delete next[i];
      }
    }
  }
};

void insert_word(struct MyTrie *root, const string &s, int pos) {
  if (pos == s.size()) {
    root->leaf += 1;
    return;
  }
  char c = s[pos];
  if (root->next[c - 'a'] == nullptr) {
    root->next[c - 'a'] = new MyTrie();
  }
  insert_word(root->next[c - 'a'], s, pos + 1);
}

struct MyTrie *find_word(struct MyTrie *root, const string &s, int pos) {
  if (s.size() == pos) {
    return root;
  }
  char c = s[pos];
  if (root->next[c - 'a'] == nullptr) {
    return nullptr;
  }
  return find_word(root->next[c - 'a'], s, pos + 1);
}

int main() {
  int n;
  cin >> n;
  vector<string> vs;
  string s;
  struct MyTrie *root = new MyTrie();
  for (int i = 0; i < n; i++) {
    cin >> s;
    reverse(s.begin(), s.end());
    vs.push_back(s);

    insert_word(root, s, 0);
  }
  long long int res = 0;
  for (int i = 0; i < n; i++) {
    vector<int> v(26, 0);
    for (auto ss : vs[i]) {
      v[ss - 'a']++;
    }

    struct MyTrie *base = root;
    for (int k = 0; k < 26; k++) {
      if (base->next[k] != nullptr && v[k] > 0) {
        res += base->next[k]->leaf;
      }
    }
    for (int j = 0; j < vs[i].size(); j++) {
      v[vs[i][j] - 'a']--;
      base = base->next[vs[i][j] - 'a'];
      if (base == nullptr)
        break;
      for (int k = 0; k < 26; k++) {
        if (base->next[k] != nullptr && v[k] > 0) {
          res += base->next[k]->leaf;
        }
      }
    }
  }
  cout << res - n << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<string> vs;
  string s;
  unordered_map<string, unordered_map<char, int>> um;
  for (int i = 0; i < n; i++) {
    cin >> s;
    reverse(s.begin(), s.end());
    vs.push_back(s);

    char last = s.back();
    s.pop_back();
    um[s][last]++;
  }
  long long int res = 0;
  for (int i = 0; i < n; i++) {
    vector<int> v(26, 0);
    for (auto ss : vs[i]) {
      v[ss - 'a']++;
    }
    s = "";
    if (um.find(s) != um.end()) {
      for (auto p : um[s]) {
        if (v[p.first - 'a'] > 0) {
          res += p.second;
        }
      }
    }
    for (int j = 0; j < vs[i].size(); j++) {
      s.push_back(vs[i][j]);
      v[vs[i][j] - 'a']--;
      if (um.find(s) != um.end()) {
        for (auto p : um[s]) {
          if (v[p.first - 'a'] > 0) {
            res += p.second;
          }
        }
      }
    }
  }
  cout << res - n << endl;
}
Problem p02589 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int ch[1000005][26], sum[1000005][26], s[200005], c[200005], tot, siz[1000005],
    le[26];
string str[200005];
bool cmp(int x, int y) { return s[x] > s[y]; }
int main() {
  int n;
  long long ans = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    cin >> str[i], s[i] = str[i].size(), c[i] = i;
  sort(c + 1, c + n + 1, cmp);
  for (int i = 1; i <= n; i++) {
    int now = 0;
    for (int j = 0; j < str[c[i]].size(); j++)
      le[str[c[i]][j] - 'a']++;
    for (int j = str[c[i]].size() - 1; j > 0; j--)
      if (!ch[now][str[c[i]][j] - 'a'])
        goto nxt;
      else
        now = ch[now][str[c[i]][j] - 'a'];
    ans += siz[ch[now][str[c[i]][0] - 'a']] + sum[now][str[c[i]][0] - 'a'];
  nxt:;
    now = 0;
    for (int j = str[c[i]].size(); j >= 0; j--) {
      if (j != s[c[i]]) {
        if (!ch[now][str[c[i]][j] - 'a'])
          ch[now][str[c[i]][j] - 'a'] = ++tot;
        now = ch[now][str[c[i]][j] - 'a'];
        siz[now]++;
        le[str[c[i]][j] - 'a']--;
      }
      for (int k = 0; k <= 25; k++)
        if (le[k] && str[c[i]][j - 1] - 'a' != k)
          sum[now][k]++;
    }
  }
  printf("%lld", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
ll const base = 233;
string str[200005];
unordered_map<ll, int> mp;
ll pw[1000005], T[200005];
int lef[26], c[200005], s[200005];
bool cmp(int x, int y) { return s[x] > s[y]; }
int main() {
  int n;
  ll ans = 0;
  scanf("%d", &n);
  pw[0] = 1;
  for (int i = 1; i <= 1000000; i++)
    pw[i] = 1ull * pw[i - 1] * base;
  for (int i = 1; i <= n; i++)
    cin >> str[i], c[i] = i, s[i] = str[i].size();
  sort(c + 1, c + n + 1, cmp);
  for (int i = 1; i <= n; i++) {
    ll res = 0;
    int m = str[c[i]].size();
    for (int j = 0; j < m; j++)
      res = res * base + str[c[i]][j], lef[str[c[i]][j] - 'a']++;
    ans += mp[res];
    res = 0;
    for (int j = m; j >= 0; j--) {
      if (j != m) {
        res += 1ull * str[c[i]][j] * pw[m - j - 1];
        mp[res]++;
        lef[str[c[i]][j] - 'a']--;
      }
      for (int k = 0; k <= 25; k++)
        if (lef[k] && k != str[c[i]][j - 1] - 'a')
          mp[(res + (k + 'a') * pw[m - j])]++;
    }
  }
  printf("%llu", ans);
  return 0;
}
Problem p02589 (C++) — buggy submission, verdict: Runtime Error

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

const int N = 1e6;
const int mo0 = 998244353;
const int mo1 = 1004535809;

int n;
string str[N + 5];

bool ed[N + 5];
int ch[N + 5][26], tot;

int main() {
#ifdef Wearry
  freopen("in", "r", stdin);
#endif

  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    cin >> str[i];
    reverse(str[i].begin(), str[i].end());
  }

  sort(str + 1, str + n + 1,
       [&](string a, string b) { return a.length() < b.length(); });

  long long ans = 0;
  for (int i = 1; i <= n; ++i) {
    static int cnt[30];
    memset(cnt, 0, sizeof cnt);

    int u = 0;
    for (int j = 0; j < (int)str[i].length(); ++j) {
      for (int c = 0; c < 26; ++c)
        if (ch[u][c] && ed[ch[u][c]]) {
          ++cnt[c];
        }

      ans = ans + cnt[str[i][j] - 'a'];
      cnt[str[i][j] - 'a'] = 0;

      if (!ch[u][str[i][j] - 'a'])
        ch[u][str[i][j] - 'a'] = ++tot;
      u = ch[u][str[i][j] - 'a'];
    }
    ed[u] = true;
  }
  printf("%lld\\n", ans);

  return 0;
}


Error: 0

Fixed submission:

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

const int N = 1e6;
const int mo0 = 998244353;
const int mo1 = 1004535809;

int n;
string str[N + 5];

map<long long, bitset<26>> mp;

int main() {
#ifdef Wearry
  freopen("in", "r", stdin);
#endif

  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    cin >> str[i];
    reverse(str[i].begin(), str[i].end());
  }

  sort(str + 1, str + n + 1,
       [&](string a, string b) { return a.length() < b.length(); });

  long long ans = 0;
  for (int i = 1; i <= n; ++i) {
    static int cnt[30];
    memset(cnt, 0, sizeof cnt);

    int hsh0 = 0, hsh1 = 0;
    for (int j = 0; j < (int)str[i].length() - 1; ++j) {
      bitset<26> temp = mp[1ll * hsh0 * mo1 + hsh1];
      for (int x = 0; x < 26; ++x)
        if (temp[x]) {
          ++cnt[x];
        }

      ans += cnt[str[i][j] - 'a'];
      cnt[str[i][j] - 'a'] = 0;

      hsh0 = (hsh0 * 29ll + str[i][j] - 'a') % mo0;
      hsh1 = (hsh1 * 31ll + str[i][j] - 'a') % mo1;
    }

    ans += cnt[str[i].back() - 'a'];
    assert(mp[1ll * hsh0 * mo1 + hsh1][str[i].back() - 'a'] == 0);
    mp[1ll * hsh0 * mo1 + hsh1].set(str[i].back() - 'a');
  }
  printf("%lld\\n", ans);

  return 0;
}