Problem p02844 (C++)

Buggy submission (status: Time Limit Exceeded):
// INCLUDE
//------------------------------------------
#include <algorithm>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>
// DEFINE
//------------------------------------------
#define ll long long
#define ALLv(a) (a).begin(), (a).end()
#define ALL(a, n) (a), (a) + n
#define vi vector<long long>
#define vd vector<long double>
#define vs vector<string>
// CONST
//------------------------------------------
#define INF 1010000000000000017LL
#define MOD 1000000007LL
#define EPS 1e-12
#define PI 3.14159265358979323846
// REPEAT
//------------------------------------------
#define FOR(i, m, n) for (ll(i) = (m); (i) < (n); (i)++)
#define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define REPS(i, x) for (ll(i) = 1; (i) <= (x); (i)++)
#define RREP(i, x) for (ll(i) = (x)-1; (i) >= 0; (i)--)
#define RREPS(i, x) for (ll(i) = (x); (i) > 0; (i)--)
#define WREP(i, in, j, jn) REP(i, in) REP(j, jn)
using namespace std;
//-------------------------------------------

int main(void) {
  ll N;
  string S;
  cin >> N >> S;
  bool flag[1000];
  REP(i, 1000) flag[i] = false;
  ll count = 0;
  ll p = 0;
  REP(i, N - 2) FOR(j, i + 1, N - 1) FOR(k, j + 1, N) {
    p = (S[i] - '0') * 100 + (S[j] - '0') * 10 + (S[k] - '0');
    flag[p] = true;
  }
  REP(i, 1000) {
    if (flag[i])
      count++;
  }
  cout << count;
  return 0;
}


Fixed submission:
// INCLUDE
//------------------------------------------
#include <algorithm>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>
// DEFINE
//------------------------------------------
#define ll long long
#define ALLv(a) (a).begin(), (a).end()
#define ALL(a, n) (a), (a) + n
#define vi vector<long long>
#define vd vector<long double>
#define vs vector<string>
// CONST
//------------------------------------------
#define INF 1010000000000000017LL
#define MOD 1000000007LL
#define EPS 1e-12
#define PI 3.14159265358979323846
// REPEAT
//------------------------------------------
#define FOR(i, m, n) for (ll(i) = (m); (i) < (n); (i)++)
#define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define REPS(i, x) for (ll(i) = 1; (i) <= (x); (i)++)
#define RREP(i, x) for (ll(i) = (x)-1; (i) >= 0; (i)--)
#define RREPS(i, x) for (ll(i) = (x); (i) > 0; (i)--)
#define WREP(i, in, j, jn) REP(i, in) REP(j, jn)
using namespace std;
//-------------------------------------------

int main(void) {
  ll N;
  string S;
  cin >> N >> S;
  bool flag[1000];
  REP(i, 1000) flag[i] = false;
  ll count = 0;
  ll p = 0;
  ll IN, JN, KN;
  bool in[10], jn[10], kn[10];
  REP(i, 10) in[i] = jn[i] = kn[i] = false;
  REP(i, N - 2) {
    IN = S[i] - '0';
    if (!in[IN]) {
      in[IN] = true;
      FOR(j, i + 1, N - 1) {
        JN = S[j] - '0';
        if (!jn[JN]) {
          jn[JN] = true;
          FOR(k, j + 1, N) {
            KN = S[k] - '0';
            if (!kn[KN]) {
              kn[KN] = true;
              p = IN * 100 + JN * 10 + KN;
              flag[p] = true;
            }
          }
          REP(i, 10) kn[i] = false;
        }
      }
      REP(i, 10) jn[i] = false;
    }
  }
  REP(i, 1000) {
    if (flag[i])
      count++;
  }
  cout << count;
  return 0;
}