alexjercan/bugnet huggingface dataset:
Problem p01275 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

#define N_MAX (10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1)
map<long long, int> d;

int main() {
  int n;
  while (cin >> n && n) {
    d.clear();
    long long a, b;
    cin >> a >> b;

    queue<long long> Q;
    Q.push(a);
    d[a] = 0;
    long long ten[10] = {};
    ten[0] = 1;
    for (int i = 1; i < 10; i++)
      ten[i] = ten[i - 1] * 10;
    while (Q.size()) {
      long long q = Q.front();
      Q.pop();
      // cout << q << endl;
      if (q == b) {
        cout << d[q] << endl;
        break;
      }
      for (int i = n - 1; i >= 0; i--) {
        if (q / ten[i] % 10 != b / ten[i] % 10) {
          int ammount = (b / ten[i] % 10 - q / ten[i] % 10 + 10) % 10;
          long long next = q;
          for (int j = i; j >= 0; j--) {
            int nd = (next / ten[j] % 10 + ammount) % 10;
            next = next - (next / ten[j] % 10) * ten[j] + ten[j] * nd;
            if (d.count(next) == 0) {
              d[next] = d[q] + 1;
              Q.push(next);
            }
          }
          break;
        }
      }
    }
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define N_MAX (10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1)
map<long long, int> d;

int main() {
  int n;
  while (cin >> n && n) {
    d.clear();
    int a, b;
    cin >> a >> b;

    queue<long long> Q;
    Q.push(a);
    d[a] = 0;
    long long ten[10] = {};
    ten[0] = 1;
    for (int i = 1; i < 10; i++)
      ten[i] = ten[i - 1] * 10;
    while (Q.size()) {
      long long q = Q.front();
      Q.pop();
      // cout << q << endl;
      if (q == b) {
        cout << d[q] << endl;
        break;
      }
      for (int i = n - 1; i >= 0; i--) {
        // if( q / ten[i]%10 != b / ten[i]%10 ){
        int ammount = (b / ten[i] % 10 - q / ten[i] % 10 + 10) % 10;
        long long next = q;
        for (int j = i; j >= 0; j--) {
          int nd = (next / ten[j] % 10 + ammount) % 10;
          next = next - (next / ten[j] % 10) * ten[j] + ten[j] * nd;
          if (d.count(next) == 0) {
            d[next] = d[q] + 1;
            Q.push(next);
          }
        }
        // break;
        //}
      }
    }
  }
}
Problem p01275 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

int n, ans;
string A, B;

string cal(string a, int x, int l, int r) {
  for (int i = l; i < r; i++) {
    int p = a[i] - '0';
    p = (p + x) % 10;
    a[i] = '0' + p;
  }
  return a;
}

void dfs(string a, string b, int d, int sum) {

  // cout<<a<<' '<<b<<' '<<d<<endl;

  if (ans <= sum)
    return;

  if (a == b) {
    ans = min(ans, sum);
    return;
  }

  if (a[d] != b[d]) {
    for (int i = 1; i <= 9; i++) {
      string nex = cal(a, i, d, d + 1);
      if (nex[d] != b[d])
        continue;
      for (int r = d + 1; r <= n; r++) {
        nex = cal(a, i, d, r);
        dfs(nex, b, d + 1, sum + 1);
      }
    }
  } else {
    dfs(a, b, d + 1, sum);
  }
}

signed main() {
  while (cin >> n, n) {
    cin >> A >> B;
    ans = 1e9;
    dfs(A, B, 0, 0);
    cout << ans << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int n, ans;
string A, B;

string cal(string a, int x, int l, int r) {
  for (int i = l; i < r; i++) {
    int p = a[i] - '0';
    p = (p + x) % 10;
    a[i] = '0' + p;
  }
  return a;
}

void dfs(string a, string b, int d, int sum) {

  // cout<<a<<' '<<b<<' '<<d<<endl;

  if (a == b) {
    ans = min(ans, sum);
    return;
  }

  if (a[d] != b[d]) {
    for (int i = 1; i <= 9; i++) {
      string nex = cal(a, i, d, d + 1);
      if (nex[d] != b[d])
        continue;
      for (int r = d + 1; r <= n; r++) {
        nex = cal(a, i, d, r);
        dfs(nex, b, d + 1, sum + 1);
      }
    }
  } else {
    dfs(a, b, d + 1, sum);
  }
}

signed main() {
  while (cin >> n, n) {
    cin >> A >> B;
    ans = 1e9;
    dfs(A, B, 0, 0);
    cout << ans << endl;
  }
}
Problem p01275 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
using namespace std;

int n;
string a, b;

int dfs(string a, int i, int cnt) {
  if (i >= n)
    return cnt;
  if (a[i] == b[i]) {
    return dfs(a, i + 1, cnt);
  }

  int ret = 1e8;
  int dif = (b[i] - a[i] + 10) % 10;
  for (int j = i; j < n; j++) {
    a[j] = '0' + (a[j] + dif - '0') % 10;
    ret = min(ret, dfs(a, i + 1, cnt + 1));
  }
  return ret;
}

int main() {
  while (cin >> n, n) {
    map<string, int> m;
    cin >> a >> b;

    queue<pair<int, string>> q;
    q.push(make_pair(0, a));
    cout << dfs(a, 0, 0) << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
using namespace std;

int deleteNumver(vector<int> &d) {
  map<int, int> m;
  rep(i, d.size()) { m[d[i]]++; }

  int res = 0;
  if (m[d.front()] == 1) {
    d.erase(d.begin());
    res++;
  }
  if (d.empty())
    return res;
  if (m[d.back()] == 1) {
    d.erase(--d.end());
    res++;
  };

  return res;
}

vector<int> deleteRow(vector<int> d) {
  vector<int> res;
  rep(i, d.size()) {
    if (res.empty())
      res.emplace_back(d[i]);
    else if (res.back() != d[i])
      res.emplace_back(d[i]);
  }
  return res;
}

vector<int> deleteZero(vector<int> d) {
  vector<int> ret;
  bool u[11] = {0};

  rep(i, d.size()) {
    if (d[i] == 0)
      u[i] = true;
    else
      break;
  }

  for (int i = d.size() - 1; i >= 0; i--) {
    if (d[i] == 0)
      u[i] = true;
    else
      break;
  }

  rep(i, d.size()) {
    if (not u[i])
      ret.emplace_back(d[i]);
  }
  return ret;
}

int main() {
  int n;
  while (cin >> n, n) {
    string a, b;
    cin >> a >> b;
    vector<int> d;
    rep(i, n) {
      int tmp = b[i] - a[i];
      if (tmp < 0)
        tmp += 10;
      if (d.empty())
        d.emplace_back(tmp);
      else if (d.back() != tmp)
        d.emplace_back(tmp);
    }

    int cnt = 0;
    int maxi = 0, p;
    while (true) {
      int before = d.size();
      // rep(i,d.size()){ cout << d[i] << ' '; }cout << endl;
      d = deleteZero(d);
      d = deleteRow(d);
      cnt += deleteNumver(d);
      // show(cnt)
      // rep(i,d.size()){ cout << d[i] << ' '; }cout << endl;
      if (d.size() == 0)
        break;

      if (before == d.size()) {
        map<int, int> m;
        bool u[11] = {0};
        rep(i, d.size()) if (d[i] == 0) u[i] = true;
        rep(i, d.size()) {
          if (u[i])
            continue;
          m[d[i]]++;
          if (maxi < m[d[i]]) {
            maxi = m[d[i]];
            p = d[i];
          }
        }
        rep(i, d.size()) {
          if (u[i])
            continue;
          if (m[d[i]] != maxi)
            cnt++;
        }
        cnt++;
        break;
      }
    }

    cout << cnt << endl;
  }
}
Problem p01275 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef struct D {
  int t;
  vector<int> d;
} D;
int N, r = 99;
vector<int> s, a;
map<vector<int>, int> F;

bool ch(D t) {
  for (int i = 0; i < N; i++)
    if (a[i] != t.d[i])
      return false;
  return true;
}

int main() {
  for (; scanf("%d", &N), N;) {
    for (int i = 0, t; i < N; i++) {
      scanf("%1d", &t);
      s.push_back(t);
    }
    for (int i = 0, t; i < N; i++) {
      scanf("%1d", &t);
      a.push_back(t);
    }
    r = 99;
    queue<D> Q;
    D ins = {0, s};
    Q.push(ins);
    while (!Q.empty()) {
      D tmp = Q.front();
      Q.pop();
      if (F.find(tmp.d) != F.end() && F[tmp.d] < tmp.t)
        continue;
      if (ch(tmp)) {
        r = min(r, tmp.t);
        continue;
      }
      for (int i = 0; i < N; i++) {
        if (a[i] != tmp.d[i]) {
          int mov = (10 + (a[i] - tmp.d[i])) % 10;
          for (int k = i + 1; k <= N; k++) {
            ins = tmp;
            for (int j = i; j < k; j++) {
              ins.d[j] = (ins.d[j] + mov) % 10;
            }
            if (F.count(ins.d) == 0) {
              ins.t++;
              Q.push(ins);
              F[ins.d] = ins.t;
            }
          }
          break;
        }
      }
    }
    s.clear();
    a.clear();
    F.clear();
    printf("%d\\n", r);
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef struct D {
  int t;
  vector<int> d;
} D;
int N, r = 99;
vector<int> s, a;

bool ch(D t) {
  for (int i = 0; i < N; i++)
    if (a[i] != t.d[i])
      return false;
  return true;
}

int main() {
  for (; scanf("%d", &N), N;) {
    for (int i = 0, t; i < N; i++) {
      scanf("%1d", &t);
      s.push_back(t);
    }
    for (int i = 0, t; i < N; i++) {
      scanf("%1d", &t);
      a.push_back(t);
    }
    r = 99;
    queue<D> Q;
    D ins = {0, s};
    Q.push(ins);
    while (!Q.empty()) {
      D tmp = Q.front();
      Q.pop();
      if (ch(tmp)) {
        r = min(r, tmp.t);
        continue;
      }
      for (int i = 0; i < N; i++) {
        if (a[i] != tmp.d[i]) {
          int mov = (10 + (a[i] - tmp.d[i])) % 10;
          for (int k = i + 1; k <= N; k++) {
            ins = tmp;
            for (int j = i; j < k; j++) {
              ins.d[j] = (ins.d[j] + mov) % 10;
            }
            ins.t++;
            Q.push(ins);
          }
          break;
        }
      }
    }
    s.clear();
    a.clear();
    printf("%d\\n", r);
  }
}
Problem p01275 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <vector>

#define rep(n) for (int i = 0; i < n; i++)
#define repp(j, n) for (int j = 0; j < n; j++)
#define reppp(i, m, n) for (int i = m; i < n; i++)

using namespace std;

int k;

int dfs(vector<int> a, vector<int> b, int i) {
  if (i == k - 1)
    return (a[i] == b[i] ? 0LL : 1LL);

  int ret = 11, rotate = (b[i] - a[i] + 10) % 10;
  vector<int> aa(a);
  aa[i] = b[i];
  reppp(j, i + 1, k) {
    ret = min(ret, dfs(aa, b, i + 1));
    aa[j] = (a[j] + rotate) % 10;
  }
  ret = min(ret, dfs(aa, b, i + 1));
  return ret + (a[i] == b[i] ? 0LL : 1LL);
}

int main() {
  while (scanf("%d", &k), k) {
    char sa[10], sb[10];
    scanf("%s %s", sa, sb);
    vector<int> a(k), b(k);
    rep(k) {
      a[i] = sa[i] - '0';
      b[i] = sb[i] - '0';
    }

    printf("%d\\n", dfs(a, b, 0));
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define rep(n) for (int i = 0; i < n; i++)
#define repp(j, n) for (int j = 0; j < n; j++)
#define reppp(i, m, n) for (int i = m; i < n; i++)
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define debug(x) cerr << #x << ": " << x << endl

using namespace std;

typedef long long ll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pii;
struct Edge {
  int from, to;
  ll cost;
};

const ll MOD = 1000000007;
const long double EPS = 10e-10;

int k;

int dfs(vector<int> a, vector<int> b, int i) {
  if (i == k)
    return 0;

  int ret_ = INT_MAX, rotate = (b[i] - a[i] + 10) % 10;
  vector<int> aa(a);
  aa[i] = b[i];
  reppp(j, i + 1, k) {
    ret_ = min(ret_, dfs(aa, b, i + 1));
    aa[j] = (a[j] + rotate) % 10;
  }
  ret_ = min(ret_, dfs(aa, b, i + 1));
  return ret_ + (a[i] == b[i] ? 0LL : 1LL);
}

int main() {
  while (scanf("%d", &k), k) {
    string sa, sb;
    cin >> sa >> sb;
    vector<int> a(k), b(k);
    rep(k) {
      a[i] = sa[i] - '0';
      b[i] = sb[i] - '0';
    }

    cout << dfs(a, b, 0) << endl;
  }
}
Problem p01275 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int inf = 1 << 30;
const long long int infll = 1LL << 58;
const double eps = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

int len;
string a, b;

int rec(const string &s, int idx) {
  if (idx == len)
    return 0;
  const int d = (b[idx] - s[idx] + 10) % 10;
  if (d == 0)
    return rec(s, idx + 1);
  int res = inf;
  rep(i, 1, len - idx + 1) {
    string tmp = s;
    rep(j, idx, idx + i) { tmp[j] = '0' + (tmp[j] - '0' + d) % 10; }
    res = min(res, rec(tmp, idx + 1) + 1);
  }
  return res;
}

void solve() {
  while (true) {
    cin >> len;
    if (!len)
      break;
    cin >> a >> b;
    cout << rec(a, 0) << endl;
  }
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  solve();
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int inf = 1 << 30;
const long long int infll = 1LL << 58;
const double eps = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void solve() {
  const string table = "0123456789";
  while (true) {
    int len;
    string a, b;
    cin >> len;
    if (!len)
      break;
    cin >> a >> b;

    queue<pair<string, int>> que;
    set<string> done;
    que.push(make_pair(a, 0));
    while (true) {
      pair<string, int> p = que.front();
      que.pop();
      if (p.first == b) {
        cout << p.second << endl;
        break;
      }
      if (done.find(p.first) != done.end())
        continue;
      done.insert(p.first);
      rep(i, 0, len) rep(j, i, len) rep(k, 1, 10) {
        string tmp = p.first;
        rep(pos, i, j + 1) tmp[pos] = table[(tmp[pos] - '0' + k) % 10];
        que.push(make_pair(tmp, p.second + 1));
      }
    }
  }
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  solve();
  return 0;
}
Problem p01275 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define repp(i, m, n) for (int i = m; i < (int)n; i++)
#define reps(i, n) for (int i = 1; i <= (int)n; i++)
const int INF = 100000000;
const double EPS = 1e-10;
const int MOD = 1000000007;
using namespace std;
typedef long long ll;
typedef pair<int, int> pai;
typedef pair<ll, ll> pal;

int main() {
  int k;
  ll a[12], b[12], c[12];
  ll x, y, z, w, aa;
  while (cin >> k) {
    if (k == 0)
      break;
    cin >> x >> y;
    z = y;
    reps(i, k) {
      a[i] = z % 10;
      z /= 10;
    }
    queue<pal> q;
    q.push(pal(100 * x + 0, 0));
    ll ans = k;
    int i;
    while (!q.empty()) {
      x = q.front().first;
      aa = q.front().second;
      i = (int)(x % 100) + 1;
      x /= 100;
      if (x == y) {
        ans = min(ans, aa);
      }
      q.pop();
      reps(j, k) {
        b[j] = x % 10;
        x /= 10;
      }
      int tmp = a[i] - b[i] + 10;
      if (tmp % 10 != 0)
        aa++;
      if (i <= k) {
        for (int l = i; l <= k; l++) {
          b[l] = (b[l] + tmp) % 10;
          w = 0;
          reps(j, k) {
            w *= 10;
            w += b[k + 1 - j];
          }
          q.push(pal(w * 100 + i, aa));
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define repp(i, m, n) for (int i = m; i < (int)n; i++)
#define reps(i, n) for (int i = 1; i <= (int)n; i++)
const int INF = 100000000;
const double EPS = 1e-10;
const int MOD = 1000000007;
using namespace std;
typedef long long ll;
typedef pair<int, int> pai;
typedef pair<ll, ll> pal;

int main() {
  int k;
  ll a[12], b[12], c[12];
  ll x, y, z, w, aa;
  while (cin >> k) {
    if (k == 0)
      break;
    cin >> x >> y;
    z = y;
    reps(i, k) {
      a[i] = z % 10;
      z /= 10;
    }
    queue<pal> q;
    q.push(pal(10 * x + 0, 0));
    ll ans = k;
    int i;
    while (!q.empty()) {
      x = q.front().first;
      aa = q.front().second;
      i = (int)(x % 10) + 1;
      x /= 10;
      if (x == y) {
        ans = min(ans, aa);
      }
      q.pop();
      reps(j, k) {
        b[j] = x % 10;
        x /= 10;
      }
      int tmp = a[i] - b[i] + 10;
      if (tmp % 10 != 0)
        aa++;
      if (i <= k) {
        for (int l = i; l <= k; l++) {
          b[l] = (b[l] + tmp) % 10;
          w = 0;
          reps(j, k) {
            w *= 10;
            w += b[k + 1 - j];
          }
          q.push(pal(w * 10 + i, aa));
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01275 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1e8;
int main() {
  int n;
  while (cin >> n, n) {
    string s, t;
    cin >> s >> t;
    if (s == t) {
      cout << 0 << endl;
      continue;
    }
    map<string, int> m;
    m[s] = 0;
    queue<string> q;
    q.push(s);
    while (!s.empty()) {
      string a = q.front();
      q.pop();
      int g = 0;
      while (a[g] == t[g])
        g++;
      int r = (10 + t[g] - a[g]) % 10;
      string b = a;
      loop(i, g, n) {
        b[i] = (b[i] + r - '0') % 10 + '0';
        //				cout<<b<<" "<<r<<endl;
        if (m[b] == 0 || m[a] + 1 < m[b]) {
          m[b] = m[a] + 1;
          q.push(b);
          if (b == t) {
            cout << m[b] << endl;
            goto end;
          }
        }
      }
    }
  end:;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1e8;
int main() {
  int n;
  while (cin >> n, n) {
    string s, t;
    cin >> s >> t;
    map<string, int> m;
    m[s] = 0;
    queue<string> q;
    q.push(s);
    while (!s.empty()) {
      string a = q.front();
      q.pop();
      int g = 0;
      while (a[g] == t[g])
        g++;
      int r = (10 + t[g] - a[g]) % 10;
      string b = a;
      loop(i, g, n) {
        b[i] = (b[i] + r - '0') % 10 + '0';
        //				cout<<b<<" "<<r<<endl;
        if (m[b] == 0 || m[a] + 1 < m[b]) {
          m[b] = m[a] + 1;
          q.push(b);
          if (b == t) {
            cout << m[b] << endl;
            goto end;
          }
        }
      }
    }
  end:;
  }
}
Problem p01275 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <map>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

typedef long long ll;
typedef vector<int> vi;

const int INF = 1 << 29;

map<ll, int> memo[10];

int solve(int p, vi a, vi b) {
  int n = a.size();

  ll key = 0;
  for (int i = p; i < n; i++)
    key = key * 10 + a[i];
  if (memo[p].count(key))
    return memo[p][key];

  if (p == n - 1)
    return memo[p][key] = (a[p] != b[p]);

  int ans = INF;
  int rot = (b[p] - a[p] + 10) % 10, cost = (rot != 0);
  for (int i = p; i < n; i++) {
    a[i] = (a[i] + rot) % 10;
    ans = min(ans, solve(p + 1, a, b) + cost);
  }
  return memo[p][key] = ans;
}

int main() {
  for (int n; scanf("%d", &n), n;) {
    char sa[16], sb[16];
    scanf("%s%s", sa, sb);
    vi a(n), b(n);
    rep(i, n) {
      memo[i].clear();
      a[i] = sa[i] - '0';
      b[i] = sb[i] - '0';
    }
    printf("%d\\n", solve(0, a, b));
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

typedef vector<int> vi;

const int INF = 1 << 29;

int solve(int p, vi a, vi b) {
  int n = a.size();
  if (p == n - 1)
    return a[p] != b[p];

  int ans = INF;
  int rot = (b[p] - a[p] + 10) % 10, cost = (rot != 0);
  for (int i = p; i < n; i++) {
    a[i] = (a[i] + rot) % 10;
    ans = min(ans, solve(p + 1, a, b) + cost);
  }
  return ans;
}

int main() {
  for (int n; scanf("%d", &n), n;) {
    char sa[16], sb[16];
    scanf("%s%s", sa, sb);
    vi a(n), b(n);
    rep(i, n) {
      a[i] = sa[i] - '0';
      b[i] = sb[i] - '0';
    }
    printf("%d\\n", solve(0, a, b));
  }
  return 0;
}