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

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < n; i++)
#define repn(i, n) for (int i = 1; i <= n; i++)
#define LL long long
#define pii pair<LL, LL>
#define fi first
#define se second
#define pb push_back
#define mpr make_pair
#define ret dp[a.fi][a.se][b.fi][b.se]

using namespace std;

const LL MOD = 1e9 + 7;

LL n, c[60], dp[60][30][60][30], ans = 1e18;
string s[60];
bool vis[60][30][60][30];

bool isp(LL k, LL lb, LL ub) {
  for (LL i = lb, j = ub; i <= j; i++, j--)
    if (s[k][i] != s[k][j])
      return false;
  return true;
}

string getstr(LL k, LL lb, LL ub) {
  string res = "";
  for (int i = lb; i <= ub; i++)
    res.pb(s[k][i]);
  return res;
}

string getstrr(LL k, LL lb, LL ub) {
  string res = "";
  for (int i = lb; i <= ub; i++)
    res.pb(s[k][i]);
  reverse(res.begin(), res.end());
  return res;
}

LL dfs(pii a, pii b) {
  // cout<<a.fi<<' '<<a.se<<' '<<b.fi<<' '<<b.se<<endl;
  if (ret != 1e18)
    return ret;
  if (vis[a.fi][a.se][b.fi][b.se])
    return 1e18;
  vis[a.fi][a.se][b.fi][b.se] = true;
  if (b.fi == 0) {
    repn(i, n) {
      if (s[i].size() >= a.se) {
        if (getstr(a.fi, s[a.fi].size() - a.se, s[a.fi].size() - 1) !=
            getstrr(i, s[i].size() - a.se, s[i].size() - 1))
          continue;
        if (isp(i, 0, s[i].size() - a.se - 1))
          ret = min(ret, c[i]);
        else
          ret = min(ret, dfs(mpr(0, 0), mpr(i, s[i].size() - a.se)) + c[i]);
      } else {
        if (getstr(a.fi, s[a.fi].size() - a.se,
                   s[a.fi].size() - a.se + s[i].size() - 1) !=
            getstrr(i, 0, s[i].size() - 1))
          continue;
        if (isp(a.fi, s[a.fi].size() - a.se + s[i].size(), s[a.fi].size() - 1))
          ret = min(ret, c[i]);
        else
          ret = min(ret, dfs(mpr(a.fi, a.se - s[i].size()), mpr(0, 0)) + c[i]);
      }
    }
  } else {
    repn(i, n) {
      if (s[i].size() >= b.se) {
        if (getstr(b.fi, 0, b.se - 1) != getstrr(i, 0, b.se - 1))
          continue;
        if (isp(i, b.se, s[i].size() - 1))
          ret = min(ret, c[i]);
        else
          ret = min(ret, dfs(mpr(i, s[i].size() - b.se), mpr(0, 0)) + c[i]);
      } else {
        if (getstr(b.fi, b.se - s[i].size(), b.se - 1) !=
            getstrr(i, 0, s[i].size() - 1))
          continue;
        if (isp(b.fi, 0, b.se - s[i].size() - 1))
          ret = min(ret, c[i]);
        else
          ret = min(ret, dfs(mpr(0, 0), mpr(b.fi, b.se - s[i].size())) + c[i]);
      }
    }
  }
  if (ret == 1e18)
    ret = 1e18 - 1;
  vis[a.fi][a.se][b.fi][b.se] = false;
  return ret;
}

int main() {
  cin >> n;
  repn(i, n) {
    cin >> s[i] >> c[i];
    if (isp(i, 0, s[i].size() - 1))
      ans = min(ans, c[i]);
  }
  rep(i, 55) rep(j, 25) rep(k, 55) rep(p, 25) dp[i][j][k][p] = 1e18;
  repn(i, n) ans = min(ans, dfs(mpr(i, s[i].size()), mpr(0LL, 0LL)) + c[i]);
  if (ans >= 1e17)
    puts("-1");
  else
    cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < n; i++)
#define repn(i, n) for (int i = 1; i <= n; i++)
#define LL long long
#define pii pair<LL, LL>
#define fi first
#define se second
#define pb push_back
#define mpr make_pair
#define ret dp[a.fi][a.se][b.fi][b.se]

using namespace std;

const LL MOD = 1e9 + 7;

LL n, c[60], dp[60][30][60][30], ans = 1e18;
string s[60];

bool isp(LL k, LL lb, LL ub) {
  for (LL i = lb, j = ub; i <= j; i++, j--)
    if (s[k][i] != s[k][j])
      return false;
  return true;
}

string getstr(LL k, LL lb, LL ub) {
  string res = "";
  for (int i = lb; i <= ub; i++)
    res.pb(s[k][i]);
  return res;
}

string getstrr(LL k, LL lb, LL ub) {
  string res = "";
  for (int i = lb; i <= ub; i++)
    res.pb(s[k][i]);
  reverse(res.begin(), res.end());
  return res;
}

LL dfs(pii a, pii b) {
  // cout<<a.fi<<' '<<a.se<<' '<<b.fi<<' '<<b.se<<endl;
  if (ret != 1e18)
    return ret;
  if (b.fi == 0) {
    repn(i, n) {
      if (s[i].size() >= a.se) {
        if (getstr(a.fi, s[a.fi].size() - a.se, s[a.fi].size() - 1) !=
            getstrr(i, s[i].size() - a.se, s[i].size() - 1))
          continue;
        if (isp(i, 0, s[i].size() - a.se - 1))
          ret = min(ret, c[i]);
        else
          ret = min(ret, dfs(mpr(0, 0), mpr(i, s[i].size() - a.se)) + c[i]);
      } else {
        if (getstr(a.fi, s[a.fi].size() - a.se,
                   s[a.fi].size() - a.se + s[i].size() - 1) !=
            getstrr(i, 0, s[i].size() - 1))
          continue;
        if (isp(a.fi, s[a.fi].size() - a.se + s[i].size(), s[a.fi].size() - 1))
          ret = min(ret, c[i]);
        else
          ret = min(ret, dfs(mpr(a.fi, a.se - s[i].size()), mpr(0, 0)) + c[i]);
      }
    }
  } else {
    repn(i, n) {
      if (s[i].size() >= b.se) {
        if (getstr(b.fi, 0, b.se - 1) != getstrr(i, 0, b.se - 1))
          continue;
        if (isp(i, b.se, s[i].size() - 1))
          ret = min(ret, c[i]);
        else
          ret = min(ret, dfs(mpr(i, s[i].size() - b.se), mpr(0, 0)) + c[i]);
      } else {
        if (getstr(b.fi, b.se - s[i].size() - 1, b.se - 1) !=
            getstrr(i, 0, s[i].size() - 1))
          continue;
        if (isp(b.fi, 0, b.se - s[i].size() - 1))
          ret = min(ret, c[i]);
        else
          ret = min(ret, dfs(mpr(0, 0), mpr(b.fi, b.se - s[i].size())) + c[i]);
      }
    }
  }
  if (ret == 1e18)
    ret = 1e18 - 1;
  return ret;
}

int main() {
  cin >> n;
  repn(i, n) {
    cin >> s[i] >> c[i];
    if (isp(i, 0, s[i].size() - 1))
      ans = min(ans, c[i]);
  }
  rep(i, 55) rep(j, 25) rep(k, 55) rep(p, 25) dp[i][j][k][p] = 1e18;
  repn(i, n) ans = min(ans, dfs(mpr(i, s[i].size()), mpr(0LL, 0LL)) + c[i]);
  if (ans >= 1e17)
    puts("-1");
  else
    cout << ans << endl;
  return 0;
}