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

#include <climits>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

map<string, int> memo;
string start, goal;
vector<string> a, b;
int ans;

string msed(string s, string a, string b) {
  string ret = "";
  int n = a.size();
  int p = 0;
  for (int i = 0; i < (int)s.size() - n + 1; i++) {
    if (s.substr(i, n) == a) {
      ret += b;
      p = i + n;
      i += n - 1;
    } else {
      ret += s[i];
      p = i + 1;
    }
  }
  for (; p < s.size(); p++)
    ret += s[p];
  return ret;
}

void rec(string s, int cnt) {
  if (s.size() > goal.size())
    return;
  if (cnt > 5)
    return;
  memo[s] = cnt;
  // if (s == goal) {
  //   cout << s << endl;
  //   cout << cnt << endl;
  // }
  // for (int i = 0; i < cnt; i++) cout << " ";
  // cout << s << endl;
  // cout << s << " " << cnt << " " << memo.size() << endl;
  // if (s == "aadeeeec") cout << s << " " << cnt << endl;
  for (int i = 0; i < a.size(); i++) {
    string ns = msed(s, a[i], b[i]);
    if (ns != s)
      rec(ns, cnt + 1);
  }
}

void rev(string s, int cnt) {
  if (s.size() < start.size())
    return;
  if (cnt > 5)
    return;
  if (memo.find(s) != memo.end()) {
    ans = min(ans, memo[s] + cnt);
  }
  if (s.size() == start.size())
    return;
  // if (s == "adeeeeeec") cout << s << " " << cnt << endl;
  // cout << s << endl;
  for (int i = 0; i < b.size(); i++) {
    string ns = msed(s, b[i], a[i]);
    // if (ns == "adeeeeeec") cout << ns << " " << cnt << endl;
    if (ns != s) {
      string ts = msed(ns, a[i], b[i]);
      // if (ns == "adeeeeeec") cout << ts << endl;
      if (s == ts)
        rev(ns, cnt + 1);
    }
  }
}

int main() {
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    map<string, int> empmap;
    memo = empmap;
    vector<string> empvec;
    a = empvec;
    b = empvec;
    ans = INT_MAX;
    for (int i = 0; i < n; i++) {
      string t1, t2;
      cin >> t1 >> t2;
      a.push_back(t1);
      b.push_back(t2);
    }
    cin >> start >> goal;
    // cout << "ok0" << endl;
    rec(start, 0);
    // cout << "ok1" << endl;
    rev(goal, 0);
    // cout << "ok2" << endl;
    if (ans == INT_MAX) {
      cout << -1 << endl;
    } else {
      cout << ans << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <climits>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

map<string, int> memo;
string start, goal;
vector<string> a, b;
int ans;

string msed(string s, string a, string b) {
  string ret = "";
  int n = a.size();
  int p = 0;
  for (int i = 0; i < s.size() - n + 1; i++) {
    if (s.substr(i, n) == a) {
      ret += b;
      p = i + n;
      i += n - 1;
    } else {
      ret += s[i];
      p = i + 1;
    }
  }
  for (; p < s.size(); p++)
    ret += s[p];
  return ret;
}

void rec(string s, int cnt) {
  if (s.size() > goal.size())
    return;
  if (cnt > 5)
    return;
  memo[s] = cnt;
  // if (s == goal) {
  //   cout << s << endl;
  //   cout << cnt << endl;
  // }
  // for (int i = 0; i < cnt; i++) cout << " ";
  // cout << s << endl;
  // cout << s << endl;
  // if (s == "aadeeeec") cout << s << " " << cnt << endl;
  for (int i = 0; i < a.size(); i++) {
    string ns = msed(s, a[i], b[i]);
    if (ns != s)
      rec(ns, cnt + 1);
  }
}

void rev(string s, int cnt) {
  if (s.size() < start.size())
    return;
  if (cnt > 5)
    return;
  if (memo.find(s) != memo.end()) {
    ans = min(ans, memo[s] + cnt);
  }
  if (s.size() == start.size())
    return;
  // if (s == "adeeeeeec") cout << s << " " << cnt << endl;
  // cout << s << endl;
  for (int i = 0; i < b.size(); i++) {
    string ns = msed(s, b[i], a[i]);
    // if (ns == "adeeeeeec") cout << ns << " " << cnt << endl;
    if (ns != s) {
      string ts = msed(ns, a[i], b[i]);
      // if (ns == "adeeeeeec") cout << ts << endl;
      if (s == ts)
        rev(ns, cnt + 1);
    }
  }
}

int main() {
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    map<string, int> empmap;
    memo = empmap;
    vector<string> empvec;
    a = empvec;
    b = empvec;
    ans = INT_MAX;
    for (int i = 0; i < n; i++) {
      string t1, t2;
      cin >> t1 >> t2;
      a.push_back(t1);
      b.push_back(t2);
    }
    cin >> start >> goal;

    rec(start, 0);
    rev(goal, 0);
    if (ans == INT_MAX) {
      cout << -1 << endl;
    } else {
      cout << ans << endl;
    }
  }
}