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

#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

template <typename T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
template <typename T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}

const int N = 60;
const ll INF = 1e17;
int n;
ll c[N];
string snml[N], srev[N];
map<pair<string, bool>, ll> memo;

ll calc(string s, bool rev);

ll calc0(string s, bool rev) {
  if (s.empty())
    return 0;
  string t(s);
  reverse(ALL(t));
  if (s == t)
    return 0;
  ll ret = INF;
  REP(i, n) {
    string t;
    if (!rev)
      t = srev[i];
    else
      t = snml[i];
    if (s == t) {
      chmin(ret, c[i]);
      continue;
    }
    bool ok = true;
    REP(j, min(s.length(), t.length())) {
      if (s[j] != t[j]) {
        ok = false;
        break;
      }
    }
    if (!ok)
      continue;
    if (s.length() > t.length()) {
      string next = s.substr(t.length());
      ll d = calc(next, rev) + c[i];
      chmin(ret, d);
    } else {
      string next = t.substr(s.length());
      ll d = calc(next, !rev) + c[i];
      chmin(ret, d);
    }
  }
  return ret;
}

ll calc(string s, bool rev) {
  auto tmp = make_pair(s, rev);
  if (memo.count(tmp))
    return memo[tmp];
  memo[tmp] = INF;
  return memo[tmp] = calc0(s, rev);
}

int main(void) {
  cin >> n;
  REP(i, n) {
    cin >> snml[i] >> c[i];
    srev[i] = snml[i];
    reverse(ALL(srev[i]));
  }
  ll ans = INF;
  REP(i, n) {
    ll x = c[i] + calc(snml[i], false);
    chmin(ans, x);
  }
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

template <typename T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
template <typename T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}

const int N = 50;
const ll INF = 1e17;
int n;
ll c[N];
string snml[N], srev[N];
map<pair<string, bool>, ll> memo;

ll calc(string s, bool rev);

ll calc0(string s, bool rev) {
  string t(s);
  reverse(ALL(t));
  if (s == t)
    return 0;
  ll ret = INF;
  REP(i, n) {
    string t;
    if (!rev)
      t = srev[i];
    else
      t = snml[i];
    if (s == t) {
      chmin(ret, c[i]);
      continue;
    }
    bool ok = true;
    REP(j, min(s.length(), t.length())) {
      if (s[j] != t[j]) {
        ok = false;
        break;
      }
    }
    if (!ok)
      continue;
    if (s.length() >= t.length()) {
      string next = s.substr((int)t.length());
      ll d = calc(next, rev) + c[i];
      chmin(ret, d);
    } else {
      string next = t.substr(s.length());
      ll d = calc(next, !rev) + c[i];
      chmin(ret, d);
    }
  }
  return ret;
}

ll calc(string s, bool rev) {
  auto tmp = make_pair(s, rev);
  if (memo.count(tmp))
    return memo[tmp];
  return memo[tmp] = calc0(s, rev);
}

int main(void) {
  cin >> n;
  REP(i, n) {
    cin >> snml[i] >> c[i];
    srev[i] = snml[i];
    reverse(ALL(srev[i]));
  }
  ll ans = INF;
  REP(i, n) {
    ll x = c[i] + calc(snml[i], false);
    chmin(ans, x);
  }
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
  return 0;
}