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

#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;

pair<int, int> hb(const vector<int> &l, const vector<int> &r, int K) {
  int hit = 0;
  int blow = 0;
  REP(i, K) if (l[i] == r[i])++ hit;
  vector<int> cnt(11, 0);
  REP(i, K)++ cnt[l[i]];
  REP(i, K) if (cnt[r[i]])++ blow;
  return make_pair(hit, blow - hit);
}

vector<int> search(const vector<int> &v,
                   const vector<tuple<vector<int>, int, int>> &hint, int l) {
  if (v.size() == l) {
    REP(i, hint.size()) {
      vector<int> hi;
      int h, b;
      tie(hi, h, b) = hint[i];
      if (hb(v, hi, l) != make_pair(h, b))
        return vector<int>();
    }
    return v;
  } else {
    vector<int> vv(l, 11);
    REP(i, v.size()) vv[i] = v[i];
    REP(i, hint.size()) {
      vector<int> hi;
      int h, b, hv, bv, hvv, bvv;
      tie(hi, h, b) = hint[i];
      tie(hvv, bvv) = hb(vv, hi, l);
      if (hvv > h || bvv > b)
        return vector<int>();
      int rem = h - hvv + b - bvv;
      if (rem > l - v.size())
        return vector<int>();
    }
    vector<int> cnt(10);
    REP(i, v.size())++ cnt[v[i]];
    vector<int> ans;
    REP(i, 10) {
      if (cnt[i] == 0) {
        auto cp = v;
        cp.push_back(i);
        auto res = search(cp, hint, l);
        if (res.empty())
          continue;
        if (ans.empty())
          ans = res;
        else
          throw "hoge";
      }
    }
    return ans;
  }
}

int main() {
  while (1) {
    int l, h;
    cin >> l >> h;
    if (!l)
      break;
    vector<tuple<vector<int>, int, int>> hint;
    REP(i, h) {
      string tr;
      int hit, blow;
      cin >> tr >> hit >> blow;
      vector<int> trv(l);
      REP(j, l) trv[j] = tr[j] - '0';
      hint.emplace_back(trv, hit, blow);
    }
    vector<int> v;
    try {
      auto ans = search(v, hint, l);
      if (ans.empty()) {
        cout << "NO" << endl;
      } else {
        for (auto i : ans)
          cout << i;
        cout << endl;
      }
    } catch (...) {
      cout << "NO" << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;

pair<int, int> hb(const vector<int> &l, const vector<int> &r, int K) {
  int hit = 0;
  int blow = 0;
  REP(i, K) if (l[i] == r[i])++ hit;
  vector<int> cnt(11, 0);
  REP(i, K)++ cnt[l[i]];
  REP(i, K) if (cnt[r[i]])++ blow;
  return make_pair(hit, blow - hit);
}

vector<int> search(const vector<int> &v,
                   const vector<tuple<vector<int>, int, int>> &hint, int l) {
  if (v.size() == l) {
    REP(i, hint.size()) {
      vector<int> hi;
      int h, b;
      tie(hi, h, b) = hint[i];
      if (hb(v, hi, l) != make_pair(h, b))
        return vector<int>();
    }
    return v;
  } else {
    REP(i, hint.size()) {
      vector<int> hi;
      int h, b, hv, bv;
      tie(hi, h, b) = hint[i];
      tie(hv, bv) = hb(v, hi, v.size());
      if (hv > h || bv > b)
        return vector<int>();
    }
    vector<int> cnt(10);
    REP(i, v.size())++ cnt[v[i]];
    vector<int> ans;
    REP(i, 10) {
      if (cnt[i] == 0) {
        auto cp = v;
        cp.push_back(i);
        auto res = search(cp, hint, l);
        if (res.empty())
          continue;
        if (ans.empty())
          ans = res;
        else
          throw "hoge";
      }
    }
    return ans;
  }
}

int main() {
  while (1) {
    int l, h;
    cin >> l >> h;
    if (!l)
      break;
    vector<tuple<vector<int>, int, int>> hint;
    REP(i, h) {
      string tr;
      int hit, blow;
      cin >> tr >> hit >> blow;
      vector<int> trv(l);
      REP(j, l) trv[j] = tr[j] - '0';
      hint.emplace_back(trv, hit, blow);
    }
    vector<int> v;
    try {
      auto ans = search(v, hint, l);
      if (ans.empty()) {
        cout << "NO" << endl;
      } else {
        for (auto i : ans)
          cout << i;
        cout << endl;
      }
    } catch (...) {
      cout << "NO" << endl;
    }
  }
  return 0;
}