Problem p03355 (C++)

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

using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1000000000;
const ll LINF = (ll)4000000000000000000;
const ll MOD = (ll)1000000007;
const double PI = acos(1.0);
const int limit = 100010;

#define REP(i, m, n) for (int i = (int)m; i < (int)n; i++)
#define rep(i, n) REP(i, 0, n)
#define rep2(i, n) REP(i, 1, n + 1)
#define rep3(i, n) REP(i, 0, n + 1)
#define REPM(i, m, n) for (int i = (int)m; i >= (int)n; i--)
#define REPLL(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define repll(i, n) REPLL(i, 0, n)
#define repll2(i, n) REPLL(i, 1, n + 1)
#define MP make_pair
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible") << endl
#define NP(v) next_permutation(v.begin(), v.end())
#define Size(s) (int)s.size()

//------------------------------------------------------
// union-find

const int MAX_V = limit;

int par[MAX_V];
int rank_[MAX_V];

void init(int n) {

  rep(i, n) {
    par[i] = i;
    rank_[i] = 0;
  }
}

int root(int x) {
  return par[x] == x ? x : par[x] = root(par[x]); // 経路圧縮
}

bool same(int x, int y) { return root(x) == root(y); }

void unite(int x, int y) {
  x = root(x);
  y = root(y);
  if (x == y)
    return;

  if (rank_[x] < rank_[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (rank_[x] == rank_[y])
      rank_[x]++;
  }
}

//------------------------------------------------------

int main() {
  string s;
  cin >> s;
  int k;
  cin >> k;

  set<string> used;

  rep(n, k) {

    string Min = "zzzzzzzzzzzzzzzzzzzzzzzzzzz";

    rep(i, s.length()) {

      rep2(j, s.length()) {
        if (i + j > s.length())
          continue;
        string sub = s.substr(i, j);
        if (used.count(sub))
          continue;
        if (Min > sub)
          Min = sub;
      }
    }
    if (n == k - 1)
      cout << Min << endl;
    else
      used.insert(Min);
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1000000000;
const ll LINF = (ll)4000000000000000000;
const ll MOD = (ll)1000000007;
const double PI = acos(1.0);
const int limit = 100010;

#define REP(i, m, n) for (int i = (int)m; i < (int)n; i++)
#define rep(i, n) REP(i, 0, n)
#define rep2(i, n) REP(i, 1, n + 1)
#define rep3(i, n) REP(i, 0, n + 1)
#define REPM(i, m, n) for (int i = (int)m; i >= (int)n; i--)
#define REPLL(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define repll(i, n) REPLL(i, 0, n)
#define repll2(i, n) REPLL(i, 1, n + 1)
#define MP make_pair
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible") << endl
#define NP(v) next_permutation(v.begin(), v.end())
#define Size(s) (int)s.size()

//------------------------------------------------------
// union-find

const int MAX_V = limit;

int par[MAX_V];
int rank_[MAX_V];

void init(int n) {

  rep(i, n) {
    par[i] = i;
    rank_[i] = 0;
  }
}

int root(int x) {
  return par[x] == x ? x : par[x] = root(par[x]); // 経路圧縮
}

bool same(int x, int y) { return root(x) == root(y); }

void unite(int x, int y) {
  x = root(x);
  y = root(y);
  if (x == y)
    return;

  if (rank_[x] < rank_[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (rank_[x] == rank_[y])
      rank_[x]++;
  }
}

//------------------------------------------------------

int main() {
  string s;
  cin >> s;
  int k;
  cin >> k;

  set<string> used;

  rep(n, k) {

    string Min = "zzzzzzzzzzzzzzzzzzzzzzzzzzz";

    rep(i, s.length()) {

      rep2(j, 5) {
        if (i + j > s.length())
          continue;
        string sub = s.substr(i, j);
        if (used.count(sub))
          continue;
        if (Min > sub)
          Min = sub;
      }
    }
    if (n == k - 1)
      cout << Min << endl;
    else
      used.insert(Min);
  }

  return 0;
}