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

#include <algorithm>
#include <iostream>
#include <string>
#include <string_view>
#include <vector>

using namespace std;

bool isPalindrome(const string &s) {
  if (s.empty())
    return false;

  for (int i = 0; i < s.size() / 2; i++)
    if (s[i] != s[s.size() - 1 - i])
      return false;
  return true;
}

vector<string> strings;
vector<string> reversedStrings;
vector<int> costs;

struct stringData {
  string s;
  bool isLeft;
  bool operator==(const stringData &other) {
    return (s == other.s) && (isLeft == other.isLeft);
  }
};

long long completePalindrome(const string &s, bool isLeft,
                             vector<stringData> visitedStrings) {
  if (isPalindrome(s))
    return 0;

  visitedStrings.push_back({s, isLeft});

  const vector<string> &complStrings = (isLeft) ? reversedStrings : strings;
  long long minCost;
  bool first = true;
  for (int i = 0; i < complStrings.size(); i++) {
    const string &complString = complStrings[i];
    int j;
    bool b = false;
    for (j = 0; j < min(s.size(), complString.size()); j++) {
      if (s[j] != complString[j]) {
        b = true;
        break;
      }
    }
    if (b)
      continue;
    if (s.size() == complString.size()) {
      if (first) {
        first = false;
        minCost = costs[i];
      } else {
        if (costs[i] < minCost)
          minCost = costs[i];
      }
    } else {
      long long val;
      string rest;
      bool newIsLeft;
      if (s.size() < complString.size()) {
        rest = complString.substr(s.size(), complString.size() - s.size());
        newIsLeft = !isLeft;
      } else // s.size() > complString.size()
      {
        rest = s.substr(complString.size(), s.size() - complString.size());
        newIsLeft = isLeft;
      }
      if (find(visitedStrings.begin(), visitedStrings.end(),
               stringData({rest, newIsLeft})) != visitedStrings.end())
        continue;
      val = completePalindrome(rest, newIsLeft, visitedStrings);
      if (val == -1)
        continue;
      if (first) {
        first = false;
        minCost = costs[i] + val;
      } else {
        if (costs[i] + val < minCost)
          minCost = costs[i] + val;
      }
    }
  }
  if (!first)
    return minCost;
  else
    return -1;
}

int main() {
  int n;
  cin >> n;
  strings.resize(n);
  reversedStrings.resize(n);
  costs.resize(n);

  for (int i = 0; i < n; i++) {
    cin >> strings[i] >> costs[i];
    reversedStrings[i] = strings[i];
    reverse(reversedStrings[i].begin(), reversedStrings[i].end());
  }

  cout << completePalindrome("", false, {}) << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <string_view>
#include <vector>

using namespace std;

bool isPalindrome(const string &s) {
  if (s.empty())
    return false;

  for (int i = 0; i < s.size() / 2; i++)
    if (s[i] != s[s.size() - 1 - i])
      return false;
  return true;
}

long long completePalindrome(const string &s, bool isLeft,
                             const vector<string> &strings,
                             const vector<string> &reversedStrings,
                             const vector<int> &costs) {
  if (isPalindrome(s))
    return 0;

  const vector<string> &complStrings = (isLeft) ? reversedStrings : strings;
  long long minCost;
  bool first = true;
  for (int i = 0; i < complStrings.size(); i++) {
    const string &complString = complStrings[i];
    int j;
    bool b = false;
    for (j = 0; j < min(s.size(), complString.size()); j++) {
      if (s[j] != complString[j]) {
        b = true;
        break;
      }
    }
    if (b)
      continue;
    if (s.size() == complString.size()) {
      if (first) {
        first = false;
        minCost = costs[i];
      } else {
        if (costs[i] < minCost)
          minCost = costs[i];
      }
    } else {
      long long val;
      if (s.size() < complString.size())
        val = completePalindrome(
            complString.substr(s.size(), complString.size() - s.size()),
            !isLeft, strings, reversedStrings, costs);
      else // s.size() > complString.size()
        val = completePalindrome(
            s.substr(complString.size(), s.size() - complString.size()), isLeft,
            strings, reversedStrings, costs);
      if (val == -1)
        continue;
      if (first) {
        first = false;
        minCost = costs[i] + val;
      } else {
        if (costs[i] + val < minCost)
          minCost = costs[i] + val;
      }
    }
  }
  if (!first)
    return minCost;
  else
    return -1;
}

int main() {
  int n;
  cin >> n;
  vector<string> strings(n);
  vector<string> reversedStrings(n);
  vector<int> costs(n);
  for (int i = 0; i < n; i++) {
    cin >> strings[i] >> costs[i];
    reversedStrings[i] = strings[i];
    reverse(reversedStrings[i].begin(), reversedStrings[i].end());
  }

  cout << completePalindrome("", false, strings, reversedStrings, costs)
       << endl;
  return 0;
}