Problem p04045 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all_map(itr, mp) for (auto itr = mp.begin(); itr != mp.end(); itr++)
#define ALL(a) (a).begin(), (a).end()

bool used[10] = {false};

int main() {
  string n;
  int k;
  cin >> n >> k;
  rep(i, k) {
    int d;
    cin >> d;
    used[d] = true;
  }
  string ban, ok;
  rep(i, 10) {
    if (used[i])
      ban.push_back('0' + i);
    else
      ok.push_back('0' + i);
  }

  rep(i, n.size()) if (ban.find(n[i]) != string::npos) {
    char tmp = n[i];
    while (ban.find(n[i]) != string::npos) {
      if (n[i] == '9')
        n[i] = '0';
      else
        n[i]++;
    }
    if (n[i] > tmp) {
      for (int j = i + 1; j < n.size(); ++j)
        n[j] = ok.front();
      break;
    } else {
      for (int j = i - 1; j >= 0; --k) {
        char tmp = n[j];
        if (n[j] == '9')
          n[j] = '0';
        else
          n[j]++;

        while (ban.find(n[j]) != string::npos) {
          if (n[j] == '9')
            n[j] = '0';
          else
            n[j]++;
        }
        if (n[j] > tmp) {
          for (int l = j + 1; l < n.size(); ++l)
            n[l] = ok.front();
          break;
        }
      }
      string ans = "";
      rep(j, n.size() + 1) ans += ok.front();
      if (ans[0] == '0')
        ans[0] = ok[1];
      cout << ans << endl;
      return 0;
    }
  }

  cout << n << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all_map(itr, mp) for (auto itr = mp.begin(); itr != mp.end(); itr++)
#define ALL(a) (a).begin(), (a).end()

bool used[10] = {false};

int main() {
  string n;
  int k;
  cin >> n >> k;
  rep(i, k) {
    int d;
    cin >> d;
    used[d] = true;
  }
  string ban, ok;
  rep(i, 10) {
    if (used[i])
      ban.push_back('0' + i);
    else
      ok.push_back('0' + i);
  }

  rep(i, n.size()) if (ban.find(n[i]) != string::npos) {
    char tmp = n[i];
    while (ban.find(n[i]) != string::npos) {
      if (n[i] == '9')
        n[i] = '0';
      else
        n[i]++;
    }
    if (n[i] > tmp) {
      for (int j = i + 1; j < n.size(); ++j)
        n[j] = ok.front();
      break;
    } else {
      for (int j = i - 1; j >= 0; --j) {
        char tmp = n[j];
        if (n[j] == '9')
          n[j] = '0';
        else
          n[j]++;

        while (ban.find(n[j]) != string::npos) {
          if (n[j] == '9')
            n[j] = '0';
          else
            n[j]++;
        }
        if (n[j] > tmp) {
          for (int l = j + 1; l < n.size(); ++l)
            n[l] = ok.front();
          break;
        }
      }
      string ans = "";
      rep(j, n.size() + 1) ans += ok.front();
      if (ans[0] == '0')
        ans[0] = ok[1];
      cout << ans << endl;
      return 0;
    }
  }

  cout << n << endl;
}