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

#include <iostream>
#include <utility>
#include <vector>
using namespace std;

string itos(int a) {
  string s;
  int k = 1000;
  for (int i = 0; i < 4; i++) {
    s.push_back(a / k + '0');
    a -= (a / k) * k;
    k /= 10;
  }
  return s;
}

pair<int, int> hitblow(string a, string b) {
  pair<int, int> res;
  int c = 0;
  bool ca[10], cb[10];
  for (int i = 0; i < 10; i++) {
    ca[i] = false;
    cb[i] = false;
  }
  for (int i = 0; i < (int)a.size(); i++) {
    if (a[i] == b[i])
      c++;
    ca[a[i] - '0'] = true;
    cb[b[i] - '0'] = true;
  }

  res.first = c;
  c = 0;
  for (int i = 0; i < 10; i++) {
    if (ca[i] && cb[i])
      c++;
  }
  res.second = c - res.first;

  return res;
}

int main() {
  bool check[10000], hoge[10000];
  string str;
  int h, b, n;

  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      for (int k = 0; k < 10; k++) {
        for (int l = 0; l < 10; l++) {
          if (i == j || i == k || i == l || j == k || j == l || k == l) {
            hoge[i * 1000 + j * 100 + k * 10 + l] = false;
          } else {
            hoge[i * 1000 + j * 100 + k * 10 + l] = true;
          }
        }
      }
    }
  }

  while (1) {
    cin >> n;
    if (!n)
      break;

    for (int i = 0; i < 10000; i++)
      check[i] = hoge[i];

    for (int i = 0; i < n; i++) {
      cin >> str >> h >> b;
      for (int i = 0; i < 10000; i++) {
        if (check[i]) {
          pair<int, int> tmp;
          tmp = hitblow(itos(i), str);
          check[i] = (tmp.first == h) && (tmp.second == b);
        }
      }
    }

    int c = 0;
    for (int i = 0; i < 10000; i++) {
      if (check[i])
        c++;
    }
    if (c == 1) {
      for (int i = 0; i < 10000; i++) {
        if (check[i])
          cout << itos(i) << endl;
      }
    } else {
      vector<int> can;
      for (int i = 0; i < 10000; i++) {
        if (check[i])
          can.push_back(i);
      }
      int i;
      for (i = 0; i < 10000; i++) {
        if (hoge[i]) {
          int hb[5][5];
          for (int j = 0; j < 5; j++) {
            for (int k = 0; k < 5; k++)
              hb[j][k] = 0;
          }
          bool f = true;
          for (int j = 0; j < (int)can.size(); j++) {
            pair<int, int> tmp;
            tmp = hitblow(itos(i), itos(can[j]));
            hb[tmp.first][tmp.second]++;
            if (hb[tmp.first][tmp.second] >= 2) {
              f = false;
              break;
            }
          }

          if (f) {
            cout << itos(i) << endl;
            break;
          }
        }
      }
      if (i == 10000)
        cout << "????" << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <utility>
#include <vector>
using namespace std;

string itos(int a) {
  string s;
  int k = 1000;
  for (int i = 0; i < 4; i++) {
    s.push_back(a / k + '0');
    a -= (a / k) * k;
    k /= 10;
  }
  return s;
}

pair<int, int> hitblow(string a, string b) {
  pair<int, int> res;
  int c = 0;
  bool ca[10], cb[10];
  for (int i = 0; i < 10; i++) {
    ca[i] = false;
    cb[i] = false;
  }
  for (int i = 0; i < (int)a.size(); i++) {
    if (a[i] == b[i])
      c++;
    ca[a[i] - '0'] = true;
    cb[b[i] - '0'] = true;
  }

  res.first = c;
  c = 0;
  for (int i = 0; i < 10; i++) {
    if (ca[i] && cb[i])
      c++;
  }
  res.second = c - res.first;

  return res;
}

int main() {
  bool check[10000], hoge[10000];
  string str;
  int h, b, n;

  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
      for (int k = 0; k < 10; k++) {
        for (int l = 0; l < 10; l++) {
          if (i == j || i == k || i == l || j == k || j == l || k == l) {
            hoge[i * 1000 + j * 100 + k * 10 + l] = false;
          } else {
            hoge[i * 1000 + j * 100 + k * 10 + l] = true;
          }
        }
      }
    }
  }

  while (1) {
    cin >> n;
    if (!n)
      break;

    for (int i = 0; i < 10000; i++)
      check[i] = hoge[i];

    for (int i = 0; i < n; i++) {
      cin >> str >> h >> b;
      for (int i = 0; i < 10000; i++) {
        if (check[i]) {
          pair<int, int> tmp;
          tmp = hitblow(itos(i), str);
          check[i] = (tmp.first == h) && (tmp.second == b);
        }
      }
    }
    // for(int i=0;i<10000;i++)if(check[i])cout << itos(i) << endl;
    int c = 0;
    for (int i = 0; i < 10000; i++) {
      if (check[i])
        c++;
    }
    if (c == 1) {
      for (int i = 0; i < 10000; i++) {
        if (check[i])
          cout << itos(i) << endl;
      }
    } else {
      vector<int> can;
      for (int i = 0; i < 10000; i++) {
        if (check[i])
          can.push_back(i);
      }
      int i;
      for (i = 0; i < 10000; i++) {
        if (hoge[i]) {
          int hb[5][5];
          for (int j = 0; j < 5; j++) {
            for (int k = 0; k < 5; k++)
              hb[j][k] = 0;
          }
          for (int j = 0; j < (int)can.size(); j++) {
            pair<int, int> tmp;
            tmp = hitblow(itos(i), itos(can[j]));
            // cout << tmp.first << " " << tmp.second << endl;
            hb[tmp.first][tmp.second]++;
          }
          bool f = true;
          for (int j = 0; j < 5; j++) {
            for (int k = 0; k < 5; k++) {
              if (hb[j][k] >= 2)
                f = false;
            }
          }
          if (f) {
            cout << itos(i) << endl;
            break;
          }
        }
      }
      if (i == 10000)
        cout << "????" << endl;
    }
  }
}