Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#define re register
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ms(arr) memset(arr, 0, sizeof(arr))
const int inf = 0x3f3f3f3f;
string s, t, ch[101];
map<string, int> b;
char minn;
int l, num, k, flag, o;
int main() {
  cin >> s;
  cin >> k;
  l = s.length();
  minn = '{';
  for (re int i = 0; i < l - 5; i++) {
    if (s[i] < minn) {
      minn = s[i];
      o = i;
    }
  }
  ch[0] = " ";
  for (re int i = o; i < l; i++) {
    for (re int j = i; j < l; j++) {
      t += s[j];
      if (b[t] == 1)
        continue;
      b[t] = 1;
      if (num != 0) {
        if (num < k) {
          for (re int h = num; h >= 0; h--) {
            if (t > ch[h]) {
              ch[h + 1] = t;
              break;
            } else {
              ch[h + 1] = ch[h];
            }
          }
          num++;
        } else {
          for (re int h = num; h >= 0; h--) {
            if (t > ch[h]) {
              ch[h + 1] = t;
              flag = h;
              break;
            } else {
              ch[h + 1] = ch[h];
            }
          }
          if (flag == num) {
            flag = 0;
            break;
          }
        }
      } else
        ch[++num] = t;
    }
    t = "";
  }
  cout << ch[k];
}

Fixed submission:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#define re register
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ms(arr) memset(arr, 0, sizeof(arr))
const int inf = 0x3f3f3f3f;
string s, t, ch[101];
map<string, int> b;
char minn;
int l, num, k, flag, o;
int main() {
  cin >> s;
  cin >> k;
  l = s.length();
  minn = '{';
  for (re int i = 0; i < l - 5; i++) {
    if (s[i] < minn) {
      minn = s[i];
      o = i;
    }
  }
  ch[0] = " ";
  for (re int i = o; i < l; i++) {
    for (re int j = i; j < min(i + k, l); j++) {
      t += s[j];
      if (b[t] == 1)
        continue;
      b[t] = 1;
      if (num != 0) {
        if (num < k) {
          for (re int h = num; h >= 0; h--) {
            if (t > ch[h]) {
              ch[h + 1] = t;
              break;
            } else {
              ch[h + 1] = ch[h];
            }
          }
          num++;
        } else {
          for (re int h = num; h >= 0; h--) {
            if (t > ch[h]) {
              ch[h + 1] = t;
              flag = h;
              break;
            } else {
              ch[h + 1] = ch[h];
            }
          }
          if (flag == num) {
            flag = 0;
            break;
          }
        }
      } else
        ch[++num] = t;
    }
    t = "";
  }
  cout << ch[k];
}