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

#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>

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

string rotate(string t, int s, int e, int d) {
  string nt(t);
  for (int i = s; i <= e; i++) {
    nt[i] = '0' + (nt[i] - '0' + d) % 10;
  }
  return nt;
}

int main() {
next:
  for (;;) {
    int k;
    cin >> k;
    if (k == 0)
      return 0;
    string from, to;
    cin >> from >> to;
    if (from == to) {
      printf("0\\n");
      continue;
    }
    set<string> vi;
    queue<pair<int, string>> q;
    q.push(make_pair(0, from));
    vi.insert(from);
    while (!q.empty()) {
      int c = q.front().first;
      string v = q.front().second;
      q.pop();
      rep(i, k) if (v[i] != to[i]) {
        for (int j = i; j < k; j++) {
          string nv = rotate(v, i, j, (to[i] - v[i] + 10) % 10);
          if (nv == to) {
            printf("%d\\n", c + 1);
            goto next;
          }
          if (vi.find(nv) != vi.end())
            continue;
          vi.insert(nv);
          q.push(make_pair(c + 1, nv));
        }
        break;
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>

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

long long rotate(long long t, int s, int e, int d) {
  int buf[10];
  rep(i, 10) {
    buf[i] = t % 10;
    t /= 10;
  }
  for (int i = s; i <= e; i++) {
    buf[i] = (buf[i] + d) % 10;
  }
  long long r = 0;
  rep(i, 10) {
    r *= 10;
    r += buf[9 - i];
  }
  return r;
}

int main() {
  for (;;) {
    int k;
    cin >> k;
    if (k == 0)
      return 0;
    string from, to;
    cin >> from >> to;
    long long sf = 0;
    rep(i, k) {
      sf *= 10;
      sf += from[i] - '0';
    }
    long long st = 0;
    rep(i, k) {
      st *= 10;
      st += to[i] - '0';
    }
    if (sf == st) {
      printf("0\\n");
      continue;
    }
    map<long long, int> vif;
    queue<pair<int, long long>> qf;
    qf.push(make_pair(0, sf));
    vif[sf] = 0;
    map<long long, int> vit;
    queue<pair<int, long long>> qt;
    qt.push(make_pair(0, st));
    vit[st] = 0;
    while (!qf.empty() || !qt.empty()) {
      if (!qf.empty()) {
        int vc = qf.front().first;
        long long vs = qf.front().second;
        qf.pop();
        rep(i, k) for (int j = i; j < k; j++) rep(d, 9) {
          long long nvs = rotate(vs, i, j, d + 1);
          if (vit.find(nvs) != vit.end()) {
            printf("%d\\n", vc + 1 + vit[nvs]);
            goto loop_end;
          }
          if (vif.find(nvs) != vif.end())
            continue;
          qf.push(make_pair(vc + 1, nvs));
        }
      }
      if (!qt.empty()) {
        int vc = qt.front().first;
        long long vs = qt.front().second;
        qt.pop();
        rep(i, k) for (int j = i; j < k; j++) rep(d, 9) {
          long long nvs = rotate(vs, i, j, d + 1);
          if (vif.find(nvs) != vif.end()) {
            printf("%d\\n", vc + 1 + vif[nvs]);
            goto loop_end;
          }
          if (vit.find(nvs) != vit.end())
            continue;
          qt.push(make_pair(vc + 1, nvs));
        }
      }
    }
  loop_end:;
  }
}