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

#include <bits/stdc++.h>
using namespace std;
#define int long long // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrep2(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()

typedef long long ll;
typedef pair<int, int> Pii;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;

vector<pair<string, string>> rules;
string s;
string g;
map<string, int> mp;

string getReplacedString(string t, pair<string, string> p) {
  string a, b;
  tie(a, b) = p;

  // cout << t << " " << a << " " << b << endl;

  int i = 0;
  while (i <= (int)t.size() - (int)a.size()) {
    // cout << t << " " << i << " " << t.substr(i, a.size()) << endl;
    if (t.substr(i, a.size()) == a) {
      t.replace(i, a.size(), b);
      i += b.size();
    } else {
      i++;
    }
  }

  return t;
}

int bfs() {
  mp.clear();
  queue<pair<string, int>> que;
  que.push(make_pair(s, 0));

  while (!que.empty()) {
    string t;
    int c;
    tie(t, c) = que.front();
    que.pop();
    if (t == g)
      break;

    for (const auto &p : rules) {
      string nt = getReplacedString(t, p);
      if (!mp.count(nt) && nt.size() <= g.size()) {
        mp[nt] = c + 1;
        que.push(make_pair(nt, c + 1));
      }
    }
  }

  return (mp.count(g) ? mp[g] : -1);
}

signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  int n;
  int t = 0;
  while (cin >> n, n) {
    rules.clear();
    rep(i, n) {
      string a, b;
      cin >> a >> b;
      rules.emplace_back(a, b);
    }
    cin >> s;
    cin >> g;

    cout << bfs() << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrep2(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()

typedef long long ll;
typedef pair<int, int> Pii;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;

vector<pair<string, string>> rules;
string s;
string g;
map<string, int> mp;

string getReplacedString(string t, pair<string, string> p) {
  string a, b;
  tie(a, b) = p;

  int i = 0;
  while (i <= t.size() - a.size()) {
    if (t.substr(i, a.size()) == a) {
      t.replace(i, a.size(), b);
      i += b.size();
    } else {
      i++;
    }
  }

  return t;
}

int bfs() {
  mp.clear();
  queue<pair<string, int>> que;
  que.push(make_pair(s, 0));

  while (!que.empty()) {
    string t;
    int c;
    tie(t, c) = que.front();
    que.pop();
    if (t == g)
      break;

    for (const auto &p : rules) {
      string nt = getReplacedString(t, p);
      if (!mp.count(nt) && nt.size() <= g.size()) {
        mp[nt] = c + 1;
        que.push(make_pair(nt, c + 1));
      }
    }
  }

  return (mp.count(g) ? mp[g] : -1);
}

signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  int n;
  while (cin >> n, n) {
    rules.clear();
    rep(i, n) {
      string a, b;
      cin >> a >> b;
      rules.emplace_back(a, b);
    }
    cin >> s;
    cin >> g;

    cout << bfs() << endl;
  }
}