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

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAX = 200002;
const int MOD = 1000000007;
string sx[50], sy[50], q2;
int c[50];
int n, ans = MOD * MOD, ans2;
bool z[2][50][50];
void dfs(int a, bool x, int b) {
  if (z[x][a][b])
    return;
  z[x][a][b] = true;
  if (x) {
    q2 = sx[a].substr(b);
    reverse(q2.begin(), q2.end());
    if (sx[a].substr(b) == q2) {
      ans = min(ans2, ans);
      z[x][a][b] = false;
      return;
    }

    for (int i = 0; i < n; i++) {
      if (sx[a].size() - b <= sy[i].size() &&
          equal(begin(sx[a]) + b, end(sx[a]), begin(sy[i]))) {
        ans2 += c[i];
        dfs(i, false, sx[a].size() - b);

        ans2 -= c[i];
      }
      if (sy[i].size() <= sx[a].size() - b &&
          equal(begin(sy[i]), end(sy[i]), begin(sx[a]) + b)) {
        ans2 += c[i];
        dfs(a, true, b + sy[i].size());

        ans2 -= c[i];
      }
    }

  } else {
    q2 = sy[a].substr(b);
    reverse(q2.begin(), q2.end());
    if (sy[a].substr(b) == q2) {
      ans = min(ans2, ans);
      z[x][a][b] = false;
      return;
    }
    for (int i = 0; i < n; i++) {
      if (sy[a].size() - b <= sx[i].size() &&
          equal(begin(sy[a]) + b, end(sy[a]), begin(sx[i]))) {
        ans2 += c[i];
        dfs(i, true, sy[a].size() - b);

        ans2 -= c[i];
      }
      if (sx[i].size() <= sy[a].size() - b &&
          equal(begin(sx[i]), end(sx[i]), begin(sy[a]) + b)) {
        ans2 += c[i];
        dfs(a, false, b + sx[i].size());

        ans2 -= c[i];
      }
    }
  }
  z[x][a][b] = false;
}

signed main() {

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> sx[i] >> c[i];
    sy[i] = sx[i];
    reverse(sy[i].begin(), sy[i].end());
  }
  for (int i = 0; i < n; i++) {

    ans2 = c[i];
    dfs(i, true, 0);

    ans2 = c[i];
    dfs(i, false, 0);
  }
  if (ans != MOD * MOD)
    cout << ans << endl;
  else
    cout << -1 << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAX = 200002;
const int MOD = 1000000007;
string sx[50], sy[50], q2;
int c[50];
int n, ans = MOD * MOD, ans2;
void dfs(bool x, string q) {
  q2 = q;
  reverse(q2.begin(), q2.end());
  if (q == q2) {
    ans = min(ans2, ans);

    return;
  }
  if (x) {
    for (int i = 0; i < n; i++) {
      if (q.size() <= sy[i].size() &&
          equal(begin(q), end(q), begin(sy[i]), begin(sy[i]) + q.size())) {
        ans2 += c[i];
        dfs(false, sy[i].substr(q.size()));

        ans2 -= c[i];
      }
    }

  } else {
    for (int i = 0; i < n; i++) {
      if (q.size() <= sx[i].size() &&
          equal(begin(q), end(q), begin(sx[i]), begin(sx[i]) + q.size())) {
        ans2 += c[i];
        dfs(true, sx[i].substr(q.size()));

        ans2 -= c[i];
      }
    }
  }
}

signed main() {

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> sx[i] >> c[i];
    sy[i] = sx[i];
    reverse(sy[i].begin(), sy[i].end());
  }
  for (int i = 0; i < n; i++) {
    ans2 = c[i];
    dfs(true, sx[i]);

    ans2 = c[i];
    dfs(false, sy[i]);
  }
  if (ans != MOD * MOD)
    cout << ans << endl;
  else
    cout << -1 << endl;
}