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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

const double EPS = (1e-10);
typedef long long ll;
typedef pair<int, int> pii;
bool EQ(double a, double b) { return abs((a) - (b)) < EPS; }
void fast_stream() { std::ios_base::sync_with_stdio(0); }

int L, H;
bool sets[101][101];
string nums[101];
int hits[101];
int blows[101];
int resNum[101];
bool used[101];
int cnt;

int ans[101];

void dfs(int pos) {
  if (pos == L) {
    bool ok = true;
    for (int i = 0; i < H; i++)
      if (hits[i] | blows[i])
        ok = false;
    if (!ok)
      return;
    cnt++;
    if (cnt == 1)
      for (int i = 0; i < pos; i++)
        ans[i] = resNum[i];
    return;
  } else {
    for (int i = 0; i < 10; i++) {
      if (used[i])
        continue;
      resNum[pos] = i;
      used[i] = true;
      bool ok = true;
      // ツ鳴オツ渉つチツェツッツク
      for (int j = 0; j < H; j++) {
        if (nums[j][pos] - '0' == i)
          hits[j]--;
        else if (sets[j][i])
          blows[j]--;
        if (hits[j] < 0 || blows[j] < 0)
          ok = false;
      }
      if (ok)
        dfs(pos + 1);
      used[i] = false;
      for (int j = 0; j < H; j++) {
        if (nums[j][pos] - '0' == i)
          hits[j]++;
        else if (sets[j][i])
          blows[j]++;
      }
    }
  }
}

int main() {

  while (cin >> L >> H && (L | H)) {
    cnt = 0;
    memset(sets, 0, sizeof(sets));
    memset(used, 0, sizeof(used));
    for (int i = 0; i < H; i++) {
      cin >> nums[i] >> hits[i] >> blows[i];
      for (int j = 0; j < nums[i].size(); j++)
        sets[i][nums[i][j] - '0'] = true;
    }
    dfs(0);
    if (cnt == 1) {
      for (int i = 0; i < L; i++)
        cout << ans[i];
      cout << endl;
    } else
      cout << "NO" << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int L, H;
string strs[1001];
int hits[1001];
int blows[1001];
int nums[1001];
bool isSelected[1001];

// numsÌgÆtgtðär
bool check(string &tgt, int hit, int blow) {
  int h = 0;
  int b = 0;
  for (int i = 0; i < tgt.size(); i++) {
    if (tgt[i] == nums[i] + '0')
      h++;
    else if (isSelected[tgt[i] - '0'])
      b++;
    if (h > hit || b > blow)
      return false;
  }
  return (h == hit) && (b == blow);
}
int main() {
  while (cin >> L >> H && (L | H)) {
    memset(isSelected, 0, sizeof(isSelected));
    int cnt = 0;
    string obj;
    for (int i = 0; i < H; i++)
      cin >> strs[i] >> hits[i] >> blows[i];
    for (int s = 0; s < (1 << 10); s++) {
      int a = 0;
      for (int i = 0; i < 10; i++) {
        if ((s >> i) & 1) {
          isSelected[i] = true;
          nums[a++] = i;
        } else
          isSelected[i] = false;
      }
      if (a != L)
        continue;
      // ÔðüêÖ¦ÂÂ·×Ä·
      do {
        bool ok = true;
        for (int i = 0; i < H; i++) {
          if (!check(strs[i], hits[i], blows[i])) {
            ok = false;
            break;
          }
        }
        // ·×ÄÌÉÎµÄÃÈçÎ
        if (ok) {
          cnt++;
          if (cnt >= 2)
            break;
          for (int i = 0; i < L; i++)
            obj += (nums[i] + '0');
        }
      } while (next_permutation(nums, nums + L));
      if (cnt >= 2)
        break;
    }
    if (cnt == 1)
      cout << obj << endl;
    else
      cout << "NO" << endl;
  }
  return 0;
}