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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define rp(i, c) rep(i, (c).size())
#define fr(i, c)                                                               \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = 1 << 28;
const double INF = 1e12, EPS = 1e-9;

int l, n, hit[100], blow[100];
bool use[100][10];
char tmp[11], in[100][11], ans[11];

int main() {
  while (scanf("%d%d", &l, &n), l) {
    rep(i, n) {
      scanf("%s%d%d", in[i], hit + i, blow + i);
      rep(j, 10) use[i][j] = 0;
      rep(j, l) use[i][in[i][j] - '0'] = 1;
    }

    int cand = 0, cbit = 0;
    rep(i, 1 << 10) {
      rep(j, n) {
        int cnt = 0;
        rep(k, 10) if ((i & 1 << k) && use[j][k]) cnt++;
        if (cnt != hit[j] + blow[j])
          goto NI;
      }
      cand |= i;
    NI:;
    }
    ans[0] = 0;
    rep(i, 10) if (cand & 1 << i) tmp[cbit++] = i + '0';
    do {
      rep(i, n) {
        int h = 0, b = 0;
        rep(j, l) {
          if (tmp[j] == in[i][j])
            h++;
          else if (use[i][tmp[j] - '0'])
            b++;
        }
        if (h != hit[i] || b != blow[i])
          goto NEXT;
      }
      if (ans[0] != 0) {
        ans[0] = 0;
        goto END;
      }
      rep(i, l) ans[i] = tmp[i];

    NEXT:;
      reverse(tmp + l, tmp + cbit);
    } while (next_permutation(tmp, tmp + cbit));

  END:
    ans[l] = 0;
    if (ans[0])
      printf("%s\\n", ans);
    else
      puts("NO");
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define rp(i, c) rep(i, (c).size())
#define fr(i, c)                                                               \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = 1 << 28;
const double INF = 1e12, EPS = 1e-9;

int l, n, hit[100], blow[100];
bool use[100][10];
char tmp[11], in[100][11], ans[11];

int main() {
  while (scanf("%d%d", &l, &n), l) {
    rep(i, n) {
      scanf("%s%d%d", in[i], hit + i, blow + i);
      rep(j, 10) use[i][j] = 0;
      rep(j, l) use[i][in[i][j] - '0'] = 1;
    }
    ans[0] = 0;
    rep(i, 10) tmp[i] = i + '0';
    do {
      rep(i, n) {
        int h = 0, b = 0;
        rep(j, l) {
          if (tmp[j] == in[i][j])
            h++;
          else if (use[i][tmp[j] - '0'])
            b++;
        }
        if (h != hit[i] || b != blow[i])
          goto NEXT;
      }
      if (ans[0] != 0) {
        ans[0] = 0;
        goto END;
      }
      rep(i, l) ans[i] = tmp[i];

    NEXT:;
      if (l < 9)
        reverse(tmp + l, tmp + 10);
    } while (next_permutation(tmp, tmp + 10));

  END:
    ans[l] = 0;
    if (ans[0])
      printf("%s\\n", ans);
    else
      puts("NO");
  }

  return 0;
}