Problem p02589 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define all(v) (v).begin(), (v).end()

using ll = long long;
using dbl = long double;

void solve(bool);

int main() {
#ifdef YA
  auto s = clock();
  assert(freopen("input.txt", "r", stdin));
  // assert(freopen("output.txt", "w", stdout));
#else
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
#endif

  cout << fixed << setprecision(20);

  srand(time(0));
  solve(true);

#ifdef YA1
  while (true)
    solve(false);
#endif

#ifdef YA
  cout << endl
       << endl
       << "time = " << (clock() - s) / (double)CLOCKS_PER_SEC << endl;
#endif

  return 0;
}

const int DEBUG = 0;

const int maxs = 1e6 + 55;
const int maxn = 2e5 + 55;
const int inf = 1e9;
const int K = 26;

int n;
string s[maxn];
int pos[maxn][K];
ll ans;
int cnt[K];

void solve(const vector<int> &ids, int x) {
  if (ids.size() <= 1)
    return;
  memset(cnt, 0, sizeof cnt);
  for (int id : ids) {
    for (int j = 0; j < K; ++j) {
      if (pos[id][j] <= (int)s[id].size() - x) {
        ++cnt[j];
      }
    }
  }
  for (int id : ids) {
    if (x == (int)s[id].size()) {
      ans += cnt[s[id][(int)s[id].size() - x] - 'a'] - 1;
    }
  }
  vector<int> to_ids;
  for (int j = 0; j < K; ++j) {
    to_ids.clear();
    for (int id : ids) {
      if (x < (int)s[id].size() && (s[id][(int)s[id].size() - x] - 'a') == j) {
        to_ids.push_back(id);
      }
    }
    solve(to_ids, x + 1);
  }
}

void solve(bool __attribute__((unused)) read) {
  if (DEBUG) {
    n = 2;
  } else {
    cin >> n;
  }
  vector<int> vec;
  for (int i = 0; i < n; ++i) {
    vec.push_back(i);
    if (DEBUG) {
      s[i] = "";
      for (int j = 0; j < (int)5e5; ++j) {
        s[i] += 'a';
      }
    } else {
      cin >> s[i];
    }
    for (int j = 0; j < K; ++j) {
      pos[i][j] = inf;
    }
    for (int j = 0; j < (int)s[i].size(); ++j) {
      if (pos[i][s[i][j] - 'a'] == inf) {
        pos[i][s[i][j] - 'a'] = j;
      }
    }
  }
  ans = 0;
  solve(vec, 1);
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define all(v) (v).begin(), (v).end()

using ll = long long;
using dbl = long double;

void solve(bool);

int main() {
#ifdef YA
  auto s = clock();
  assert(freopen("input.txt", "r", stdin));
  // assert(freopen("output.txt", "w", stdout));
#else
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
#endif

  cout << fixed << setprecision(20);

  srand(time(0));
  solve(true);

#ifdef YA1
  while (true)
    solve(false);
#endif

#ifdef YA
  cout << endl
       << endl
       << "time = " << (clock() - s) / (double)CLOCKS_PER_SEC << endl;
#endif

  return 0;
}

const int maxs = 1e6 + 55;
const int maxn = 2e5 + 55;
const int inf = 1e9;
const int K = 26;

int n;
string s[maxn];
int pos[maxn][K];
ll ans;

void solve(const vector<int> &ids, int x) {
  if (ids.empty())
    return;
  int cnt[K];
  memset(cnt, 0, sizeof cnt);
  for (int id : ids) {
    for (int j = 0; j < K; ++j) {
      if (pos[id][j] <= (int)s[id].size() - x) {
        ++cnt[j];
      }
    }
  }
  for (int id : ids) {
    if (x == (int)s[id].size()) {
      ans += cnt[s[id][(int)s[id].size() - x] - 'a'] - 1;
    }
  }
  vector<int> to_ids;
  for (int j = 0; j < K; ++j) {
    to_ids.clear();
    if (cnt[j] == 0)
      continue;
    for (int id : ids) {
      if (x < (int)s[id].size() && (s[id][(int)s[id].size() - x] - 'a') == j) {
        to_ids.push_back(id);
      }
    }
    solve(to_ids, x + 1);
  }
}

void solve(bool __attribute__((unused)) read) {
  cin >> n;
  vector<int> vec;
  for (int i = 0; i < n; ++i) {
    vec.push_back(i);
    cin >> s[i];
    for (int j = 0; j < K; ++j) {
      pos[i][j] = inf;
    }
    for (int j = 0; j < (int)s[i].size(); ++j) {
      if (pos[i][s[i][j] - 'a'] == inf) {
        pos[i][s[i][j] - 'a'] = j;
      }
    }
  }
  ans = 0;
  solve(vec, 1);
  cout << ans << endl;
}