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

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


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

#include <bits/stdc++.h> // First Second, by Errichto
using namespace std;
const int nax = 1e6 + 5;
int child[nax][26];
bool is_end[nax];
int nxt = 1;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  vector<string> in(n);
  vector<vector<int>> paths(n);
  for (int who = 0; who < n; ++who) {
    string &s = in[who];
    cin >> s;
    reverse(s.begin(), s.end());
    int node = 0;
    for (char ch : s) {
      paths[who].push_back(node);
      if (child[node][ch - 'a'] == 0) {
        child[node][ch - 'a'] = nxt++;
      }
      node = child[node][ch - 'a'];
    }
    is_end[node] = true;
  }
  long long answer = 0; // 'int' is enough
  for (int who = 0; who < n; ++who) {
    string &s = in[who];
    set<char> suffix;
    for (int i = (int)s.length() - 1; i >= 0; --i) {
      suffix.insert(s[i]);
      int node = paths[who][i];
      for (char ch : suffix) {
        answer += is_end[child[node][ch - 'a']];
      }
    }
  }
  printf("%lld\\n", answer - n); // subtract pairs (s[i], s[i])
}

Error: 0

Fixed submission:


#include <bits/stdc++.h>

using namespace std;
bool cmp(string a, string b) { return a.length() > b.length(); }

void insert_To_Map(unordered_map<string, long long> &m, string s,
                   unordered_map<string, long long> &m2) {
  if (s.length() == 0) {
    return;
  }
  if (s.length() == 1) {
    if (m2[s] == 0) {
      m[s] += 1;
      m2[s] = 1;
    }

    return;
  }
  if (m2[s] > 0) {
    return;
  }
  if (m2[s] == 0) {
    m[s] += 1;
    m2[s] = 1;
  }
  insert_To_Map(m, s.substr(1), m2);
  string str = s;
  str.erase(1, 1);
  insert_To_Map(m, str, m2);
}

int main() {
  int n;
  cin >> n;
  vector<string> v;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    v.push_back(s);
  }

  sort(v.begin(), v.end(), cmp);
  // for(int i=0;i<n;i++){
  //     cout<<v[i]<<endl;
  // }
  unordered_map<string, long long> m;
  unordered_map<string, long long> m2;
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    m2.clear();
    ans += m[v[i]];
    insert_To_Map(m, v[i], m2);
  }
  // unordered_map<string,int> :: iterator it;
  // for(it=m.begin() ;it != m.end();it++){
  //      cout << '\\t' << it->first
  //          << '\\t' << it->second << '\\n';
  // }
  cout << ans << endl;
  return 0;
}
Problem p02589 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
#define RI register int
#define CI const int &
using namespace std;
const int N = 1000005;
int n, len[N], c[26];
string s[N];
long long ans;
class Trie {
private:
  struct Trie_Node {
    int ch[26], ct[26];
  } node[N];
  int rt, tot = 1;
#define next(x, y) node[x].ch[y]
#define C(x, y) node[x].ct[y]
public:
  inline void reroot(void) {
    rt = 1;
    for (RI i = 0; i < 26; ++i)
      if (c[i])
        ++C(rt, i);
  }
  inline void insert(const char &ch) {
    if (next(rt, ch - 'a'))
      rt = next(rt, ch - 'a');
    else
      rt = next(rt, ch - 'a') = ++tot;
    for (RI i = 0; i < 26; ++i)
      if (c[i])
        ++C(rt, i);
  }
  inline int query(const string &s, CI len) {
    rt = 1;
    for (RI i = len - 1; i; --i)
      if (next(rt, s[i] - 'a'))
        rt = next(rt, s[i] - 'a');
      else
        return 0;
    return C(rt, s[0] - 'a');
  }
#undef next
#undef C
} T;
int main() {
  RI i, j;
  ios::sync_with_stdio(0);
  for (cin >> n, i = 1; i <= n; ++i) {
    for (cin >> s[i], len[i] = s[i].size(), j = 0; j < len[i]; ++j)
      ++c[s[i][j] - 'a'];
    for (T.reroot(), j = len[i] - 1; ~j; --j)
      --c[s[i][j] - 'a'], T.insert(s[i][j]);
  }
  for (i = 1; i <= n; ++i)
    ans += T.query(s[i], len[i]);
  return cout << ans - n, 0;
}

Error: WA

Fixed submission:

#include <iostream>
#include <string>
#define RI register int
#define CI const int &
using namespace std;
const int N = 200005;
int n, len[N], c[26];
string s[N];
long long ans;
class Trie {
private:
  struct Trie_Node {
    int ch[26], ct[26];
  } node[N];
  int rt, tot = 1;
#define next(x, y) node[x].ch[y]
#define C(x, y) node[x].ct[y]
public:
  inline void reroot(void) {
    rt = 1;
    for (RI i = 0; i < 26; ++i)
      if (c[i])
        ++C(rt, i);
  }
  inline void insert(const char &ch) {
    if (next(rt, ch))
      rt = next(rt, ch);
    else
      rt = next(rt, ch) = ++tot;
    for (RI i = 0; i < 26; ++i)
      if (c[i])
        ++C(rt, i);
  }
  inline int query(const string &s, CI len) {
    rt = 1;
    for (RI i = len - 1; i; --i)
      if (next(rt, s[i]))
        rt = next(rt, s[i]);
      else
        return 0;
    return C(rt, s[0] - 'a');
  }
#undef next
#undef C
} T;
int main() {
  RI i, j;
  ios::sync_with_stdio(0);
  for (cin >> n, i = 1; i <= n; ++i) {
    for (cin >> s[i], len[i] = s[i].size(), j = 0; j < len[i]; ++j)
      ++c[s[i][j] - 'a'];
    for (T.reroot(), j = len[i] - 1; ~j; --j)
      --c[s[i][j] - 'a'], T.insert(s[i][j]);
  }
  for (i = 1; i <= n; ++i)
    ans += T.query(s[i], len[i]);
  return cout << ans - n, 0;
}
Problem p02589 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
int n, cnt, t[N], ch[N][26], dp[N][26];
string a[N];
map<string, int> mp;
long long ans;
bool cmp(string a, string b) {
  return a.size() != b.size() ? a.size() < b.size() : a < b;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  sort(a + 1, a + n + 1, cmp);
  for (int i = 1; i <= n; i++) {
    for (int k = 0; k < 26; k++)
      t[k] = 0;
    for (int j = 0; j < a[i].size(); j++)
      t[a[i][j] - 'a']++;
    int u = 0;
    for (int k = 0; k < 26; k++) {
      if (t[k] == 0)
        continue;
      ans += dp[u][k];
    }
    if (a[i].size() == 1)
      dp[u][a[i][0] - 'a']++;
    for (int j = a[i].size() - 1; j >= 0; j--) {
      int x = a[i][j] - 'a';
      if (ch[u][x] == 0)
        ch[u][x] = ++cnt;
      u = ch[u][x];
      t[x]--;
      for (int k = 0; k < 26; k++) {
        if (t[k] == 0)
          continue;
        ans += dp[u][k];
      }
      if (j == 1)
        dp[u][a[i][0] - 'a']++;
    }
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
int n, cnt, dp[N][30], t[N];
string a[N];
map<string, int> mp;
long long ans;
bool cmp(string a, string b) {
  return a.size() != b.size() ? a.size() < b.size() : a < b;
}
inline int id(string &s) {
  if (mp.find(s) == mp.end())
    return mp[s] = ++cnt;
  return mp[s];
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  sort(a + 1, a + n + 1, cmp);
  for (int i = 1; i <= n; i++) {
    for (int k = 0; k < 26; k++)
      t[k] = 0;
    for (int j = 0; j < a[i].size(); j++)
      t[a[i][j] - 'a']++;
    string zh = "";
    for (int k = 0; k < 26; k++) {
      if (!t[k])
        continue;
      ans += dp[id(zh)][k];
    }
    for (int j = a[i].size() - 1; j >= 0; j--) {
      zh += a[i][j];
      t[a[i][j] - 'a']--;
      for (int k = 0; k < 26; k++) {
        if (!t[k])
          continue;
        ans += dp[id(zh)][k];
      }
    }
    zh.pop_back();
    dp[id(zh)][a[i][0] - 'a']++;
  }
  cout << ans << endl;
  return 0;
}
Problem p02589 (C++) — buggy submission, verdict: 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 = 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;
}

Error: WA

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

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

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

  int N;
  cin >> N;
  vector<string> S(N);
  for (int i = 0; i < N; i++) {
    cin >> S[i];
    reverse(S[i].begin(), S[i].end());
  }
  sort(S.begin(), S.end(),
       [](auto &s1, auto &s2) { return s1.size() > s2.size(); });
  map<ll, vector<int>> cnt;
  vector<int> one(26);
  ll ans = 0;
  for (auto &s : S) {
    int n = s.size();
    if (n == 1) {
      ans += one[s[0] - 'a'];
      continue;
    }
    vector<ll> hashes(n - 1);
    hashes[0] = s[0];
    for (int i = 1; i < n - 1; i++)
      hashes[i] = hashes[i - 1] * 100007 + s[i];
    if (cnt.count(hashes[n - 2]))
      ans += cnt[hashes[n - 2]][s[n - 1] - 'a'];
    vector<int> found(26);
    found[s[n - 1] - 'a'] = 1;
    for (int i = n - 2; i > 0; i--) {
      found[s[i] - 'a'] = 1;
      if (cnt.count(hashes[i - 1]) == 0)
        cnt[hashes[i - 1]] = vector<int>(26);
      for (int c = 0; c < 26; c++)
        cnt[hashes[i - 1]][c] += found[c];
    }
    found[s[0] - 'a'] = 1;
    for (int c = 0; c < 26; c++)
      one[c] += found[c];
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

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

  int N;
  cin >> N;
  vector<string> S(N);
  for (int i = 0; i < N; i++)
    cin >> S[i];
  sort(S.begin(), S.end(),
       [](auto &s1, auto &s2) { return s1.size() < s2.size(); });
  unordered_map<string, int> cntstr;
  ll ans = 0;
  vector<int> cnt(26);
  string t;
  for (auto &s : S) {
    cnt.assign(26, 0);
    for (char c : s)
      cnt[c - 'a']++;
    t = "";
    for (int c = 0; c < 26; c++) {
      if (cnt[c] == 0)
        continue;
      t.push_back(c + 'a');
      if (cntstr.count(t))
        ans += cntstr[t];
      t.pop_back();
    }
    for (int i = (int)s.size() - 1; i >= 0; i--) {
      t.push_back(s[i]);
      cnt[s[i] - 'a']--;
      for (int c = 0; c < 26; c++) {
        if (cnt[c] == 0)
          continue;
        t.push_back(c + 'a');
        if (cntstr.count(t))
          ans += cntstr[t];
        t.pop_back();
      }
    }
    reverse(s.begin(), s.end());
    if (cntstr.count(s) == 0)
      cntstr[s] = 1;
    else
      cntstr[s]++;
  }
  cout << ans << endl;
}
Problem p02589 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define PI pair<int, int>
const int maxm = 1e6 + 5;
string s[maxm];
int ans;
int n;
struct Trie {
  int a[maxm][26], tot;
  int type[maxm];   // 节点x的字符类型
  int val[maxm];    // 节点x为根的子树endpos个数
  int ed[maxm][26]; // ed[i][j]表示i为根的子树中经过j的endpos数
  void add(string s) {
    int len = s.size();
    int node = 0;
    for (int i = len - 1; i >= 0; i--) {
      int v = s[i] - 'a';
      if (!a[node][v]) {
        a[node][v] = ++tot;
        type[tot] = v;
      }
      node = a[node][v];
    }
    val[node]++;
  }
  void dfs(int x) {
    for (int i = 0; i < 26; i++) {
      int v = a[x][i];
      if (!v)
        continue;
      dfs(v);
      val[x] += val[v];
      for (int j = 0; j < 26; j++) {
        ed[x][j] += ed[v][j];
      }
    }
    ed[x][type[x]] = val[x]; //
  }
  void cal(string s) {
    int len = s.size();
    int node = 0;
    for (int i = len - 1; i >= 1; i--) {
      int v = s[i] - 'a';
      node = a[node][v];
    }
    for (int i = 0; i < 26; i++) {
      int v = a[node][i];
      if (!v)
        continue;
      ans += ed[v][s[0] - 'a'];
    }
  }
} T;
signed main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    T.add(s[i]);
  }
  T.dfs(0);
  for (int i = 1; i <= n; i++) {
    T.cal(s[i]);
  }
  ans -= n;
  cout << ans << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <cstring>
#include <iostream>
using namespace std;
struct Trie {
  int next[27];
  int cnt;
  int type;
  void init() {
    cnt = 0;
    memset(next, -1, sizeof next);
  }
} T[1000005];
int dp[100005][27], val[1000005];
int le;
void Insert(string s) {
  int p = 0, len = s.size();
  for (int i = len - 1; i >= 0; i--) {
    int r = s[i] - 'a';
    if (T[p].next[r] == -1) {
      T[le].init();
      T[le].type = r;
      T[p].next[r] = le++;
    }
    p = T[p].next[r];
    T[p].cnt++;
  }
  val[p]++;
}
void dfs(int x) {
  for (int i = 0, v; i < 26; i++) {
    v = T[x].next[i];
    if (v <= 0)
      continue;
    dfs(v);
    val[x] += val[v];
    for (int j = 0; j < 26; j++) {
      dp[x][j] += dp[v][j];
    }
  }
  dp[x][T[x].type] = val[x];
}
long long xx;
long long query(string s) {
  int p = 0, len = s.size();
  xx = 0;
  for (int i = len - 1; i >= 1; i--) {
    int r = s[i] - 'a';
    if (T[p].next[r] <= 0) {
      return 0;
    }
    p = T[p].next[r];
  }
  for (int i = 0, v; i < 26; i++) {
    v = T[p].next[i];
    if (v <= 0)
      continue;
    xx += dp[v][s[0] - 'a'];
  }
  return xx;
}
string s[1000005];
int n;
long long ans;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  T[0].init();
  le = 1;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    Insert(s[i]);
  }
  dfs(0);
  for (int i = 1; i <= n; i++) {
    ans += query(s[i]);
  }
  cout << ans - n;
}
Problem p02589 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
#define mkp make_pair
const ll H1 = 257, H2 = 114, M1 = 1e9 + 7, M2 = 19260817;
int n, T;
char c[1000004];
bool ok[1000004][26];
string s[200004];
int p[200004];
map<pii, int> mp;
ll ans = 0;
bool cmp(int a, int b) { return s[a].size() > s[b].size(); }
void reads(string &S) {
  S = "";
  register char ch = getchar();
  for (; ch < 'a' || ch > 'z'; ch = getchar())
    ;
  for (; ch >= 'a' && ch <= 'z'; S += ch, ch = getchar())
    ;
}
int main() {
  cin >> T;
  for (int i = 0; i < T; i++)
    reads(s[i]), p[i] = i;
  sort(p, p + T, cmp);
  while (T > 0) {
    T--;
    n = s[p[T]].size();
    for (int i = 0; i < n; i++)
      c[n - i - 1] = s[p[T]][i];
    for (int i = 0; i < 26; i++)
      ok[n][i] = 0;
    for (int i = n - 1; i >= 0; i--) {
      for (int j = 0; j < 26; j++)
        ok[i][j] = ok[i + 1][j];
      ok[i][c[i] - 'a'] = 1;
    }
    ll r1 = 0, r2 = 0;
    for (int i = 0; i < n - 1; i++) {
      int msk = mp[mkp(r1, r2)];
      for (int j = 0; j < 26; j++)
        if ((msk & (1 << j)) > 0 && ok[i][j])
          ans++;
      r1 = (r1 * H1 + c[i]) % M1, r2 = (r2 * H2 + c[i]) % M2;
    }
    mp[mkp(r1, r2)] |= (1 << (c[n - 1] - 'a'));
  }
  cout << ans;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
#define mkp make_pair
const ll H1 = 257, H2 = 114, M1 = 1e9 + 7, M2 = 19260817;
int n, T;
char c[1000004];
bool ok[1000004][26];
string s[200004];
int p[200004];
map<pii, int> mp;
ll ans = 0;
bool cmp(int a, int b) { return s[a].size() > s[b].size(); }
void reads(string &S) {
  S = "";
  register char ch = getchar();
  for (; ch < 'a' || ch > 'z'; ch = getchar())
    ;
  for (; ch >= 'a' && ch <= 'z'; S += ch, ch = getchar())
    ;
}
int main() {
  cin >> T;
  for (int i = 0; i < T; i++)
    reads(s[i]), p[i] = i;
  sort(p, p + T, cmp);
  while (T > 0) {
    T--;
    for (int i = 0; i < s[p[T]].size(); i++)
      c[i] = s[p[T]][i];
    n = s[p[T]].size();
    reverse(c, c + n);
    for (int i = 0; i < n; i++)
      c[i];
    for (int i = 0; i < 26; i++)
      ok[n][i] = 0;
    for (int i = n - 1; i >= 0; i--) {
      for (int j = 0; j < 26; j++)
        ok[i][j] = ok[i + 1][j];
      ok[i][c[i] - 'a'] = 1;
    }
    ll r1 = 0, r2 = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 26; j++)
        if (ok[i][j])
          ans += mp[mkp((r1 * H1 + j + 'a') % M1, (r2 * H2 + j + 'a') % M2)];
      r1 = (r1 * H1 + c[i]) % M1, r2 = (r2 * H2 + c[i]) % M2;
    }
    mp[mkp(r1, r2)]++;
  }
  cout << ans;
}
Problem p02589 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

const int max_n = 202222, inf = 1000111222;
const int max_v = 1000111;
const int max_c = 27;

char buf[max_v];

string read() {
  scanf("%s", buf);
  return buf;
}

int n;
int first, nxt[max_v][max_c], cnt[max_v][max_c], tot[max_c];
string s[max_n];
long long ans;

void add(const string &s) {
  memset(tot, 0, sizeof(tot));
  for (char c : s) {
    ++tot[c - 'a'];
  }
  int v = 0;
  for (char c : s) {
    for (int to = 0; to < max_c; ++to) {
      if (tot[to]) {
        ++cnt[v][to];
      }
    }
    --tot[c - 'a'];
    if (!nxt[v][c - 'a']) {
      nxt[v][c - 'a'] = ++first;
    }
    v = nxt[v][c - 'a'];
  }
}

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    s[i] = read();
    reverse(s[i].begin(), s[i].end());
    add(s[i]);
  }
  for (int i = 0; i < n; ++i) {
    int v = 0;
    for (int j = 0; j + 1 < s[i].length(); ++j) {
      v = nxt[v][s[i][j] - 'a'];
    }
    ans += cnt[v][s[i].back() - 'a'] - 1;
  }
  cout << ans << "\\n";
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int max_n = 202222, inf = 1000111222;
const int max_v = 1000111;
const int max_c = 27;

char buf[max_n];

string read() {
  scanf("%s", buf);
  return buf;
}

int n;
int first, nxt[max_v][max_c], cnt[max_v][max_c], tot[max_c];
string s[max_n];
long long ans;

void add(const string &s) {
  memset(tot, 0, sizeof(tot));
  for (char c : s) {
    ++tot[c - 'a'];
  }
  int v = 0;
  for (char c : s) {
    for (int to = 0; to < max_c; ++to) {
      if (tot[to]) {
        ++cnt[v][to];
      }
    }
    --tot[c - 'a'];
    if (!nxt[v][c - 'a']) {
      nxt[v][c - 'a'] = ++first;
    }
    v = nxt[v][c - 'a'];
  }
}

int main() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    s[i] = read();
    reverse(s[i].begin(), s[i].end());
    add(s[i]);
  }
  for (int i = 0; i < n; ++i) {
    int v = 0;
    for (int j = 0; j + 1 < s[i].length(); ++j) {
      v = nxt[v][s[i][j] - 'a'];
    }
    ans += cnt[v][s[i].back() - 'a'] - 1;
  }
  cout << ans << "\\n";
  return 0;
}