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

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF (1e9 + 1)
// #define INF (1LL<<59)

bool isValid(string tmp) {
  int count[300] = {};
  rep(i, tmp.size()) count[tmp[i]]++;
  rep(i, 300) if (count[i] > 4) return false;

  sort(all(tmp));

  rep(i, tmp.size() - 1) {
    if (tmp[i] == tmp[i + 1]) {
      string s = "";
      rep(j, tmp.size()) {
        if (j != i && j != i + 1)
          s += tmp[j];
      }

      vector<bool> used(s.size(), false);

      rep(k, s.size()) {
        if (used[k])
          continue;
        int same = 0, plus1 = 0, plus2 = 0;
        for (int l = k + 1; l < s.size(); l++) {
          if (used[l] == false && s[k] == s[l])
            same++;
          if (used[l] == false && s[k] + 1 == s[l])
            plus1++;
          if (used[l] == false && s[k] + 2 == s[l])
            plus2++;
        }
        if (same >= 2) {
          used[k] = true;
          int c = 0;
          for (int l = k + 1; l < s.size(); l++) {
            if (used[l] == false && s[k] == s[l]) {
              used[l] = true;
              c++;
            }
            if (c >= 2)
              break;
          }
        } else if (plus1 && plus2) {
          used[k] = true;
          for (int l = k + 1; l < s.size(); l++) {
            if (used[l] == false && s[k] + 1 == s[l]) {
              used[l] = true;
              break;
            }
          }
          for (int l = k + 1; l < s.size(); l++) {
            if (used[l] == false && s[k] + 2 == s[l]) {
              used[l] = true;
              break;
            }
          }
        }
      }
      bool f = false;
      rep(j, used.size()) {
        if (used[j] == false)
          f = true;
      }
      if (!f)
        return true;
    }
  }
  return false;
}

int main() {
  string s;
  while (cin >> s) {
    int c = 0;
    for (int i = 1; i <= 9; i++) {
      string tmp = s;
      tmp += '0' + i;
      if (isValid(tmp)) {
        if (c)
          cout << " ";
        cout << i;
        c++;
      }
    }
    if (c == 0)
      cout << 0;
    cout << endl;
  }
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

#define rep(a, b) for (int(a) = 0; (a) < b; (a)++)

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;

bool isok(int a, int b, int c) {
  if (a == b && b == c)
    return true;
  if (a + 2 == b + 1 && b + 1 == c)
    return true;
  return false;
}

int main() {
  string s;
  while (cin >> s) {
    bool used[10] = {};
    int four[10] = {};
    vector<int> data;
    rep(i, s.size()) {
      four[s[i] - '0']++;
      data.push_back(s[i] - '0');
    }
    for (int add = 1; add <= 9; add++) {
      if (four[add] == 4)
        continue;
      vector<int> tmp = data;
      tmp.push_back(add);
      sort(tmp.begin(), tmp.end());
      do {
        bool f = true;
        rep(j, 4) {
          if (!isok(tmp[3 * j + 0], tmp[3 * j + 1], tmp[3 * j + 2]))
            f = false;
        }
        if (tmp[12] != tmp[13])
          f = false;

        if (f) {
          used[add] = true;
          break;
          //					rep(l,13)cout<<tmp[l]<<" ";
          //					cout<<i<<endl;
        }
      } while (next_permutation(tmp.begin(), tmp.end()));
    }
    bool f = true;
    for (int i = 1; i <= 9; i++) {
      if (used[i]) {
        if (!f)
          cout << " ";
        f = false;
        cout << i;
      }
    }
    if (f)
      cout << "0" << endl;
    else
      cout << endl;
  }
  return 0;
}