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

#include <algorithm>
#include <array>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define REP(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define FOR(i, a, b) for (ll i = (a); i < (ll)(b); ++i)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(v) ((int)v.size())
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <int MOD> struct Fp {
  ll val;
  constexpr Fp(ll v = 0) noexcept : val(v % MOD) {
    if (val < 0)
      val += MOD;
  }
  constexpr int getmod() { return MOD; }
  constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; }
  constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; }
  constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; }
  constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; }
  constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; }
  constexpr Fp &operator+=(const Fp &r) noexcept {
    val += r.val;
    if (val >= MOD)
      val -= MOD;
    return *this;
  }
  constexpr Fp &operator-=(const Fp &r) noexcept {
    val -= r.val;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr Fp &operator*=(const Fp &r) noexcept {
    val = val * r.val % MOD;
    return *this;
  }
  constexpr Fp &operator/=(const Fp &r) noexcept {
    ll a = r.val, b = MOD, u = 1, v = 0;
    while (b) {
      ll t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    val = val * u % MOD;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr bool operator==(const Fp &r) const noexcept {
    return this->val == r.val;
  }
  constexpr bool operator!=(const Fp &r) const noexcept {
    return this->val != r.val;
  }
  friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept {
    return os << x.val;
  }
  friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, ll n) noexcept {
    if (n == 0)
      return 1;
    auto t = modpow(a, n / 2);
    t = t * t;
    if (n & 1)
      t = t * a;
    return t;
  }
};
constexpr ll MOD = 1e9 + 7;
using mint = Fp<MOD>;

#define pb push_back
#define mp make_pair
#define mt make_tuple

ll solve_fast(int N, vector<string> Ss) {
  map<string, vector<char>> counter;

  map<int, set<string>> srev_set_with_len;

  set<string> srev_set;
  for (auto s : Ss) {
    reverse(ALL(s));
    srev_set.insert(s);
    srev_set_with_len[SIZE(s)].insert(s);
  }

  ll ans = 0;
  for (auto s : Ss) {
    // cout << "s: " << s << endl;
    if (SIZE(s) == 1)
      continue;
    set<char> cnt_set;

    REP(i, 2) { cnt_set.insert(s[i]); }
    int i = 2;

    string s_rev = s;
    reverse(ALL(s_rev));
    s_rev = s_rev.substr(0, SIZE(s_rev) - 2);

    while (1) {
      int tmp_size = SIZE(s_rev) + 1;
      if (SIZE(srev_set_with_len[tmp_size]) != 0) {
        for (auto ch : cnt_set) {
          if (srev_set_with_len[tmp_size].count(s_rev + ch)) {
            ans += 1;
          }
        }
      }

      if (i == SIZE(s))
        break;
      cnt_set.insert(s[i]);
      i += 1;
      s_rev.pop_back();
      // cout << "i = " << i << ", size(cntset) = " << SIZE(cnt_set) << endl;
    }
  }
  return ans;

  // ll ans = 0;
  // for(auto s: Ss) {
  //     string subs = s.substr(1);
  //     // cout << "subs = " << subs << endl;
  //     if (!counter.count(subs)) continue;
  //     auto& ctr = counter[subs];
  //     auto& val = ctr[s[0] - 'a'];
  //     // cout << s << ", " << int(val) << endl;
  //     ans += val;
  // }
  // return ans;
}

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  // while(1){
  //     int N = 10;
  //     vector<string> Ss(N);
  //     REP(n, N) {
  //         string s = "";
  //         REP(i, 1 + rand() % 5) {
  //             s += 'a' + rand() % 26;
  //         }
  //         Ss[n] = s;
  //     }
  //     cout << solve_fast(N, Ss) << endl;
  // }

  int N;
  cin >> N;

  vector<string> Ss(N);
  REP(n, N) cin >> Ss[n];

  auto mine = solve_fast(N, Ss);
  cout << mine << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define REP(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define FOR(i, a, b) for (ll i = (a); i < (ll)(b); ++i)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(v) ((int)v.size())
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <int MOD> struct Fp {
  ll val;
  constexpr Fp(ll v = 0) noexcept : val(v % MOD) {
    if (val < 0)
      val += MOD;
  }
  constexpr int getmod() { return MOD; }
  constexpr Fp operator-() const noexcept { return val ? MOD - val : 0; }
  constexpr Fp operator+(const Fp &r) const noexcept { return Fp(*this) += r; }
  constexpr Fp operator-(const Fp &r) const noexcept { return Fp(*this) -= r; }
  constexpr Fp operator*(const Fp &r) const noexcept { return Fp(*this) *= r; }
  constexpr Fp operator/(const Fp &r) const noexcept { return Fp(*this) /= r; }
  constexpr Fp &operator+=(const Fp &r) noexcept {
    val += r.val;
    if (val >= MOD)
      val -= MOD;
    return *this;
  }
  constexpr Fp &operator-=(const Fp &r) noexcept {
    val -= r.val;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr Fp &operator*=(const Fp &r) noexcept {
    val = val * r.val % MOD;
    return *this;
  }
  constexpr Fp &operator/=(const Fp &r) noexcept {
    ll a = r.val, b = MOD, u = 1, v = 0;
    while (b) {
      ll t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    val = val * u % MOD;
    if (val < 0)
      val += MOD;
    return *this;
  }
  constexpr bool operator==(const Fp &r) const noexcept {
    return this->val == r.val;
  }
  constexpr bool operator!=(const Fp &r) const noexcept {
    return this->val != r.val;
  }
  friend constexpr ostream &operator<<(ostream &os, const Fp<MOD> &x) noexcept {
    return os << x.val;
  }
  friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, ll n) noexcept {
    if (n == 0)
      return 1;
    auto t = modpow(a, n / 2);
    t = t * t;
    if (n & 1)
      t = t * a;
    return t;
  }
};
constexpr ll MOD = 1e9 + 7;
using mint = Fp<MOD>;

#define pb push_back
#define mp make_pair
#define mt make_tuple

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

  int N;
  cin >> N;

  vector<string> Ss(N);
  REP(n, N) cin >> Ss[n];

  map<string, vector<char>> counter;

  for (auto s : Ss) {
    if (SIZE(s) == 1)
      continue;
    vector<char> cnt(26);

    REP(i, 2) cnt[s[i] - 'a']++;
    int i = 2;
    while (1) {
      string subs = s.substr(i);
      // cout << subs << ",";
      // REP(j, 26) cout << int(cnt[j]) << " ";
      // cout << endl;

      if (counter.count(subs)) {
        auto &target = counter[subs];
        REP(j, 26) target[j] += cnt[j];
      } else {
        counter[subs] = cnt;
      }

      if (i == SIZE(s))
        break;
      cnt[s[i] - 'a']++;
      i += 1;
    }
  }

  ll ans = 0;
  for (auto s : Ss) {
    string subs = s.substr(1);
    // cout << "subs = " << subs << endl;
    if (!counter.count(subs))
      continue;
    auto &ctr = counter[subs];
    auto &val = ctr[s[0] - 'a'];
    // cout << s << ", " << int(val) << endl;
    ans += val;
  }
  cout << ans << endl;
  return 0;
}