Problem p00875 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define SQ(x) ((x) * (x))
#define Mod(x, mod) (((x)+(mod)%(mod))
#define MP make_pair
#define PB push_back
#define Fi first
#define Se second
#define INF (1 << 29)
#define EPS 1e-10
#define MOD 1000000007

typedef pair<int, int> pi;
typedef pair<int, string> P;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int N;
string a[16], b[16];
string y, d;

string parse(string s, string A, string B) {
  //  cout << s << " " << A << " " << B << endl;
  rep(i, s.size()) {
    if (i + A.size() <= s.size() && s.substr(i, A.size()) == A) {
      //      cout << i << endl;
      s.replace(s.begin() + i, s.begin() + i + A.size(), B);
      i += B.size() - 1;
      //      i++;
    }
  }
  // cout << s << endl;
  return s;
}

int bfs() {
  queue<P> q;
  q.push(P(0, y));
  set<string> s;
  s.insert(y);
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    string cur = p.second;
    //    cout << cur << endl;
    if (cur == d)
      return p.first;
    rep(i, N) {
      string tar = parse(cur, a[i], b[i]);
      //      cout << tar << endl;
      if (s.find(tar) != s.end() || tar.size() > d.size())
        continue;
      q.push(P(p.first + 1, tar));
      s.insert(tar);
    }
  }
  return -1;
}

int main() {
  while (cin >> N, N) {
    rep(i, N) cin >> a[i] >> b[i];
    cin >> y >> d;

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

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define SQ(x) ((x) * (x))
#define Mod(x, mod) (((x)+(mod)%(mod))
#define MP make_pair
#define PB push_back
#define Fi first
#define Se second
#define INF (1 << 29)
#define EPS 1e-10
#define MOD 1000000007

typedef pair<int, int> pi;
typedef pair<int, string> P;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int N;
string a[16], b[16];
string y, d;

string parse(string s, string A, string B) {
  //  cout << s << " " << A << " " << B << endl;
  rep(i, s.size()) {
    if (i + A.size() <= s.size() && s.substr(i, A.size()) == A) {
      s.replace(s.begin() + i, s.begin() + i + A.size(), B);
      //      i += B.size();
      i++;
    }
  }
  //  cout << s << endl;
  return s;
}

int bfs() {
  queue<P> q;
  q.push(P(0, y));
  set<string> s;
  s.insert(y);
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    string cur = p.second;
    //    cout << cur << endl;
    if (cur == d)
      return p.first;
    rep(i, N) {
      string tar = parse(cur, a[i], b[i]);
      //      cout << tar << endl;
      if (s.find(tar) != s.end() || tar.size() > d.size())
        continue;
      q.push(P(p.first + 1, tar));
      s.insert(tar);
    }
  }
  return -1;
}

int main() {
  while (cin >> N, N) {
    rep(i, N) cin >> a[i] >> b[i];
    cin >> y >> d;

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