Problem p02844 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

// C++14 (GCC 5.4.1)

#define LLINF (1LL << 60)
typedef long long ll;
#define mod 1000000007

#define repd(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
#define rrepd(i, a, b) for (int i = (a); i >= (b); i--)

template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

template <typename T> inline string toString(const T &a) {
  ostringstream oss;
  oss << a;
  return oss.str();
}

#define dbg_v1(i, V)                                                           \
  do {                                                                         \
    cout << "-v1----\n";                                                       \
    rep(i, V.size()) cout << " " << i << "\t:" << V[i] << endl;                \
    cout << endl;                                                              \
  } while (0)
#define dbg_v2(i, k, V)                                                        \
  do {                                                                         \
    cout << "-v2----\n";                                                       \
    rep(i, V.size()) {                                                         \
      rep(k, V[0].size()) cout << " " << V[i][k];                              \
      cout << endl;                                                            \
    }                                                                          \
  } while (0)
#define lower_bound_idx(V, c)                                                  \
  distance(V.begin(), lower_bound(V.begin(), V.end(), c))

int main() {
  ios::sync_with_stdio(false); // stdoutとcoutの同期解除
  cin.tie(nullptr);            // cinとcoutの同期解除

  int N;
  string S;
  cin >> N >> S;
  vector<vector<int>> nms(10, vector<int>(N, 0));
  vector<int> ls(1000, 0);

  nms[S[N - 1] - '0'][N - 1]++;
  rrepd(i, N - 1, 1) {
    rep(j, 10) nms[j][i - 1] = nms[j][i];
    nms[S[i - 1] - '0'][i - 1]++;
  }
  // dbg_v2(i,k,nms);
  int x = 0, y;
  ll ans = 0;
  rep(i, N - 2) {
    x = (S[i] - '0') * 100;
    repd(j, i + 1, N - 1) {
      y = x + (S[j] - '0') * 10;
      rep(t, 10) {
        if (nms[t][j + 1] > 0) {
          ls[y + t]++;
        }
      }
    }
  }
  rep(i, 1000) if (ls[i] > 0) ans++;

  cout << ans << endl;
  return (0);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

// C++14 (GCC 5.4.1)

#define LLINF (1LL << 60)
typedef long long ll;
#define mod 1000000007

#define repd(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
#define rrepd(i, a, b) for (int i = (a); i >= (b); i--)

template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

template <typename T> inline string toString(const T &a) {
  ostringstream oss;
  oss << a;
  return oss.str();
}

#define dbg_v1(i, V)                                                           \
  do {                                                                         \
    cout << "-v1----\n";                                                       \
    rep(i, V.size()) cout << " " << i << "\t:" << V[i] << endl;                \
    cout << endl;                                                              \
  } while (0)
#define dbg_v2(i, k, V)                                                        \
  do {                                                                         \
    cout << "-v2----\n";                                                       \
    rep(i, V.size()) {                                                         \
      rep(k, V[0].size()) cout << " " << V[i][k];                              \
      cout << endl;                                                            \
    }                                                                          \
  } while (0)
#define lower_bound_idx(V, c)                                                  \
  distance(V.begin(), lower_bound(V.begin(), V.end(), c))

int main() {
  ios::sync_with_stdio(false); // stdoutとcoutの同期解除
  cin.tie(nullptr);            // cinとcoutの同期解除

  int N;
  string S;
  cin >> N >> S;
  vector<vector<int>> nms(10, vector<int>(N, 0));
  vector<int> ls(1000, 0);

  nms[S[N - 1] - '0'][N - 1]++;
  rrepd(i, N - 1, 1) {
    rep(j, 10) nms[j][i - 1] = nms[j][i];
    nms[S[i - 1] - '0'][i - 1]++;
  }
  // dbg_v2(i,k,nms);
  int x = 0, y;
  ll ans = 0;
  rep(i, N - 2) {
    x = (S[i] - '0') * 100;

    rep(t, 10) {
      if (nms[t][i + 1] > 0) {
        int k = i + 1;
        while (k < N - 2 && nms[t][k] == nms[t][k + 1])
          k++;
        if (nms[t][k] == nms[t][k + 1])
          continue;
        rep(u, 10) {
          if (nms[u][k + 1] > 0) {
            ls[x + t * 10 + u]++;
            // cout<<x + t*10 +u<<endl;
          }
        }
      }
    }
  }
  rep(i, 1000) if (ls[i] > 0) ans++;

  cout << ans << endl;
  return (0);
}