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

#define _CRT_SECURE_NO_WARNINGS
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
#define all(c) (c).begin(), (c).end()
#define loop(i, a, b) for (ll i = a; i < ll(b); i++)
#define rep(i, b) loop(i, 0, b)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
template <class T> ostream &operator<<(ostream &os, vector<T> const &);
template <int n, class... T>
typename enable_if<(n >= sizeof...(T))>::type _ot(ostream &,
                                                  tuple<T...> const &) {}
template <int n, class... T>
typename enable_if<(n < sizeof...(T))>::type _ot(ostream &os,
                                                 tuple<T...> const &t) {
  os << (n == 0 ? "" : " ") << get<n>(t);
  _ot<n + 1>(os, t);
}
template <class... T> ostream &operator<<(ostream &os, tuple<T...> const &t) {
  _ot<0>(os, t);
  return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, pair<T, U> const &p) {
  return os << "(" << p.first << ", " << p.second << ") ";
}
template <class T> ostream &operator<<(ostream &os, vector<T> const &v) {
  rep(i, v.size()) os << v[i] << (i + 1 == (int)v.size() ? "" : " ");
  return os;
}
#ifdef DEBUG
#define dump(...)                                                              \\
  (cerr << #__VA_ARGS__ << " = " << mt(__VA_ARGS__) << " [" << __LINE__ << "]" \\
        << endl)
#else
#define dump(...)
#endif

int n;

ll encode(vi const &v) {
  ll res = 0;
  rep(i, n) {
    res *= 10;
    res += v[i];
  }
  return res;
}

vi decode(ll c) {
  vi res(n);
  rep(i, n) {
    res[n - i - 1] = c % 10;
    c /= 10;
  }
  return res;
}

int main() {
  while (cin >> n && n) {
    ll s, t;
    cin >> s >> t;
    vi t_ = decode(t);
    queue<pair<ll, int>> q;
    q.emplace(s, 0);
    int ans;
    while (q.size()) {
      ll c;
      int d;
      tie(c, d) = q.front();
      // dump(c,d);
      vi c_ = decode(c);
      q.pop();
      int p = 0;
      while (p < (int)t_.size() && t_[p] == c_[p])
        p++;
      if (p == (int)t_.size()) {
        ans = d;
        break;
      }
      int diff = t_[p] - c_[p];
      for (int i = p; i < (int)t_.size(); i++) {
        c_[i] = c_[i] + diff + 10;
        c_[i] %= 10;
        q.emplace(encode(c_), d + 1);
      }
    }
    cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
#define all(c) (c).begin(), (c).end()
#define loop(i, a, b) for (ll i = a; i < ll(b); i++)
#define rep(i, b) loop(i, 0, b)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
template <class T> ostream &operator<<(ostream &os, vector<T> const &);
template <int n, class... T>
typename enable_if<(n >= sizeof...(T))>::type _ot(ostream &,
                                                  tuple<T...> const &) {}
template <int n, class... T>
typename enable_if<(n < sizeof...(T))>::type _ot(ostream &os,
                                                 tuple<T...> const &t) {
  os << (n == 0 ? "" : " ") << get<n>(t);
  _ot<n + 1>(os, t);
}
template <class... T> ostream &operator<<(ostream &os, tuple<T...> const &t) {
  _ot<0>(os, t);
  return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, pair<T, U> const &p) {
  return os << "(" << p.first << ", " << p.second << ") ";
}
template <class T> ostream &operator<<(ostream &os, vector<T> const &v) {
  rep(i, v.size()) os << v[i] << (i + 1 == (int)v.size() ? "" : " ");
  return os;
}
#ifdef DEBUG
#define dump(...)                                                              \\
  (cerr << #__VA_ARGS__ << " = " << mt(__VA_ARGS__) << " [" << __LINE__ << "]" \\
        << endl)
#else
#define dump(...)
#endif

int main() {
  int n;
  while (cin >> n && n) {
    string s, t;
    cin >> s >> t;
    queue<pair<string, int>> q;
    q.emplace(s, 0);
    int ans;
    while (q.size()) {
      string c;
      int d;
      tie(c, d) = q.front();
      q.pop();
      int p = 0;
      while (p < (int)t.size() && t[p] == c[p])
        p++;
      if (p == (int)t.size()) {
        ans = d;
        break;
      }
      int diff = t[p] - c[p];
      for (int i = p; i < (int)t.size(); i++) {
        c[i] += diff;
        if (c[i] > '9')
          c[i] -= 10;
        q.emplace(c, d + 1);
      }
    }
    cout << ans << endl;
  }
}