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

#include <iostream>
#include <string>
#include <vector>

using namespace std;

typedef unsigned long long ull;
#define loop(i, a, b) for (int i = (a); i < ull(b); ++i)
#define rep(i, n) loop(i, 0, n)
#define all(a) (a).begin(), (a).end()

bool ok(int i, int j) { return i == j && i < 9; }

int main() {
  string s;
  vector<vector<int>> a, b;
  for (int i = 1; i < 10; i++) {
    vector<int> v;
    for (int j = 0; j < 2; j++)
      v.push_back(i);
    b.push_back(v);
    v.push_back(i);
    a.push_back(v);
  }
  for (int i = 1; i <= 7; i++) {
    vector<int> v;
    for (int j = 0; j < 3; j++)
      v.push_back(i + j);
    a.push_back(v);
  }

  while (cin >> s) {
    int c[10] = {0};
    for (int i = 0; i < s.size(); i++)
      c[s[i] - '0']++;
    string t = "123456789";

    vector<int> ret;
    rep(q, t.size()) {
      c[t[q] - '0']++;
      bool p = true;
      rep(i, 10) if (4 < c[i]) {
        c[t[q] - '0']--;
        p = false;
        break;
      }
      if (!p)
        continue;
      loop(i, 0, a.size()) loop(j, 0, a.size()) {
        if (ok(i, j))
          continue;
        loop(k, 0, a.size()) {
          if (ok(j, k) || ok(i, k))
            continue;
          loop(l, 0, a.size()) {
            if (ok(i, l) || ok(j, l) || ok(k, l))
              continue;
            rep(m, b.size()) {
              if (i == m || j == m || k == m || l == m)
                continue;
              int d[10] = {0};
              rep(n, 3) {
                d[a[i][n]]++;
                d[a[j][n]]++;
                d[a[k][n]]++;
                d[a[l][n]]++;
              }
              rep(n, 2) d[b[m][n]]++;
              bool possible = true;
              rep(n, 10) if (c[n] != d[n]) {
                possible = false;
                break;
              }
              if (possible)
                ret.push_back(q + 1);
            }
          }
        }
      }
      c[t[q] - '0']--;
    }
    int n = -1;
    rep(i, ret.size()) {
      if (n != ret[i]) {
        cout << (i == 0 ? "" : " ") << ret[i];
        n = ret[i];
      }
    }
    if (n == -1)
      cout << 0;
    cout << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

typedef unsigned long long ull;
#define loop(i, a, b) for (int i = (a); i < ull(b); ++i)
#define rep(i, n) loop(i, 0, n)
#define all(a) (a).begin(), (a).end()

bool ok(int i, int j) { return i == j && i < 9; }

int main() {
  string s;
  vector<vector<int>> a, b;
  for (int i = 1; i < 10; i++) {
    vector<int> v;
    for (int j = 0; j < 2; j++)
      v.push_back(i);
    b.push_back(v);
    v.push_back(i);
    a.push_back(v);
  }
  for (int i = 1; i <= 7; i++) {
    vector<int> v;
    for (int j = 0; j < 3; j++)
      v.push_back(i + j);
    a.push_back(v);
  }

  while (cin >> s) {
    int c[10] = {0};
    for (int i = 0; i < s.size(); i++)
      c[s[i] - '0']++;
    string t = "123456789";

    vector<int> ret;
    rep(q, t.size()) {
      c[t[q] - '0']++;
      bool p = true;
      rep(i, 10) if (4 < c[i]) {
        c[t[q] - '0']--;
        p = false;
        break;
      }
      if (!p)
        continue;
      loop(i, 0, a.size()) loop(j, 0, a.size()) {
        if (ok(i, j))
          continue;
        loop(k, 0, a.size()) {
          if (ok(j, k) || ok(i, k))
            continue;
          loop(l, 0, a.size()) {
            if (ok(i, l) || ok(j, l) || ok(k, l))
              continue;
            rep(m, b.size()) {
              if (i == m || j == m || k == m || l == m)
                continue;
              int d[10] = {0};
              rep(n, 3) {
                d[a[i][n]]++;
                d[a[j][n]]++;
                d[a[k][n]]++;
                d[a[l][n]]++;
              }
              rep(n, 2) d[b[m][n]]++;
              bool possible = true;
              rep(n, 10) if (c[n] != d[n]) possible = false;
              if (possible)
                ret.push_back(q + 1);
            }
          }
        }
      }
      c[t[q] - '0']--;
    }
    int n = -1;
    rep(i, ret.size()) {
      if (n != ret[i]) {
        cout << (i == 0 ? "" : " ") << ret[i];
        n = ret[i];
      }
    }
    if (n == -1)
      cout << 0;
    cout << endl;
  }
}