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

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF (1e9 + 1)
// #define INF (1LL<<59)

// verified AOJ GRL_1
#define MAX_V 100000
struct edge {
  int to, cost;
};
vector<edge> G[MAX_V];

void dijkstra(int s, vector<int> &d) {
  priority_queue<pii, vector<pii>, greater<pii>> que;
  rep(i, d.size()) d[i] = INF;
  d[s] = 0;
  que.push(pii(0, s));

  while (que.size()) {
    pii p = que.top();
    que.pop();

    int v = p.second;
    if (d[v] < p.first)
      continue;

    rep(i, G[v].size()) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(pii(d[e.to], e.to));
      }
    }
  }
}

string replace(string origin, string from, string to) {
  string ret = "";
  for (int i = 0; i < origin.size();) {
    if (i + from.size() <= origin.size()) {
      bool isSame = true;
      for (int j = i; j < i + from.size(); j++) {
        if (origin[j] != from[j - i])
          isSame = false;
      }
      if (isSame) {
        ret += to;
        i += from.size();
      } else
        ret += origin[i++];
    } else
      ret += origin[i++];
  }
  return ret;
}

int main() {
  assert(replace("aaxaaa", "aa", "bca") == "bcaxbcaa");
  int n;
  while (cin >> n && n) {
    vector<pair<string, string>> vp;
    rep(i, MAX_V) G[i].clear();
    rep(i, n) {
      string a, b;
      cin >> a >> b;
      vp.pb(make_pair(a, b));
    }

    string s, t;
    cin >> s >> t;

    map<string, int> num;
    queue<string> que;
    que.push(s);
    int c = 0;
    num[s] = c++;

    while (que.size()) {
      string q = que.front();
      que.pop();
      //			cout<<q<<endl;

      rep(i, vp.size()) {
        string a = vp[i].first, b = vp[i].second;
        if (q.find(a) != string::npos) {
          string res = replace(q, a, b);
          if (res.size() > t.size() + 1 || num.count(res) != 0)
            continue;
          que.push(res);
          num[res] = c++;
          G[num[q]].pb(edge{c - 1, 1});
        }
      }
    }
    assert(c < MAX_V);
    vector<int> d(c);
    dijkstra(num[s], d);
    if (num.count(t) == 0)
      cout << -1 << endl;
    else
      cout << d[num[t]] << endl;
  }
}

Error: MLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF (1e9 + 1)
// #define INF (1LL<<59)

// verified AOJ GRL_1
#define MAX_V 1000000
struct edge {
  int to, cost;
};
vector<edge> G[MAX_V];

void dijkstra(int s, vector<int> &d) {
  priority_queue<pii, vector<pii>, greater<pii>> que;
  rep(i, d.size()) d[i] = INF;
  d[s] = 0;
  que.push(pii(0, s));

  while (que.size()) {
    pii p = que.top();
    que.pop();

    int v = p.second;
    if (d[v] < p.first)
      continue;

    rep(i, G[v].size()) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(pii(d[e.to], e.to));
      }
    }
  }
}

string replace(string origin, string from, string to) {
  string ret = "";
  for (int i = 0; i < origin.size();) {
    if (i + from.size() <= origin.size()) {
      bool isSame = true;
      for (int j = i; j < i + from.size(); j++) {
        if (origin[j] != from[j - i])
          isSame = false;
      }
      if (isSame) {
        ret += to;
        i += from.size();
      } else
        ret += origin[i++];
    } else
      ret += origin[i++];
  }
  return ret;
}

int main() {
  assert(replace("aaxaaa", "aa", "bca") == "bcaxbcaa");
  int n;
  while (cin >> n && n) {
    vector<pair<string, string>> vp;
    rep(i, MAX_V) G[i].clear();
    rep(i, n) {
      string a, b;
      cin >> a >> b;
      vp.pb(make_pair(a, b));
    }

    string s, t;
    cin >> s >> t;

    map<string, int> num;
    queue<string> que;
    que.push(s);
    int c = 0;
    num[s] = c++;

    while (que.size()) {
      string q = que.front();
      que.pop();
      //			cout<<q<<endl;

      rep(i, vp.size()) {
        string a = vp[i].first, b = vp[i].second;
        if (q.find(a) != string::npos) {
          string res = replace(q, a, b);
          if (res.size() > 2 * t.size() - 1 || num.count(res) != 0)
            continue;
          que.push(res);
          num[res] = c++;
          G[num[q]].pb(edge{c - 1, 1});
        }
      }
    }
    assert(c < 1000000);
    vector<int> d(c);
    dijkstra(num[s], d);
    if (num.count(t) == 0)
      cout << -1 << endl;
    else
      cout << d[num[t]] << endl;
  }
}