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

#include <cstring>
#include <iostream>
#include <queue>

using namespace std;
int N;

int search(int *rot, int t, int cost) {
  if (t == N)
    return cost;
  if (!rot[t])
    return search(rot, t + 1, cost);
  int rn = rot[t];
  int min = 10;
  for (int i = t; i < N; i++) {
    int tmp[12];
    memcpy(tmp, rot, sizeof(tmp));
    for (int j = t; j <= i; j++)
      tmp[j] = (tmp[j] - rn + 10) % 10;
    int ret = search(tmp, t + 1, cost + 1);
    if (ret < min)
      min = ret;
  }
  return min;
}

int main() {
  int i, j;
  while (cin >> N, N) {
    char s[12], t[12];
    cin >> s >> t;
    int rot[12];
    for (i = 0; i < N; i++) {
      rot[i] = (t[i] - s[i] + 10) % 10;
    }

    cout << search(rot, 0, 0) << endl;
  }
}

Error: MLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <queue>

using namespace std;

class DP {
public:
  int cost, t;
  int Map[10];

  DP() {}
  DP(int xx, int yy, int *m) {
    cost = xx;
    t = yy;
    memcpy(Map, m, 10 * sizeof(int));
  }

  bool operator<(const DP &opp) const { return cost > opp.cost; }
};

int main() {
  int i, j, N;
  while (cin >> N, N) {
    char s[12], t[12];
    cin >> s >> t;
    int rot[12];
    for (i = 0; i < N; i++) {
      rot[i] = (t[i] - s[i] + 10) % 10;
    }

    priority_queue<DP> dp;
    DP T, S;
    dp.push(DP(0, 0, rot));
    int ans = 10;
    while (!dp.empty()) {
      S = dp.top();
      dp.pop();
      while (S.Map[S.t] == 0 && S.t != N) {
        S.t++;
      }
      if (S.t == N) {
        ans = (ans < S.cost) ? ans : S.cost;
        break;
      }
      int rn = S.Map[S.t];
      for (i = S.t; i < N; i++) {
        memcpy(rot, S.Map, sizeof(rot));
        for (j = S.t; j <= i; j++) {
          rot[j] = (rot[j] - rn + 10) % 10;
        }
        dp.push(DP(S.cost + 1, S.t + 1, rot));
      }
    }
    cout << ans << endl;
  }
}
Problem p01275 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int k, ans;
vector<int> dif;
const int M = 10;

void rec(int d, int p) {
  if (p == k) {
    ans = min(ans, d);
    return;
  }
  if (d >= ans)
    return;

  for (int i = k; i >= 1; i--) {
    if (p + i - 1 >= k)
      continue;
    vector<int> tmp = dif;
    for (int j = p + i - 1; j >= p; j--) {
      dif[j] -= dif[p];
      dif[j] = (dif[j] % M + M) % M;
    }
    int np = p;
    while (np < k && !dif[np])
      np++;
    rec(d + 1, np);
    dif = tmp;
  }
}

int main() {
  while (cin >> k, k) {
    dif.resize(k);

    string s, t;
    cin >> s >> t;
    for (int i = 0; i < k; i++) {
      dif[i] = (((int)(t[i] - s[i])) % M + M) % M;
    }

    ans = k;
    int p = 0;
    while (p < k && !dif[p])
      p++;
    rec(0, p);
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int k, ans;
vector<int> dif;
const int M = 10;

void rec(int d, int p) {
  if (p == k) {
    ans = min(ans, d);
    return;
  }
  if (d >= ans)
    return;

  for (int i = 1; i <= k; i++) {
    if (p + i - 1 > k)
      return;
    vector<int> tmp = dif;
    for (int j = p + i - 1; j >= p; j--) {
      dif[j] -= dif[p];
      dif[j] = (dif[j] % M + M) % M;
    }
    int np = p;
    while (np < k && !dif[np])
      np++;
    rec(d + 1, np);
    dif = tmp;
  }
}

int main() {
  while (cin >> k, k) {
    dif.resize(k);

    string s, t;
    cin >> s >> t;
    for (int i = 0; i < k; i++) {
      dif[i] = (((int)(t[i] - s[i])) % M + M) % M;
    }

    ans = 10;
    rec(0, 0);
    cout << ans << endl;
  }
}
Problem p01275 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int N;

int solve(string &s, string &t) {
  queue<string> Q;
  map<string, int> dist;
  Q.push(s);
  dist[s] = 0;
  while (!Q.empty()) {
    string v = Q.front();
    Q.pop();
    if (v == t)
      return dist[v];
    int i;
    for (i = 0; i < N; i++) {
      if (v[i] != t[i])
        break;
    }
    string next = v;
    int diff = (t[i] - v[i] + 10) % 10;
    for (int j = i; j < N; j++) {
      next[j] = ((next[j] - '0' + diff) % 10) + '0';
      if (dist.find(next) == dist.end()) {
        dist[next] = dist[v] + 1;
        Q.push(next);
      }
    }
  }
}

int main() {
  string s, t;
  while (cin >> N, N) {
    cin >> s >> t;
    cout << solve(s, t) << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int N;

int solve(string &s, string &t) {
  queue<string> Q;
  map<string, int> dist;
  Q.push(s);
  dist[s] = 0;
  while (!Q.empty()) {
    string v = Q.front();
    Q.pop();
    if (v == t)
      return dist[v];
    for (int i = 0; i < N; i++) {
      if (v[i] == t[i])
        continue;
      string next = v;
      int diff = (t[i] - v[i] + 10) % 10;
      for (int j = i; j < N; j++) {
        next[j] = ((next[j] - '0' + diff) % 10) + '0';
        if (dist.find(next) == dist.end()) {
          dist[next] = dist[v] + 1;
          Q.push(next);
        }
      }
    }
  }
}

int main() {
  string s, t;
  while (cin >> N, N) {
    cin >> s >> t;
    cout << solve(s, t) << endl;
  }
  return 0;
}
Problem p01275 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

int solve(string s, string t) {
  if (s == t)
    return 0;
  int p = INF;
  REP(i, s.size()) {
    if (s[i] != t[i]) {
      chmin(p, i);
    }
  }
  int r = t[p] - s[p];
  int res = INF;
  FOR(i, 1, s.size() - p + 1) {
    string u = s;
    REP(j, i) {
      u[p + j] += r;
      if (u[p + j] > '9')
        u[p + j] -= 10;
      if (u[p + j] < '0')
        u[p + j] += 10;
    }
    chmin(res, solve(u, t) + 1);
  }
  return res;
}

int main() {
  int n;
  while (cin >> n, n) {
    string s, t;
    cin >> s >> t;
    cout << solve(s, t) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-10;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

int num(ll n, int i) {
  ll mod = pow(10, i + 1);
  return n % mod / (mod / 10);
}

ll add(ll n, int i, int p) {
  int x = num(n, i);
  if (x + p >= 10)
    return n - pow(10, i) * (10 - p);
  else
    return n + pow(10, i) * p;
}

int dif(int c, int d) {
  if (c <= d)
    return d - c;
  else
    return 10 - (c - d);
}

int main() {
  int n;
  while (cin >> n, n) {
    ll s, t;
    cin >> s >> t;
    queue<pair<ll, int>> que;
    que.push(make_pair(s, 0));
    int ans = INF;
    while (que.size()) {
      ll tmp = que.front().first;
      int cnt = que.front().second;
      que.pop();
      if (tmp == t) {
        chmin(ans, cnt);
        continue;
      }
      int p;
      REP(i, n) {
        if (num(tmp, i) != num(t, i)) {
          p = i;
          break;
        }
      }
      int plus = dif(num(tmp, p), num(t, p));
      FOR(i, p, n) {
        tmp = add(tmp, i, plus);
        que.push(make_pair(tmp, cnt + 1));
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01275 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <string>
using namespace std;
string S, T, U;
queue<pair<string, int>> Q;
set<string> E;
int solve(string P, int n) {
  while (!Q.empty())
    Q.pop();
  Q.push(make_pair(P, 0));
  int CNT = 0;
  int maxn = 10;
  while (!Q.empty()) {
    string V = Q.front().first;
    int Z = Q.front().second;
    Q.pop();
    int to = -1;
    for (int j = 0; j < n; j++)
      if (V[j] != '0') {
        to = j;
        break;
      }
    long long H = stoll(V);
    if (to == -1) {
      return Z;
    }
    int Y = (V[to] - '0');
    int en = n - 1;
    CNT++;
    for (int j = to; j < n; j++)
      if (V[j] != V[to]) {
        en = j - 1;
        break;
      }

    for (int j = en; j < n; j++) {
      string W = V;
      int Y = (W[to] - '0');
      for (int k = to; k <= j; k++) {
        W[k] -= Y;
        if (W[k] < '0')
          W[k] += 10;
      }
      if (E.find(W) == E.end()) {
        Q.push(make_pair(W, Z + 1));
        E.insert(W);
      }
    }
  }
}
int main() {
  while (true) {
    int n;
    cin >> n;
    E.clear();
    if (n == 0)
      break;
    cin >> S >> T;
    U = "";
    for (int i = 0; i < n; i++) {
      int sa = T[i] - S[i];
      if (sa < 0)
        sa += 10;
      U += (char)('0' + sa);
    }
    cout << solve(U, n) << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <queue>
#include <string>
using namespace std;
string S, T, U;
queue<pair<string, int>> Q;
int main() {
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    cin >> S >> T;
    U = "";
    for (int i = 0; i < n; i++) {
      int sa = T[i] - S[i];
      if (sa < 0)
        sa += 10;
      U += (char)('0' + sa);
    }
    while (!Q.empty())
      Q.pop();
    Q.push(make_pair(U, 0));
    while (!Q.empty()) {
      string V = Q.front().first;
      int Z = Q.front().second;
      Q.pop();
      int to = -1;
      for (int j = 0; j < n; j++)
        if (V[j] != '0') {
          to = j;
          break;
        }
      if (to == -1) {
        cout << Z << endl;
        break;
      }
      if (Z >= 8) {
        cout << "9" << endl;
        break;
      }
      int Y = (V[to] - '0');
      int en = n - 1;
      for (int j = to; j < n; j++)
        if (V[j] != V[to]) {
          en = j - 1;
          break;
        }

      for (int j = en; j < n; j++) {
        string W = V;
        int Y = (W[to] - '0');
        for (int k = to; k <= j; k++) {
          W[k] -= Y;
          if (W[k] < '0')
            W[k] += 10;
        }
        Q.push(make_pair(W, Z + 1));
      }
    }
  }
  return 0;
}
Problem p01275 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <string>
using namespace std;

string goal;

int dfs(string s, int c) {
  if (s == goal)
    return 0;
  if (c == s.length())
    return 1 << 29;
  if (s[c] == goal[c])
    return dfs(s, c + 1);
  int res = 1 << 29, rot = s[c] - goal[c];
  for (int i = 1; i <= (s.length() - c); ++i) {
    s[c + i - 1] -= rot;
    if (s[c + i - 1] < '0')
      s[c + i - 1] += 10;
    if (s[c + i - 1] > '9')
      s[c + i - 1] -= 10;
    res = min(res, dfs(s, c + 1) + 1);
  }
  return res;
}

int main() {
  int k;
  string s;
  while (cin >> k, k) {
    cin >> s >> goal;
    cout << dfs(s, 0) << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <string>
using namespace std;

map<string, int> memo;
string goal;

string rot_n(string s, int start, int len, int rot) {
  for (int i = 0; i < len; ++i) {
    s[start + i] -= rot;
    if (s[start + i] < '0')
      s[start + i] += 10;
    if (s[start + i] > '9')
      s[start + i] -= 10;
  }
  return s;
}

int dfs(const string &s, int c) {
  // cout<<s<<" "<<c<<" "<<t<<endl;
  // if(memo.find(s) != memo.end()) return memo[s];
  if (s == goal)
    return 0;
  if (c == s.length())
    return 1 << 29;
  if (s[c] == goal[c])
    return dfs(s, c + 1);
  int res = 1 << 29;
  for (int i = 1; i <= (s.length() - c); ++i) {
    res = min(res, dfs(rot_n(s, c, i, s[c] - goal[c]), c + 1) + 1);
  }
  return res;
}

int main() {
  int k;
  string s;
  while (cin >> k, k) {
    cin >> s >> goal;
    memo.clear();
    memo[goal] = 0;
    cout << dfs(s, 0) << endl;
  }
}
Problem p01275 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int n;
int solve(int d, string s) {
  if (d == n)
    return 0;
  if (s[d] == 48)
    return solve(d + 1, s);
  int ret = n;
  int e = s[d] - 48;
  for (int i = d + 1; i <= n; i++) {
    s[i - 1] = (s[i - 1] - 38 - e) % 10 + 48;
    ret = min(ret, solve(d + 1, s) + 1);
  }
  return ret;
}
int main() {
  string s, t;
  while (cin >> n, n) {
    cin >> s >> t;
    for (int i = 0; i < n; i++)
      s[i] = (t[i] - s[i] + 10) % 10 + 48;
    cout << solve(0, s) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int n;
string s, t;
int main() {
  while (cin >> n, n) {
    cin >> s >> t;
    for (int i = 0; i < n; i++)
      s[i] = (t[i] - s[i] + 10) % 10 + 48;
    int ret = n;
    vector<int> p(n);
    for (int i = 0; i < n; i++)
      p[i] = i;
    do {
      t = s;
      vector<int> e(n);
      for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
          if (p[i] < p[j])
            e[i]++;
        }
      }
      int cnt = 0;
      for (int i = 0; i < n; i++) {
        int d = (t[i] - 48) % 10;
        if (d != 0)
          cnt++;
        for (int j = i; j <= i + e[i]; j++)
          t[j] = (t[j] - 38 - d) % 10 + 48;
      }
      ret = min(ret, cnt);
    } while (next_permutation(p.begin(), p.end()));
    cout << ret << endl;
  }
  return 0;
}
Problem p01275 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
string A, B;
int k;
unordered_map<string, int> mem;

string add(string a, int l, int r, int v) {
  for (int i = l; i <= r; i++)
    a[i] = (a[i] - v + 10) % 10;
  return a;
}

int dfs(int idx, string dial) {
  if (idx == k)
    return 0;
  if (mem.count(dial))
    return mem[dial];
  int res = 1e9;
  for (int i = idx; i <= k; i++)
    res =
        min(res, dfs(idx + 1, add(dial, idx, i, dial[idx])) + (dial[idx] != 0));
  return mem[dial] = res;
}

int main() {
  while (cin >> k, k) {
    cin >> A >> B;
    for (int i = 0; i < k; i++)
      A[i] = (A[i] - B[i] + 10) % 10;
    mem.clear();
    cout << dfs(0, A) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
string A, B;
int k;
map<string, int> mem;

string add(string a, int l, int r, int v) {
  for (int i = l; i <= r; i++)
    a[i] = (a[i] - v + 10) % 10;
  return a;
}

int dfs(int idx, string dial) {
  if (idx == k)
    return 0;
  //  if(mem.count(dial)) return mem[dial];
  int res = 1e9;
  for (int i = idx; i <= k; i++)
    res =
        min(res, dfs(idx + 1, add(dial, idx, i, dial[idx])) + (dial[idx] != 0));
  //  return mem[dial]=res;
  return res;
}

int main() {

  long long cnt = 0;
  while (cin >> k, k) {
    cin >> A >> B;
    for (int i = 0; i < k; i++)
      A[i] = (A[i] - B[i] + 10) % 10;
    mem.clear();
    cout << dfs(0, A) << endl;
  }
  return 0;
}
Problem p01275 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF INT_MAX / 3

int n;
char s[22], t[22];
int res;

void dfs(int d, int nc) {
  int idx = d;
  while (idx < n && s[idx] == t[idx])
    idx++;
  if (idx == n) {
    minch(res, nc);
    return;
  }
  if (nc > res)
    return;

  int add = (t[idx] - s[idx] + 10) % 10;
  string tmp;
  repl(i, idx, n) tmp += s[i];
  repl(i, idx, n) {
    s[i] = (char)('0' + (s[i] - '0' + add) % 10);
    dfs(idx, nc + 1);
  }
  repl(i, idx, n) s[i] = tmp[i - idx];
  return;
}

int main() {
  cin.sync_with_stdio(false);
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    cin >> s >> t;
    res = INF;
    dfs(0, 0);
    cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcount

#define INF INT_MAX / 3

int n;
char s[22], t[22];

int dfs(int d) {
  int idx = d;
  while (idx < n && s[idx] == t[idx])
    idx++;
  if (idx == n)
    return 0;

  int res = INF;
  int add = (t[idx] - s[idx] + 10) % 10;
  char tmp[10];
  repl(i, idx, n) tmp[i] = s[i];
  repl(i, idx, n) {
    s[i] = (char)('0' + (s[i] - '0' + add) % 10);
    minch(res, dfs(idx) + 1);
  }
  repl(i, idx, n) s[i] = tmp[i];
  return res;
}

int main() {
  cin.sync_with_stdio(false);
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    cin >> s >> t;
    cout << dfs(0) << endl;
  }
  return 0;
}
Problem p01275 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <set>
#include <string>
using namespace std;

class Str {
public:
  string s;
  int c;
  Str(string s, int c) : s(s), c(c) {}

  bool operator<(const Str &sr) const {
    if (s != sr.s)
      return s < sr.s;
    return c < sr.c;
  }
};

int K, lim;
string sor, dis;
set<Str> st;

string rot(int p, int n, string d) {
  string ret = d;

  int t = (ret[p] - '0') + n;
  if (t < 0)
    t += 10;
  ret[p] = (t % 10) + '0';

  return ret;
}

bool dfs(string rt, int p, int c) {
  if (p == K)
    return true;

  int m;
  while (1) {
    m = (dis[p] - rt[p]);
    if (m != 0)
      break;
    p++;
    if (p == K)
      return true;
  }

  if (c >= lim)
    return false;

  string t = rt;
  for (int j = p; j < K; j++) {
    t = rot(j, m, t);
    if (!st.insert(Str(t, c)).second)
      continue;

    if (dfs(t, p + 1, c + 1))
      return true;
  }

  return false;
}
int main() {
  while (cin >> K, K) {
    cin >> sor >> dis;

    for (lim = 0;; lim++) {
      st.clear();
      if (dfs(sor, 0, 0))
        break;
    }

    cout << lim << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
#include <string>
using namespace std;

class Str {
public:
  string s;
  int c;
  Str(string s, int c) : s(s), c(c) {}

  bool operator<(const Str &sr) const {
    if (s != sr.s)
      return s < sr.s;
    return c < sr.c;
  }
};

int K, lim;
string sor, dis;
set<Str> st;

string rot(int p, int n, string d) {
  string ret = d;

  int t = (ret[p] - '0') + n;
  if (t < 0)
    t += 10;
  ret[p] = (t % 10) + '0';

  return ret;
}

int dist(string str) {
  bool dig[10] = {0}, dig2[10] = {0};
  for (int i = 0; i < K; i++) {
    if (str[i] == dis[i])
      continue;
    int l = str[i] - '0', r = dis[i] - '0';
    dig2[abs(l - r)] = 1;
    if (l == 0)
      l = 10;
    if (r == 0)
      r = 10;
    dig[abs(l - r)] = 1;
  }

  int ret = 0, ret2 = 0;
  for (int i = 0; i < 10; i++) {
    if (dig[i])
      ret++;
    if (dig2[i])
      ret2++;
  }

  return min(ret, ret2);
}

bool dfs(string rt, int c, int pd) {
  if (rt == dis)
    return true;
  if (dist(rt) + c > lim)
    return false;

  for (int i = 0; i < K; i++)
    for (int k = -5; k <= 5; k++) {
      if (k == 0)
        continue;
      string t = rt;
      for (int j = i; j < K; j++) {
        t = rot(j, k, t);
        if (dist(t) + c > pd)
          continue;
        if (!st.insert(Str(t, c)).second)
          continue;

        if (dfs(t, c + 1, min(pd, dist(t) + c)))
          return true;
      }
    }

  return false;
}
int main() {
  while (cin >> K, K) {
    cin >> sor >> dis;

    for (lim = 1;; lim++) {
      st.clear();
      if (dfs(sor, 0, 999))
        break;
    }

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

#include <iostream>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int k, s[10], g[10], ans;
void dfs(int c, int step) {
  if (c == k) {
    if (ans > step)
      ans = step;
  } else if (s[c] == g[c])
    dfs(c + 1, step);
  else {
    for (int e = c + 1; e <= k; e++) {
      int mv = (g[c] + 10 - s[c]) % 10;
      for (int i = c; i < e; i++)
        s[i] = (s[i] + mv) % 10;
      dfs(c + 1, step + 1);
      for (int i = c; i < e; i++)
        s[i] = (s[i] + 10 - mv) % 10;
    }
  }
}
int main() {
  while (cin >> k, k) {
    char buf[11];
    cin >> buf;
    rep(i, k) s[i] = buf[i] - '0';
    cin >> buf;
    rep(i, k) g[i] = buf[i] - '0';
    ans = 10;
    dfs(0, 0);
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef vector<int> vi;
int k;
int diff(vi &a, vi &b) {
  int r = 0;
  rep(i, k) r += a[i] != b[i];
  return r;
}
int main() {
  while (cin >> k, k) {
    vi st(k), gl(k);
    char buf[11];
    cin >> buf;
    rep(i, k) st[i] = buf[i] - '0';
    cin >> buf;
    rep(i, k) gl[i] = buf[i] - '0';

    queue<vi> Q;
    map<vi, int> cost;
    vi c(k), nx(k);
    int maxd = k; // for blunch-cut
    // DFS
    Q.push(st);
    cost[st] = 0;
    while (!Q.empty() && cost.find(gl) == cost.end()) {
      c = Q.front();
      Q.pop();
      int nstp = cost[c] + 1;
      rep(s, k) for (int e = s + 1; e <= k; e++) for (int mv = 1; mv <= 9;
                                                      mv++) {
        rep(i, k) nx[i] = s <= i && i < e ? (c[i] + mv) % 10 : c[i];
        int cd = diff(nx, gl);
        if (Q.empty() || cost.find(nx) == cost.end() && cd <= maxd) {
          if (cd < maxd)
            maxd = cd;
          Q.push(nx);
          cost[nx] = nstp;
        }
      }
    }
    cout << cost[gl] << endl;
  }
  return 0;
}