Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
#define MAXS (5000 + 5)
#define MAXN (25000000 + 5)
#define MAXC (26)
#define cint const int
#define rint register int
#define LL long long
#define pii pair<int, int>
#define pci pair<char, int>
using namespace std;
cint root = 0;
struct cmp {
  string s;
  int bh;
};
struct node {
  int sum, next[MAXC], is_end, fa;
  node() : sum(0), is_end(0) { memset(next, -1, sizeof(next)); }
  node(int dqs, int dqi) : sum(dqs), is_end(dqi) {
    memset(next, -1, sizeof(next));
  }
};
vector<node> b;
void ins(char *s, int n) {
  static int cnt = 0;
  int dq = root;
  for (int i = 0; i < n; i++) {
    if (b[dq].next[s[i] - 'a'] == -1) {
      b.push_back(node());
      b[dq].next[s[i] - 'a'] = ++cnt;
    }
    b[b[dq].next[s[i] - 'a']].fa = dq;
    dq = b[dq].next[s[i] - 'a'];
  }
  int zh = 1 - b[dq].is_end;
  b[dq].is_end = 1;
  for (; dq != root; dq = b[dq].fa)
    b[dq].sum += zh;
  b[root].sum += zh;
}
void solve(int dq, int k) {
  if (b[dq].is_end == 1 && k == 1)
    return;
  int dqs = b[dq].is_end;
  for (int i = 0; i < MAXC; i++) {
    if (b[dq].next[i] != -1 && dqs + b[b[dq].next[i]].sum >= k) {
      printf("%c", 'a' + i);
      solve(b[dq].next[i], k - dqs);
      return;
    }
    if (b[dq].next[i] != -1)
      dqs += b[b[dq].next[i]].sum;
  }
}
bool comp(cmp x, cmp y) { return x.s < y.s; }
int main() {
  char s[MAXS];
  b.push_back(node());
  cmp sors[MAXS];
  int k, n;
  scanf("%s%d", s, &k);
  n = strlen(s);
  for (int i = 0; i < n; i++) {
    sors[i].bh = i;
    for (int j = i; j < n; j++)
      sors[i].s += s[j];
  }
  sort(sors, sors + n, comp);
  for (int i = 0; i < n; i++)
    for (int j = sors[i].bh; j < n; j++) {
      if (b[root].sum >= k * 1000) {
        solve(root, k);
        return 0;
      }
      ins(s + sors[i].bh, j - sors[i].bh + 1);
    }
  solve(root, k);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
#define MAXS (5000 + 5)
#define MAXN (25000000 + 5)
#define MAXC (26)
#define cint const int
#define rint register int
#define LL long long
#define pii pair<int, int>
#define pci pair<char, int>
using namespace std;
cint root = 0;
struct cmp {
  string s;
  int bh;
};
struct node {
  int sum, next[MAXC], is_end, fa;
  node() : sum(0), is_end(0) { memset(next, -1, sizeof(next)); }
  node(int dqs, int dqi) : sum(dqs), is_end(dqi) {
    memset(next, -1, sizeof(next));
  }
};
vector<node> b;
void ins(char *s, int n) {
  static int cnt = 0;
  int dq = root;
  for (int i = 0; i < n; i++) {
    if (b[dq].next[s[i] - 'a'] == -1) {
      b.push_back(node());
      b[dq].next[s[i] - 'a'] = ++cnt;
    }
    b[b[dq].next[s[i] - 'a']].fa = dq;
    dq = b[dq].next[s[i] - 'a'];
  }
  int zh = 1 - b[dq].is_end;
  b[dq].is_end = 1;
  for (; dq != root; dq = b[dq].fa)
    b[dq].sum += zh;
  b[root].sum += zh;
}
void solve(int dq, int k) {
  if (b[dq].is_end == 1 && k == 1)
    return;
  int dqs = b[dq].is_end;
  for (int i = 0; i < MAXC; i++) {
    if (b[dq].next[i] != -1 && dqs + b[b[dq].next[i]].sum >= k) {
      printf("%c", 'a' + i);
      solve(b[dq].next[i], k - dqs);
      return;
    }
    if (b[dq].next[i] != -1)
      dqs += b[b[dq].next[i]].sum;
  }
}
bool comp(cmp x, cmp y) { return x.s < y.s; }
int main() {
  char s[MAXS];
  b.push_back(node());
  cmp sors[MAXS];
  int k, n;
  scanf("%s%d", s, &k);
  n = strlen(s);
  for (int i = 0; i < n; i++) {
    sors[i].bh = i;
    for (int j = i; j < n; j++)
      sors[i].s += s[j];
  }
  sort(sors, sors + n, comp);
  for (int i = 0; i < n; i++)
    for (int j = sors[i].bh; j < n; j++) {
      if (b[root].sum >= k) {
        solve(root, k);
        return 0;
      }
      ins(s + sors[i].bh, j - sors[i].bh + 1);
    }
  solve(root, k);
  return 0;
}