alexjercan/bugnet huggingface dataset:
Problem p02588 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h> // Integer Product, by Errichto
using namespace std;
int main() {
  int n;
  scanf("%d", &n);
  map<pair<int, int>, int> cnt;
  for (int rep = 0; rep < n; ++rep) {
    double x;
    scanf("%lf", &x);
    long long i = llround(x * 1e9);
    int two = 0, five = 0;
    while (i % 2 == 0) {
      two++;
      i /= 2;
    }
    while (i % 5 == 0) {
      five++;
      i /= 5;
    }
    ++cnt[{two, five}];
  }
  long long answer = 0;
  for (auto p1 : cnt) {
    for (auto p2 : cnt) {
      if (p1.first.first + p2.first.first >= 18 &&
          p1.first.second + p2.first.second >= 18) {
        if (p1 < p2) {
          answer += (long long)p1.second * p2.second;
        } else if (p1 == p2) {
          answer += (long long)p1.second * (p1.second - 1) / 2;
        }
      }
    }
  }
  printf("%lld\\n", answer);
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  // freopen("in.txt","r",stdin);
  int n;
  int arr[1000000][4];
  cin >> n;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    string ss;
    cin >> ss;

    long long temp = 0;
    int cnt = 0;
    bool chk = false;
    for (int j = 0; j < ss.size(); j++) {
      if (ss[j] == '.') {
        chk = true;
        continue;
      }
      if (chk == true)
        cnt++;
      temp = temp * 10 + ss[j] - 48;
    }
    int two = 0, five = 0;

    while (temp % 2 == 0) {
      two++;
      temp /= 2;
    }

    while (temp % 5 == 0) {
      five++;
      temp /= 5;
    }
    arr[i][0] = two;
    arr[i][1] = five;
    arr[i][2] = cnt;
  }

  int ans = 0;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      int two = arr[i][0] + arr[j][0];
      int five = arr[i][1] + arr[j][1];
      int need = arr[i][2] + arr[j][2];

      if (min(two, five) >= need)
        ans++;
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p02588 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long int ll;
typedef pair<int, int> pii;
const ll base = 1000000000;
const int n2 = 9;
const int n5 = 9;
int n;
ll ans;
int bucket[555][555];
pii get(ll x) {
  int x2 = -n2, x5 = -n5;
  while (x % 2 == 0)
    x /= 2, ++x2;
  while (x % 5 == 0)
    x /= 5, ++x5;
  return pii(x2, x5);
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int e = 1; e <= n; ++e) {
    string s;
    cin >> s;
    ll x = 0, G = base, f = 0;
    for (int i = 0; i < s.size(); ++i) {
      if (s[i] == '.') {
        f = 1;
        continue;
      }
      x = x * 10 + s[i] - '0';
      if (f)
        G /= 10;
    }
    x = x * G;
    pii g = get(x);
    int x2 = g.first, x5 = g.second;
    for (int j = -x2; j <= 46; ++j)
      for (int k = -x5; k <= 21; ++k)
        if (x2 + j >= 0 && x5 + k >= 0)
          ans += bucket[j + 50][k + 50];
    ++bucket[x2 + 50][x5 + 50];
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long int ll;
typedef pair<int, int> pii;
const ll base = 1000000000;
const int n2 = 9;
const int n5 = 9;
int n;
ll ans;
int bucket[555][555];
pii get(ll x) {
  int x2 = -n2, x5 = -n5;
  while (x % 2 == 0)
    x /= 2, ++x2;
  while (x % 5 == 0)
    x /= 5, ++x5;
  return pii(x2, x5);
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int e = 1; e <= n; ++e) {
    string s;
    cin >> s;
    ll x = 0, G = base, f = 0;
    for (int i = 0; i < s.size(); ++i) {
      if (s[i] == '.') {
        f = 1;
        continue;
      }
      x = x * 10 + s[i] - '0';
      if (f)
        G /= 10;
    }
    x = x * G;
    pii g = get(x);
    int x2 = g.first, x5 = g.second;
    for (int j = -x2; j <= 15; ++j)
      for (int k = -x5; k <= 15; ++k)
        if (x2 + j >= 0 && x5 + k >= 0)
          ans += bucket[j + n2][k + n5];
    ++bucket[x2 + n2][x5 + n5];
  }
  cout << ans << endl;
  return 0;
}
Problem p02588 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

const int INF = 1e9;
const int MOD = 1e9 + 7;

using namespace std;

typedef long long ll;

#define REP(i, n) for (int i = 0; i < int(n); i++)
#define REPD(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (int i = a; i < int(b); i++)
#define FORD(i, a, b) for (int i = b - 1; i >= int(a); i--)
#define WRAP(y, x, h, w) (0 <= y && y < h && 0 <= x && x < w)

#define ALL(x) (x).begin(), (x).end()

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {
  int n;
  cin >> n;
  vector<ll> vll(n);
  REP(i, n) {
    double d;
    cin >> d;
    vll[i] = (ll)(d * 1e9 + 0.5);
  }

  vector<vector<int>> vvi(64, vector<int>(64, 0));
  ll ans = 0;

  REP(i, vll.size()) {
    int cnt2 = 0;
    int cnt5 = 0;
    while (vll[i] % 2 == 0) {
      cnt2++;
      vll[i] /= 2;
    }
    while (vll[i] % 5 == 0) {
      cnt5++;
      vll[i] /= 5;
    }
    vvi[cnt2][cnt5]++;
    if (cnt2 >= 9 && cnt5 >= 9)
      ans--;
  }

  REP(i, 64)
  REP(j, 64)
  REP(k, 64)
  REP(l, 64) {
    if (i + k < 18 || j + l < 18)
      continue;
    ans += 1LL * vvi[i][j] * vvi[k][l];
  }

  cout << ans / 2 << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

const int INF = 1e9;
const int MOD = 1e9 + 7;

using namespace std;

typedef long long ll;

#define REP(i, n) for (int i = 0; i < int(n); i++)
#define REPD(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (int i = a; i < int(b); i++)
#define FORD(i, a, b) for (int i = b - 1; i >= int(a); i--)
#define WRAP(y, x, h, w) (0 <= y && y < h && 0 <= x && x < w)

#define ALL(x) (x).begin(), (x).end()

int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {
  int n;
  cin >> n;
  vector<string> vs(n);
  REP(i, n)
  cin >> vs[i];
  vector<ll> vll;
  REP(i, n) {
    ll itg;
    auto idx = vs[i].find('.');
    if (idx != string::npos) {
      string s = vs[i].substr(idx + 1, vs[i].length() - idx - 1);
      while (s.length() < 9)
        s += '0';
      itg = atoll((vs[i].substr(0, idx) + s).c_str());
    } else {
      itg = atoll(vs[i].substr(0, idx).c_str());
      itg *= 1000000000;
    }
    vll.push_back(itg);
  }
  vector<vector<int>> vvi(32, vector<int>(32, 0));
  int ans = 0;

  REP(i, vll.size()) {
    int cnt2 = 0;
    int cnt5 = 0;
    while (vll[i] % 2 == 0) {
      cnt2++;
      vll[i] /= 2;
    }
    while (vll[i] % 5 == 0) {
      cnt5++;
      vll[i] /= 5;
    }
    vvi[cnt2][cnt5]++;
    if (cnt2 >= 9 && cnt5 >= 9)
      ans--;
  }
  REP(i, 32)
  REP(j, 32)
  REP(k, 32)
  REP(l, 32) {
    if (i + k < 18 || j + l < 18)
      continue;
    ans += vvi[i][j] * vvi[k][l];
  }

  cout << ans / 2 << endl;
}
Problem p02589 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <cmath>
#include <sstream>
#include <string>
#include <unordered_map>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORD(i, a, b) for (int i = (a - 1); i >= (b); i--)
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ff first
#define ss second
typedef long long int ll;
using namespace std;
// ll mod=1000000007;
ll mod = 67280421310721;
// ll mod=998244353;
ll INF = 1e18;
ll po(ll a, ll b) {
  ll re = 1;
  while (b) {
    if (b % 2 == 1) {
      re = (re * a);
      re %= mod;
    }
    a = (a * a);
    a %= mod;
    b >>= 1;
  }
  return re;
}
bool che(string a, string b) { return a.size() < b.size(); }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int test = 1;
  //	cin>>test;
  while (test--) {
    int n;
    cin >> n;
    string s[n];
    unordered_map<ll, int> cnt[27];
    ll hash;
    FOR(i, 0, n) { cin >> s[i]; }
    sort(s, s + n, che);
    ll ans = 0;
    FOR(i, 0, n) {
      hash = 0;
      int cn[26] = {0};
      FORD(j, s[i].size(), 0) {
        if (cnt[26][hash] > 0) {
          FOR(k, 0, 26) { cn[k] += cnt[k][hash]; }
        }
        ans += cn[s[i][j] - 'a'];
        cn[s[i][j] - 'a'] = 0;
        if (j == 0) {
          cnt[s[i][0] - 'a'][hash]++;
          cnt[26][hash]++;
        }
        hash *= 31;
        hash += (s[i][j] - 'a' + 2);
        hash %= mod;
      }
    }
    cout << ans;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <cmath>
#include <sstream>
#include <string>
#include <unordered_map>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORD(i, a, b) for (int i = (a - 1); i >= (b); i--)
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ff first
#define ss second
typedef long long int ll;
using namespace std;
// ll mod=1000000007;
ll mod = 67280421310721;
// ll mod=998244353;
ll INF = 1e18;
ll po(ll a, ll b) {
  ll re = 1;
  while (b) {
    if (b % 2 == 1) {
      re = (re * a);
      re %= mod;
    }
    a = (a * a);
    a %= mod;
    b >>= 1;
  }
  return re;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int test = 1;
  //	cin>>test;
  while (test--) {
    int n;
    cin >> n;
    string s[n];
    unordered_map<ll, int> cnt[27];
    ll hash;
    FOR(i, 0, n) {
      hash = 0;
      cin >> s[i];
      FORD(j, s[i].size(), 1) {
        hash *= 31;
        hash += (s[i][j] - 'a' + 2);
        hash %= mod;
      }
      cnt[s[i][0] - 'a'][hash]++;
      cnt[26][hash]++;
    }
    ll ans = 0;
    FOR(i, 0, n) {
      hash = 0;
      int cn[26] = {0};
      FORD(j, s[i].size(), 0) {
        if (cnt[26][hash] > 0) {
          FOR(k, 0, 26) { cn[k] += cnt[k][hash]; }
        }
        ans += cn[s[i][j] - 'a'];
        cn[s[i][j] - 'a'] = 0;
        hash *= 31;
        hash += (s[i][j] - 'a' + 2);
        hash %= mod;
      }
    }
    cout << ans - n;
  }
  return 0;
}
Problem p02589 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
long long n, cur, pas, b[5 * N], endz[N * 10][30], aft[N * 10][30], k, ans;
string s[N];
bool ss(string s1, string s2) { return s1.size() < s2.size(); }
int get() {
  int u = 0, pas = 0;
  b[0] = 1ll << (s[k][0] - 'a');
  for (int i = 1; i < s[k].size(); i++) {
    b[i] = b[i - 1] | (1ll << (s[k][i] - 'a'));
  }
  // cout<<s[k].size()<<endl;
  for (int i = s[k].size() - 1; i >= 0; i--) {

    if (i == 0)
      break;
    for (int j = 0; j <= 25; j++) {
      if (b[i] & (1 << j))
        pas += endz[u][j];
    }
    if (!aft[u][s[k][i] - 'a'])
      break;
    u = aft[u][s[k][i] - 'a'];
  }
  return pas;
}
void add() {
  int u = 0;
  for (int i = s[k].size() - 1; i >= 1; i--) {
    if (!aft[u][s[k][i] - 'a']) {
      cur++;
      aft[u][s[k][i] - 'a'] = cur;
    }
    u = aft[u][s[k][i] - 'a'];
  }
  endz[u][s[k][0] - 'a']++;
}
int main() {
  cin >> n;
  for (k = 1; k <= n; k++) {
    cin >> s[k];
  }
  sort(s + 1, s + n + 1, ss);
  long long ans = 0;
  for (k = 1; k <= n; k++) {
    ans += (long long)get();
    add();
  }
  cout << ans;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
long long n, cur, pas, b[N], endz[N * 10][30], aft[N * 10][30], k, ans;
string s[N];
bool ss(string s1, string s2) { return s1.size() < s2.size(); }
int get() {
  int u = 0, pas = 0;
  b[0] = 1ll << (s[k][0] - 'a');
  for (int i = 1; i < s[k].size(); i++) {
    b[i] = b[i - 1] | (1ll << (s[k][i] - 'a'));
  }
  // cout<<s[k].size()<<endl;
  for (int i = s[k].size() - 1; i >= 0; i--) {

    if (i == 0)
      break;
    for (int j = 0; j <= 25; j++) {
      if (b[i] & (1 << j))
        pas += endz[u][j];
    }
    if (!aft[u][s[k][i] - 'a'])
      break;
    u = aft[u][s[k][i] - 'a'];
  }
  return pas;
}
void add() {
  int u = 0;
  for (int i = s[k].size() - 1; i >= 1; i--) {
    if (!aft[u][s[k][i] - 'a']) {
      cur++;
      aft[u][s[k][i] - 'a'] = cur;
    }
    u = aft[u][s[k][i] - 'a'];
  }
  endz[u][s[k][0] - 'a']++;
}
int main() {
  cin >> n;
  for (k = 1; k <= n; k++) {
    cin >> s[k];
  }
  sort(s + 1, s + n + 1, ss);
  long long ans = 0;
  for (k = 1; k <= n; k++) {
    ans += (long long)get();
    add();
  }
  cout << ans;
}
Problem p02589 (C++) — buggy submission, verdict: Runtime Error

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

const int nax = 1e6 + 5;
int child[nax][26];
bitset<nax> is_end;
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);
  memset(child, 0, sizeof child);
  for (int i = 0; i < n; ++i) {
    auto &s = in[i];
    cin >> s;
    reverse(begin(s), end(s));
    int node = 0;
    for (char ch : s) {
      paths[i].push_back(node);
      if (child[node][ch - 'a'] == 0) {
        child[node][ch - 'a'] = nxt++;
      }
      node = child[node][ch - 'a'];
    }
    is_end[node] = 1;
  }
  long long ans = 0;

  for (int i = 0; i < n; ++i) {
    auto &s = in[i];
    set<int> suffix;

    for (int j = (int)s.size() - 1; j >= 0; --j) {
      int node = paths[i][j];
      suffix.insert(s[j]);
      for (char ch : suffix) {
        ans += is_end[child[node][ch - 'a']];
      }
      cout << "\\n";
    }
  }

  cout << ans - n;
}


Error: 127
/tmp/8202caa0-f18f-45e4-a59a-d66b61c0d854.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 child[nax][26];
bitset<nax> is_end;
int nxt = 1;

int main() {
  int n;
  cin >> n;
  vector<string> in(n);
  vector<vector<int>> paths(n);
  memset(child, 0, sizeof child);
  for (int i = 0; i < n; ++i) {
    auto &s = in[i];
    cin >> s;
    reverse(begin(s), end(s));
    int node = 0;
    for (char ch : s) {
      paths[i].push_back(node);
      if (child[node][ch - 'a'] == 0) {
        child[node][ch - 'a'] = nxt++;
      }
      node = child[node][ch - 'a'];
    }
    is_end[node] = 1;
  }
  long long ans = 0;

  for (int i = 0; i < n; ++i) {
    auto &s = in[i];
    set<int> suffix;

    for (int j = (int)s.size() - 1; j >= 0; --j) {
      int node = paths[i][j];
      suffix.insert(s[i]);

      for (char ch : s) {
        ans += is_end[child[node][ch - 'a']];
      }
    }
  }

  cout << ans - n;
}
Problem p02589 (C++) — buggy submission, verdict: Runtime Error

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

const int ALPHABET_SIZE = 26;
int glob_ans = 0;

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

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

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

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

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


Error: -11

Fixed submission:

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

const int ALPHABET_SIZE = 26;
int glob_ans = 0;

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

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

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

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

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e6 + 5;
int num[N], nex[N][26], sum[N][26], cnt = 1;
ll ans;
string s[N / 5 + 5];
void add1(int x) {
  int n = s[x].length();
  int now = 1;
  for (int i = n - 1; ~i; i--) {
    if (!nex[now][s[x][i] - 'a'])
      nex[now][s[x][i] - 'a'] = ++cnt;
    now = nex[now][s[x][i] - 'a'];
  }
}
void add2(int x) {
  int n = s[x].length();
  int now = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 26; j++) {
      if (s[x][i] - 'a' == j)
        sum[i][j] = (i > 0 ? sum[i - 1][j] : 0) + 1;
      else
        sum[i][j] = (i > 0 ? sum[i - 1][j] : 0);
    }
  }
  for (int i = n - 1; ~i; i--) {
    for (int j = 0; j < 26; j++)
      if (sum[i][j] && nex[now][j])
        num[nex[now][j]]++;
    now = nex[now][s[x][i] - 'a'];
  }
}
void finds(int x) {
  int n = s[x].length();
  int now = 1;
  for (int i = n - 1; ~i; i--)
    now = nex[now][s[x][i] - 'a'];
  ans += num[now] - 1;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    add1(i);
  }
  for (int i = 1; i <= n; i++)
    add2(i);
  for (int i = 1; i <= n; i++)
    finds(i);
  cout << ans << '\\n';
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e6 + 5;
int num[N * 26], nex[N * 5][26], sum[N][26], cnt = 1;
ll ans;
string s[N / 5 + 5];
void add(int x) {
  int n = s[x].length();
  int now = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 26; j++) {
      if (s[x][i] - 'a' == j)
        sum[i][j] = sum[i - 1][j] + 1;
      else
        sum[i][j] = sum[i - 1][j];
    }
  }
  for (int i = n - 1; ~i; i--) {
    for (int j = 0; j < 26; j++) {
      if (sum[i][j]) {
        if (!nex[now][j])
          nex[now][j] = ++cnt;
        num[nex[now][j]]++;
      }
    }
    now = nex[now][s[x][i] - 'a'];
  }
}
void finds(int x) {
  int n = s[x].length();
  int now = 1;
  for (int i = n - 1; ~i; i--)
    now = nex[now][s[x][i] - 'a'];
  ans += num[now] - 1;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    add(i);
  }
  for (int i = 1; i <= n; i++)
    finds(i);
  printf("%lld\\n", ans);
  return 0;
}
Problem p02589 (C++) — buggy submission, verdict: Time Limit Exceeded

// ababa
//  baba
// a  ba

//   aba
//  b ba
//   aba
// a  ba

//

#include <algorithm>
#include <iostream>
#include <set>
#include <unordered_map>
#include <vector>

using namespace std;
typedef long long ll;
ll ha[1'000'001];
const ll hash_c1 = 1007;
const ll hash_c2 = 11;
int main() {
  int n;
  cin >> n;
  unordered_map<ll, int> n_intact;
  vector<string> input;
  ll ans = 0;
  for (int i = 0; i < n; ++i) {
    string s;
    cin >> s;
    input.push_back(s);
    ll h = 0;
    // ha[j] == s[j...]
    ha[s.size()] = 0;
    // cout<<s<<": ";
    for (int j = (int)s.size() - 1; j >= 0; --j) {
      ha[j] = ha[j + 1] * hash_c1 + s[j];
      // cout<<ha[j]<<' ';
    }
    ans += n_intact[ha[0]];
    ++n_intact[ha[0]];
  }

  for (int i = 0; i < n; ++i) {
    string s = input[i];
    ll h = 0;
    // ha[j] == s[j...]
    ha[s.size()] = 0;
    // cout<<s<<": ";
    for (int j = (int)s.size() - 1; j >= 0; --j) {
      ha[j] = ha[j + 1] * hash_c1 + s[j];
      // cout<<ha[j]<<' ';
    }
    // cout<<'\\n';
    set<char> c;
    for (int j = 0; j < s.size(); ++j) {
      c.insert(s[j]);
      if (j == 0)
        continue;
      for (auto x : c) {
        ll comb_hash = ha[j + 1] * hash_c1 + x;
        ans += n_intact.count(comb_hash);
      }
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

// ababa
//  baba
// a  ba

//   aba
//  b ba
//   aba
// a  ba

//

#include <algorithm>
#include <iostream>
#include <set>
#include <unordered_map>
#include <vector>

using namespace std;
typedef long long ll;
ll ha[1'000'001];
const ll hash_c1 = 1007;
const ll hash_c2 = 11;
int main() {
  int n;
  cin >> n;
  unordered_map<ll, int> n_altered;
  unordered_map<ll, int> n_intact;
  ll ans = 0;
  for (int i = 0; i < n; ++i) {
    string s;
    cin >> s;
    ll h = 0;
    // ha[j] == s[j...]
    ha[s.size()] = 0;
    // cout<<s<<": ";
    for (int j = (int)s.size() - 1; j >= 0; --j) {
      ha[j] = ha[j + 1] * hash_c1 + s[j];
      // cout<<ha[j]<<' ';
    }
    // cout<<'\\n';
    ans += n_altered[ha[0]];
    // vector<ll> v;
    set<char> c;
    for (int j = 0; j < s.size(); ++j) {
      c.insert(s[j]);
      for (auto x : c) {
        ll comb_hash = ha[j + 1] * hash_c1 + x;
        // v.push_back(comb_hash);
        // cout<<x<<" + "<<j+1<<": "<<comb_hash<<endl;
        ++n_altered[comb_hash];
        ans += n_intact.count(comb_hash);
      }
    }
    ans += n_intact[ha[0]];
    ++n_intact[ha[0]];
    // sort(v.begin(), v.end());
    // v.erase(unique(v.begin(), v.end()), v.end());
  }
  cout << ans << endl;
}
Problem p02589 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#pragma GCC optimize(2)
using namespace std;
typedef int LL;

LL n, ans = 0, cnt[555];
string s[200005], t, tt;
map<string, LL> mp[1000005];
LL vis[1000005];

int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (LL ls, i = 1; i <= n; i++) {
    cin >> s[i];
    ls = s[i].length();
    t = s[i];
    reverse(t.begin(), t.end());
    mp[ls][t]++;
    vis[ls] = 1;
  }
  for (LL len, ls, i = 1; i <= n; i++) {
    for (char j = 'a'; j <= 'z'; j++)
      cnt[j] = 0;
    ls = s[i].length();
    for (LL j = 0; j < ls; j++)
      cnt[s[i][j]]++;
    t = "";
    len = 0;
    for (LL j = ls - 1; j >= 0; j--) {
      for (char k = 'a'; k <= 'z'; k++) {
        if (cnt[k]) {
          t += k;
          len++;
          if (vis[len])
            if (mp[len].find(t) != mp[len].end())
              ans++;
          t.pop_back();
          len--;
        }
      }
      t += s[i][j];
      cnt[s[i][j]]--;
      len++;
    }
  }
  cout << ans - n << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#pragma GCC optimize(2)
using namespace std;
typedef long long LL;

LL n, ans = 0, cnt[555];
string s[200005], t, tt;
map<string, LL> mp;

int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (LL i = 1; i <= n; i++) {
    cin >> s[i];
    t = s[i];
    reverse(t.begin(), t.end());
    mp[t]++;
  }
  for (LL ls, i = 1; i <= n; i++) {
    for (char j = 'a'; j <= 'z'; j++)
      cnt[j] = 0;
    ls = s[i].length();
    for (LL j = 0; j < ls; j++)
      cnt[s[i][j]]++;
    t = "";
    for (LL j = ls - 1; j >= 0; j--) {
      for (char k = 'a'; k <= 'z'; k++) {
        if (cnt[k]) {
          t += k;
          if (mp.find(t) != mp.end())
            ans++;
          t.pop_back();
        }
      }
      t += s[i][j];
      cnt[s[i][j]]--;
    }
  }
  cout << ans - n << '\\n';
  return 0;
}