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

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
// #include<cctype>
#include <climits>
#include <iostream>
#include <map>
#include <string>
#include <vector>
// #include<list>
#include <algorithm>
#include <deque>
#include <queue>
// #include<numeric>
#include <utility>
// #include<memory>
#include <cassert>
#include <functional>
#include <random>
#include <set>
#include <stack>

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;

int N;
vi now, target;

bool dfs(int d, int maxDepth) {
  if (d == maxDepth) {
    if (now == target)
      return true;
    else
      return false;
  }
  int exist = 0;
  for (int i = 0; i < N; i++) {
    int tmp = (10 + target[i] - now[i]) % 10;
    exist |= 1 << tmp;
  }
  int cnt = __builtin_popcount(exist);
  if (cnt / 3 * 3 + (cnt % 3) + d > maxDepth + 1)
    return false;
  for (int i = 0; i < N; i++)
    if (now[i] != target[i]) {
      int diff = (10 + target[i] - now[i]) % 10;
      for (int j = i; j < N; j++) {
        for (int k = i; k <= j; k++) {
          now[k] = (now[k] + diff) % 10;
        }
        if (dfs(d + 1, maxDepth))
          return true;
        for (int k = i; k <= j; k++) {
          now[k] = (now[k] + 10 - diff) % 10;
        }
      }
      break;
    }
  for (int i = N - 1; i >= 0; i--)
    if (now[i] != target[i]) {
      int diff = (10 + target[i] - now[i]) % 10;
      for (int j = 0; j <= i; j++) {
        for (int k = j; k <= i; k++) {
          now[k] = (now[k] + diff) % 10;
        }
        if (dfs(d + 1, maxDepth))
          return true;
        for (int k = j; k <= i; k++) {
          now[k] = (now[k] + 10 - diff) % 10;
        }
      }
      break;
    }
  return false;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (cin >> N) {
    if (N == 0)
      break;
    now.clear();
    target.clear();
    now.resize(N);
    target.resize(N);
    string s, t;
    cin >> s >> t;
    for (int i = 0; i < N; i++) {
      now[i] = (s[i] - '0');
      target[i] = (t[i] - '0');
    }
    int ans = N;
    for (int i = 0; i < ans; i++) {
      if (dfs(0, i)) {
        ans = i;
        break;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
// #include<cctype>
#include <climits>
#include <iostream>
#include <map>
#include <string>
#include <vector>
// #include<list>
#include <algorithm>
#include <deque>
#include <queue>
// #include<numeric>
#include <utility>
// #include<memory>
#include <cassert>
#include <functional>
#include <random>
#include <set>
#include <stack>

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, -1, 0, 1};
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;

int N;
vi now, target;

bool dfs(int d, int maxDepth) {
  if (d == maxDepth) {
    if (now == target)
      return true;
    else
      return false;
  }
  int exist = 0;
  for (int i = 0; i < N; i++) {
    int tmp = (10 + target[i] - now[i]) % 10;
    exist |= 1 << tmp;
  }
  int cnt = __builtin_popcount(exist);
  if (max(cnt * 2 / 3, min(2, cnt)) + d > maxDepth + 1)
    return false;
  for (int i = 0; i < N; i++)
    if (now[i] != target[i]) {
      int diff = (10 + target[i] - now[i]) % 10;
      for (int j = i; j < N; j++) {
        for (int k = i; k <= j; k++) {
          now[k] = (now[k] + diff) % 10;
        }
        if (dfs(d + 1, maxDepth))
          return true;
        for (int k = i; k <= j; k++) {
          now[k] = (now[k] + 10 - diff) % 10;
        }
      }
      break;
    }
  for (int i = N - 1; i >= 0; i--)
    if (now[i] != target[i]) {
      int diff = (10 + target[i] - now[i]) % 10;
      for (int j = 0; j <= i; j++) {
        for (int k = j; k <= i; k++) {
          now[k] = (now[k] + diff) % 10;
        }
        if (dfs(d + 1, maxDepth))
          return true;
        for (int k = j; k <= i; k++) {
          now[k] = (now[k] + 10 - diff) % 10;
        }
      }
      break;
    }
  return false;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (cin >> N) {
    if (N == 0)
      break;
    now.clear();
    target.clear();
    now.resize(N);
    target.resize(N);
    string s, t;
    cin >> s >> t;
    for (int i = 0; i < N; i++) {
      now[i] = (s[i] - '0');
      target[i] = (t[i] - '0');
    }
    int ans = N;
    for (int i = 0; i < ans; i++) {
      if (dfs(0, i)) {
        ans = i;
        break;
      }
    }
    cout << ans << endl;
  }
  return 0;
}