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

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

using ll = long long;
#define endl '\\n'

template <typename A, typename B> string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string &s) { return '"' + s + '"'; }

string to_string(const char c) { return string(1, c); }

string to_string(const char *s) { return to_string((string)s); }

string to_string(bool b) { return (b ? "true" : "false"); }

string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template <size_t N> string to_string(bitset<N> v) {
  string res = "";
  for (int i = int(N) - 1; i >= 0; --i) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}

template <typename A> string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#ifdef COMPETITIVE_PROGRAMMING_LOCAL
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define dbg(...)                                                               \\
  if (0) {                                                                     \\
  }
#endif

constexpr ll INF = INT64_MAX / 8;

bool IsPal(string &s) {
  int ns = s.length();
  for (int i = 0; i < ns / 2; ++i) {
    if (s[i] != s[ns - 1 - i])
      return false;
  }
  return true;
}

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

  ll N;
  cin >> N;
  vector<pair<string, ll>> A(N);
  for (auto &_a : A)
    cin >> _a.first >> _a.second;
  sort(A.begin(), A.end());
  for (int i = 1; i < N; ++i) {
    if (A[i].first == A[i - 1].first) {
      assert(A[i].second >= A[i - 1].second);
      A[i].second = A[i - 1].second;
    }
  }
  sort(A.begin(), A.end()); // TODO i don't think this is necessary
  A.resize(unique(A.begin(), A.end()) - A.begin());
  N = A.size();

  ll ans = INF;
  vector<bool> is_pal(N);
  for (int i = 0; i < N; ++i) {
    string s = A[i].first;
    reverse(s.begin(), s.end());
    if (s == A[i].first) {
      is_pal[i] = true;
      ans = min(ans, A[i].second);
    }
  }
  {
    int j = 0;
    for (int i = 0; i < N; ++i)
      if (!is_pal[i])
        A[j++] = A[i];
    A.resize(j);
    N = A.size();
  }

  map<pair<string, string>, ll> D;
  using tup = tuple<ll, string, string>;
  priority_queue<tup, vector<tup>, greater<tup>> pq;

  for (int i = 0; i < N; ++i) {
    if (A[i].second < ans)
      pq.push({A[i].second, A[i].first, ""});
    if (A[i].second < ans)
      pq.push({A[i].second, "", A[i].first});
  }
  // for (int i = 0; i < N; ++i) {
  //   if (A[i].second < ans) {
  //     string s = A[i].first;
  //     reverse(s.begin(), s.end());
  //     pq.push({A[i].second, "", s});
  //     D[{"",s}] = A[i].second;
  //   }
  // }

  while (pq.size()) {
    auto [d, prefix, suffix] = pq.top();
    pq.pop();
    // dbg(prefix,suffix, d);
    if ((prefix.empty() && IsPal(suffix)) ||
        (suffix.empty() && IsPal(prefix))) {
      ans = min(ans, d);
      break;
    }
    if (D.count({prefix, suffix}) && d > D[{prefix, suffix}])
      continue;
    if (prefix.length()) {
      for (int is = 0; is < N; ++is) {
        auto &[s, c] = A[is];
        if (d + c >= ans)
          continue;
        int ns = s.length();
        int nn = min(ns, int(prefix.length()));
        bool good = true;
        int ij = 0;
        for (; ij < nn; ++ij)
          if (prefix[ij] != s[ns - 1 - ij])
            break;
        if (ij != nn)
          continue;
        if (nn == ns) {
          string new_prefix = prefix.substr(ns);
          if (!D.count({new_prefix, ""}) || d + c < D[{new_prefix, ""}]) {
            D[{new_prefix, ""}] = d + c;
            pq.push({d + c, new_prefix, ""});
          }
        } else {
          string new_suffix = s.substr(0, ns - nn);
          if (!D.count({"", new_suffix}) || d + c < D[{"", new_suffix}]) {
            D[{"", new_suffix}] = d + c;
            pq.push({d + c, "", new_suffix});
          }
        }
      }
    } else {
      for (int is = 0; is < N; ++is) {
        auto &[s, c] = A[is];
        if (d + c >= ans)
          continue;
        // int ns = s.length();
        int n_suf = suffix.length();

        int nn = min(int(s.length()), n_suf);
        bool good = true;
        int ij = 0;
        for (; ij < nn; ++ij)
          if (suffix[n_suf - 1 - ij] != s[ij])
            break;
        if (ij != nn)
          continue;
        if (nn == n_suf) {
          string new_prefix = s.substr(n_suf);
          if (!D.count({new_prefix, ""}) || d + c < D[{new_prefix, ""}]) {
            D[{new_prefix, ""}] = d + c;
            pq.push({d + c, new_prefix, ""});
          }
        } else {
          string new_suffix = suffix.substr(0, n_suf - nn);
          if (!D.count({"", new_suffix}) || d + c < D[{"", new_suffix}]) {
            D[{"", new_suffix}] = d + c;
            pq.push({d + c, "", new_suffix});
          }
        }
      }
    }
  }
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
}


Error: 0

Fixed submission:

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

using ll = long long;
constexpr ll mod = 1e9 + 7;
#define endl '\\n'

template <typename A, typename B> string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string &s) { return '"' + s + '"'; }

string to_string(const char c) { return string(1, c); }

string to_string(const char *s) { return to_string((string)s); }

string to_string(bool b) { return (b ? "true" : "false"); }

string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template <size_t N> string to_string(bitset<N> v) {
  string res = "";
  for (int i = int(N) - 1; i >= 0; --i) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}

template <typename A> string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}

#ifdef COMPETITIVE_PROGRAMMING_LOCAL
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define dbg(...)                                                               \\
  if (0) {                                                                     \\
  }
#endif

constexpr ll INF = INT64_MAX / 8;

class Trie {
public:
  bool exists = false;
  bool is_end_pal = false;
  bool is_start_pal = false;
  ll start_pal_C = INF;
  ll end_pal_C = INF;
  int idx = -1;
  Trie *next[26] = {};
  ll min_C = INF;
  ll C = INF;

  void Insert(string &s, ll _C, ll _idx, vector<bool> &_is_start_pal,
              vector<bool> &_is_end_pal, bool is_reverse) {
    Trie *cur = this;
    cur->min_C = min(cur->min_C, _C);
    int n = s.length();
    assert(_is_start_pal.size() == n);
    assert(_is_end_pal.size() == n);
    for (int i = 0; i < n; ++i) {
      if (!cur->next[s[i] - 'a'])
        cur->next[s[i] - 'a'] = new Trie();
      cur = cur->next[s[i] - 'a'];
      cur->min_C = min(cur->min_C, _C);
      if (is_reverse) {
        if (n - 2 - i >= 0 && _is_start_pal[n - 2 - i]) {
          cur->is_start_pal = true;
          cur->start_pal_C = min(cur->start_pal_C, _C);
        }
      } else {
        if (i + 1 < n && _is_end_pal[i + 1]) {
          cur->is_end_pal = true;
          cur->end_pal_C = min(cur->end_pal_C, _C);
        }
      }
    }
    cur->exists = true;
    cur->idx = _idx;
    assert(cur->C == INF);
    cur->C = _C;
  }
};

void Search(Trie *trie, Trie *rtrie, Trie *orig_trie, Trie *orig_rtrie, ll &ans,
            ll cost, string &first, int n_char) {
  if (n_char > 20 * 50 * 2)
    return;
  if (!trie || !rtrie)
    return;
  if (cost + trie->min_C + rtrie->min_C >= ans)
    return;
  for (int c = 0; c < 26; ++c) {
    Trie *t_nxt = trie->next[c];
    Trie *r_nxt = rtrie->next[c];
    if (t_nxt && r_nxt) {
      first += c + 'a';
      dbg(first);

      if (t_nxt->exists && r_nxt->exists) {
        ans = min(ans, cost + t_nxt->C + r_nxt->C);
        continue;
      }
      if (t_nxt->exists) {
        if (r_nxt->is_start_pal) {
          ans = min(ans, cost + r_nxt->start_pal_C + t_nxt->C);
        } else {
          Search(orig_trie, r_nxt, orig_trie, orig_rtrie, ans, cost + t_nxt->C,
                 first, n_char + 1);
        }
      }
      if (r_nxt->exists) {
        //        dbg("rtrie exists; trie end_pal:",
        //        t_nxt->next[2]->is_start_pal);
        if (t_nxt->is_end_pal) {
          dbg(c, ans, cost, t_nxt->end_pal_C, r_nxt->C);
          ans = min(ans, cost + t_nxt->end_pal_C + r_nxt->C);
        } else {
          Search(t_nxt, orig_rtrie, orig_trie, orig_rtrie, ans, cost + r_nxt->C,
                 first, n_char + 1);
        }
      }
      Search(t_nxt, r_nxt, orig_trie, orig_rtrie, ans, cost, first, n_char + 1);
      first.pop_back();
    }
  }
}

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

  ll N;
  cin >> N;
  vector<pair<string, ll>> A(N);
  for (auto &_a : A)
    cin >> _a.first >> _a.second;
  sort(A.begin(), A.end());
  for (int i = 1; i < N; ++i) {
    if (A[i].first == A[i - 1].first) {
      assert(A[i].second > A[i - 1].second);
      A[i].second = A[i - 1].second;
    }
  }
  sort(A.begin(), A.end()); // TODO i don't think this is necessary
  A.resize(unique(A.begin(), A.end()) - A.begin());
  N = A.size();

  ll ans = INF;
  vector<bool> is_pal(N);
  for (int i = 0; i < N; ++i) {
    string s = A[i].first;
    reverse(s.begin(), s.end());
    if (s == A[i].first) {
      is_pal[i] = true;
      ans = min(ans, A[i].second);
    }
  }
  int j = 0;
  for (int i = 0; i < N; ++i)
    if (!is_pal[i])
      A[j++] = A[i];
  A.resize(j);
  N = A.size();

  vector<vector<bool>> is_end_pal(N), is_start_pal(N);
  vector<int> d1, d2;
  for (int iw = 0; iw < N; ++iw) {
    string &w = A[iw].first;

    int n = w.length();
    is_end_pal[iw].resize(n);
    is_start_pal[iw].resize(n);
    d1.assign(n, 0);
    d2.assign(n, 0);

    for (int i = 0, l = 0, r = -1; i < n; ++i) {
      int k = i > r ? 1 : min(d1[l + r - i], r - i + 1);
      while (i >= k && i + k < n && w[i - k] == w[i + k]) {
        ++k;
      }
      d1[i] = k--;
      if (i + k > r) {
        r = i + k;
        l = i - k;
      }
    }
    for (int i = 0, l = 0, r = -1; i < n; ++i) {
      int k = i > r ? 0 : min(d2[l + r - i + 1], r - i + 1);
      while (i - k - 1 >= 0 && i + k < n && w[i - k - 1] == w[i + k])
        ++k;
      d2[i] = k--;
      if (i + k > r) {
        r = i + k;
        l = i - k - 1;
      }
    }

    for (int i = 0; i < n; ++i) {
      int n_left = n - i;
      is_end_pal[iw][i] = n_left & 1 ? d1[i + n_left / 2] == n_left / 2 + 1
                                     : d2[i + n_left / 2] == n_left / 2;
      is_start_pal[iw][i] =
          (i & 1 ? d2[i - i / 2] : d1[i - i / 2]) == i / 2 + 1;
    }
  }

  Trie trie, rtrie;
  for (int i = 0; i < N; ++i) {
    trie.Insert(A[i].first, A[i].second, i, is_start_pal[i], is_end_pal[i],
                false);

    reverse(A[i].first.begin(), A[i].first.end());
    rtrie.Insert(A[i].first, A[i].second, i, is_start_pal[i], is_end_pal[i],
                 true);
    reverse(A[i].first.begin(), A[i].first.end());
  }

  string cur;
  Search(&trie, &rtrie, &trie, &rtrie, ans, 0, cur, 0);
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
}