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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

int main() {
  int n;
  int nn = 0;
  while (cin >> n, n) {
    nn++;
    map<string, string> mp;
    REP(i, n) {
      string a, b;
      cin >> a >> b;
      mp[a] = b;
    }
    string s, t;
    cin >> s >> t;
    map<string, int> dist;
    queue<string> Q;
    dist[s] = 0;
    Q.push(s);
    int res = -1;
    while (!Q.empty()) {
      string now = Q.front();
      Q.pop();
      // cout << dist[now] << " " << now << endl;

      if (now == t) {
        res = dist[now];
        break;
      }
      if (now.size() >= t.size())
        continue;
      FOR(it, mp) {
        string a = it->first;
        string b = it->second;
        string tmp = now;
        for (int i = 0; i < (int)tmp.size() - (int)a.size() + 1; ++i) {
          // cout << ":" << tmp.substr(i, a.size()) << endl;
          if (tmp.substr(i, a.size()) == a) {
            string hoge;
            if (i + a.size() < tmp.size())
              hoge = tmp.substr(i + a.size());
            tmp = tmp.substr(0, i) + b + hoge;
            // cout << i << " " <<tmp.substr(0, i) << " " << b << " "  << hoge
            // << endl;
            i += b.size() - 1;
          }
        }
        if (dist.count(tmp))
          continue;
        dist[tmp] = dist[now] + 1;
        Q.push(tmp);
      }
    }
    cout << res << endl;
    // if (res == 2) cout << " " << nn << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

int main() {
  int n;
  while (cin >> n, n) {
    map<string, string> mp;
    REP(i, n) {
      string a, b;
      cin >> a >> b;
      mp[a] = b;
    }
    string s, t;
    cin >> s >> t;
    map<string, int> dist;
    queue<string> Q;
    Q.push(s);
    int res = -1;
    while (!Q.empty()) {
      string now = Q.front();
      Q.pop();
      // cout << now << endl;

      if (now == t) {
        res = dist[now];
        break;
      }
      if (now.size() >= t.size())
        continue;
      FOR(it, mp) {
        string a = it->first;
        string b = it->second;
        string tmp = now;
        for (int i = 0; i < (int)tmp.size() - a.size() + 1; ++i) {
          // cout << ":" << tmp.substr(i, a.size()) << endl;
          if (tmp.substr(i, a.size()) == a) {
            string hoge;
            if (i + a.size() < tmp.size())
              hoge = tmp.substr(i + a.size());
            tmp = tmp.substr(0, i) + b + hoge;
            // cout << i << " " <<tmp.substr(0, i) << " " << b << " "  << hoge
            // << endl;
            i += b.size() - 1;
          }
        }
        if (dist.count(tmp))
          continue;
        dist[tmp] = dist[now] + 1;
        Q.push(tmp);
      }
    }
    cout << res << endl;
  }
}