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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define FOR(i, a, b) for (int i = (int)a; i <= (int)b; i++)
#define DEC(i, a, b) for (int i = (int)a; i >= (int)b; i--)
typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef pair<pi, pi> pipi;
#define f first
#define s second
typedef vector<int> vi;
typedef vector<pi> vpi;
typedef vector<pii> vpii;
#define pb push_back
#define pf push_front
#define all(v) v.begin(), v.end()
#define disc(v)                                                                \\
  sort(all(v));                                                                \\
  v.resize(unique(all(v)) - v.begin());
#define INF (int)1e9 + 100
#define LLINF (ll)1e18
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
mt19937
    rng(chrono::steady_clock::now()
            .time_since_epoch()
            .count()); // can be used by calling rng() or shuffle(A, A+n, rng)
inline ll rand(ll x, ll y) {
  ++y;
  return (rng() % (y - x)) + x;
} // inclusivesss

ll n, cost[55], ans = LLINF;
string str[55][2];
map<string, ll> memo[2];

ll dp(string s, ll p) { // p = 0 means prefix
  string s2 = s;
  reverse(all(s2));
  if (s == s2)
    return 0ll;

  if (memo[p].count(s))
    return memo[p][s];
  memo[p][s] = LLINF;
  ll f = !p;

  FOR(i, 1, n) {
    bool ok = 1;
    FOR(j, 0, min(s.size(), str[i][f].size()) - 1)
    if (s[j] != str[i][f][j]) ok = 0;
    if (!ok)
      continue;

    if (s.size() > str[i][f].size()) {
      string nxt = "";
      FOR(j, str[i][f].size(), s.size() - 1) nxt += s[j];
      memo[p][s] = min(memo[p][s], dp(nxt, p) + cost[i]);
    } else {
      string nxt = "";
      FOR(j, s.size(), str[i][f].size() - 1) nxt += str[i][f][j];
      memo[p][s] = min(memo[p][s], dp(nxt, f) + cost[i]);
    }
  }
  return memo[p][s];
}

int main() {
  fastio;
  cin >> n;
  FOR(i, 1, n) {
    cin >> str[i][0] >> cost[i];
    str[i][1] = str[i][0];
    reverse(all(str[i][1]));
  }
  FOR(i, 1, n) ans = min(ans, dp(str[i][0], 0) + cost[i]);
  cout << (ans == LLINF ? -1 : ans);
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define FOR(i, a, b) for (int i = (int)a; i <= (int)b; i++)
#define DEC(i, a, b) for (int i = (int)a; i >= (int)b; i--)
typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef pair<pi, pi> pipi;
#define f first
#define s second
typedef vector<int> vi;
typedef vector<pi> vpi;
typedef vector<pii> vpii;
#define pb push_back
#define pf push_front
#define all(v) v.begin(), v.end()
#define disc(v)                                                                \\
  sort(all(v));                                                                \\
  v.resize(unique(all(v)) - v.begin());
#define INF (int)1e9 + 100
#define LLINF (ll)1e18
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
mt19937
    rng(chrono::steady_clock::now()
            .time_since_epoch()
            .count()); // can be used by calling rng() or shuffle(A, A+n, rng)
inline ll rand(ll x, ll y) {
  ++y;
  return (rng() % (y - x)) + x;
} // inclusivesss

ll n, cost[55], ans = LLINF;
string str[55][2];
map<string, ll> memo[2];

ll dp(string s, bool p) { // p = 0 means prefix
  string s2 = s;
  reverse(all(s2));
  if (s == s2)
    return 0;

  if (memo[p].find(s) != memo[p].end())
    return memo[p][s];
  ll res = LLINF;
  bool f = !p;

  FOR(i, 1, n) {
    bool ok = 1;
    FOR(j, 0, min(s.size(), str[i][f].size()) - 1)
    if (s[j] != str[i][f][j]) ok = 0;
    if (!ok)
      continue;

    if (s.size() > str[i][f].size()) {
      string nxt = "";
      FOR(j, str[i][f].size(), s.size() - 1) nxt += s[j];
      res = min(res, dp(nxt, p) + cost[i]);
    } else {
      string nxt = "";
      FOR(j, s.size(), str[i][f].size() - 1) nxt += str[i][f][j];
      res = min(res, dp(nxt, f) + cost[i]);
    }
  }
  return memo[p][s] = res;
}

int main() {
  fastio;
  cin >> n;
  FOR(i, 1, n) {
    cin >> str[i][0] >> cost[i];
    str[i][1] = str[i][0];
    reverse(all(str[i][1]));
  }
  FOR(i, 1, n) ans = min(ans, dp(str[i][0], 0) + cost[i]);
  cout << (ans == LLINF ? -1 : ans);
}