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

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

//.define
// #define FILE_IN_OUT
#define RET(_x) return cout << (_x) << '\\n', 0;
#define all(_obj) (_obj).begin(), (_obj).end()
#define loop(_n) for (int i = 0; i < (_n); ++i)
#define sz(_obj) static_cast<int>((_obj).size())
#ifdef SHJ_LOCAL
#define debug(_x) std::cerr << (#_x) << " = " << (_x) << '\\n' << std::flush;
#else
#define debug(_x)                                                              \\
  {}
#endif
#define endl "\\n"
template <typename P, typename Q> P CeilDiv(P _dividend, Q _divider) {
  return static_cast<P>((_dividend - 1LL + _divider) / _divider);
}
template <typename Tp>
inline void outarr(Tp _begin, Tp _end, const char *_delim = " ") {
  for (Tp current = _begin; current != _end; ++current) {
    std::cout << *current << _delim;
  }
  std::cout << '\\n';
}

//.constant
using ll = long long;
using pii = std::pair<int, int>;
constexpr int INF = 0x3f3f3f3f;
constexpr int MOD = static_cast<const int>(1e9 + 7);

//.data
constexpr int MAXN = 50;

string arr[2][MAXN];
int cost[MAXN];
map<string, ll> dp[2];
set<string> used[2];

bool IsPalindrom(const string &s) {
  loop(sz(s) >> 1) {
    if (s[i] != s[sz(s) - 1 - i]) {
      return false;
    }
  }
  return true;
}

ll Do(const string &s, int rev, int n) {
  if (IsPalindrom(s)) {
    return 0;
  }
  auto it = dp[rev].find(s);
  if (it != dp[rev].end()) {
    return it->second;
  }
  if (!used[rev].insert(s).second) {
    return 1e18;
  }
  ll res = 1e18;
  for (int i = 0; i < n; ++i) {
    bool ok = true;
    for (int j = 0; j < min(sz(s), sz(arr[rev][i])); ++j) {
      if (s[j] != arr[rev][i][j]) {
        ok = false;
        break;
      }
    }
    if (!ok) {
      continue;
    }
    if (sz(s) < sz(arr[rev][i])) {
      res = min(res, cost[i] + Do(arr[rev][i].substr(sz(s)), rev ^ 1, n));
    } else {
      res = min(res, cost[i] + Do(s.substr(sz(arr[rev][i])), rev, n));
    }
  }
  used[rev].erase(s);
  dp[rev][s] = res;
  return res;
}

//.code
int main() {
#ifdef FILE_IN_OUT
  std::ifstream cin("input.txt", std::ios::in);
  std::ofstream cout("output.txt", std::ios::out);
#else
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
#endif

  int n;
  cin >> n;
  loop(n) {
    cin >> arr[0][i] >> cost[i];
    arr[1][i] = arr[0][i];
    reverse(all(arr[1][i]));
  }
  ll ans = 1e18;
  loop(n) {
    used[0].clear();
    used[1].clear();
    ans = min(ans, cost[i] + Do(arr[0][i], 1, n));
  }
  if (ans == (ll)1e18) {
    RET(-1);
  }
  cout << ans << endl;

#ifdef FILE_IN_OUT
  cin.close();
  cout.close();
#endif
  return 0;
}

Error: 0

Fixed submission:

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

//.define
// #define FILE_IN_OUT
#define RET(_x) return cout << (_x) << '\\n', 0;
#define all(_obj) (_obj).begin(), (_obj).end()
#define loop(_n) for (int i = 0; i < (_n); ++i)
#define sz(_obj) static_cast<int>((_obj).size())
#ifdef SHJ_LOCAL
#define debug(_x) std::cerr << (#_x) << " = " << (_x) << '\\n' << std::flush;
#else
#define debug(_x)                                                              \\
  {}
#endif
#define endl "\\n"
template <typename P, typename Q> P CeilDiv(P _dividend, Q _divider) {
  return static_cast<P>((_dividend - 1LL + _divider) / _divider);
}
template <typename Tp>
inline void outarr(Tp _begin, Tp _end, const char *_delim = " ") {
  for (Tp current = _begin; current != _end; ++current) {
    std::cout << *current << _delim;
  }
  std::cout << '\\n';
}

//.constant
using ll = long long;
using pii = std::pair<int, int>;
constexpr int INF = 0x3f3f3f3f;
constexpr int MOD = static_cast<const int>(1e9 + 7);

//.data
constexpr int MAXN = 50;

string arr[2][MAXN];
int cost[MAXN];

bool IsPalindrom(const string &s) {
  loop(sz(s) >> 1) {
    if (s[i] != s[sz(s) - 1 - i]) {
      return false;
    }
  }
  return true;
}

ll g_ans = 1e18;
void Do(const string &s, int rev, int n, ll cur_cost) {
  if (IsPalindrom(s)) {
    g_ans = min(g_ans, cur_cost);
    return;
  }
  for (int i = 0; i < n; ++i) {
    bool ok = true;
    for (int j = 0; j < min(sz(s), sz(arr[rev][i])); ++j) {
      if (s[j] != arr[rev][i][j]) {
        ok = false;
        break;
      }
    }
    if (!ok) {
      continue;
    }
    if (sz(s) < sz(arr[rev][i])) {
      auto t = arr[rev][i].substr(sz(s));
      if (rev == 1) {
        reverse(all(t));
      }
      Do(t, rev ^ 1, n, cur_cost + cost[i]);
    } else {
      Do(s.substr(sz(arr[rev][i])), rev, n, cur_cost + cost[i]);
    }
  }
}

//.code
int main() {
#ifdef FILE_IN_OUT
  std::ifstream cin("input.txt", std::ios::in);
  std::ofstream cout("output.txt", std::ios::out);
#else
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
#endif

  int n;
  cin >> n;
  loop(n) {
    cin >> arr[0][i] >> cost[i];
    arr[1][i] = arr[0][i];
    reverse(all(arr[1][i]));
  }
  loop(n) { Do(arr[0][i], 1, n, cost[i]); }
  if (g_ans == (ll)1e18) {
    RET(-1);
  }
  cout << g_ans << endl;

#ifdef FILE_IN_OUT
  cin.close();
  cout.close();
#endif
  return 0;
}