Problem p03355 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 5050;
const int M = 10050;
char str[N];
int K;

int ch[M][26], step[M], sum[M], val[M], pre[M], root, cnt, last;
void SAM(int inde, int id) {
  int u = last;
  last = ++cnt;
  int now = last;
  step[now] = id;
  val[now] = 1;
  while (u && !ch[u][inde])
    ch[u][inde] = now, u = pre[u];
  if (!u)
    pre[now] = root;
  else {
    int v = ch[u][inde];
    if (step[u] + 1 == step[v])
      pre[now] = v;
    else {
      int av = ++cnt;
      step[av] = step[u] + 1;
      memcpy(ch[av], ch[v], sizeof(ch[v]));
      pre[av] = pre[v];
      pre[v] = pre[now] = av;
      while (u && ch[u][inde] == v)
        ch[u][inde] = av, u = pre[u];
    }
  }
}
int buc[N], sa[M];
void prepare() {
  for (int i = 1; i <= cnt; i++)
    buc[step[i]]++;
  for (int i = 1; i <= cnt; i++)
    buc[i] += buc[i - 1];
  for (int i = 1; i <= cnt; i++)
    sa[buc[step[i]]--] = i;

  for (int i = cnt; i >= 1; i--) {
    int t = sa[i];
    val[pre[t]] = 1;
  }
  val[1] = 0;
  for (int i = cnt; i >= 1; i--) {
    int t = sa[i];
    sum[t] = val[t];
    for (int j = 0; j < 26; j++)
      sum[t] += sum[ch[t][j]];
  }
}
void solve(int now, int k) {
  if (k <= val[now])
    return;
  k -= val[now];
  for (int i = 0; i < 26; i++) {
    if (sum[ch[now][i]] < k)
      k -= sum[ch[now][i]];
    else {
      printf("%c", 'a' + i);
      solve(ch[now][i], k);
      return;
    }
  }
}
int main() {
  scanf("%s", &str[1]);
  scanf("%d", &K);
  int len = strlen(&str[1]);
  last = root = cnt = 1;
  for (int i = 1; i <= len; i++)
    SAM(str[i] - 'a', i);
  prepare();
  solve(1, K);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 5050;
const int M = 10050;
char str[N];
int K;

int ch[M][26], step[M], sum[M], val[M], pre[M], root, cnt, last;
void SAM(int inde, int id) {
  int u = last;
  last = ++cnt;
  int now = last;
  step[now] = id;
  val[now] = 1;
  while (u && !ch[u][inde])
    ch[u][inde] = now, u = pre[u];
  if (!u)
    pre[now] = root;
  else {
    int v = ch[u][inde];
    if (step[u] + 1 == step[v])
      pre[now] = v;
    else {
      int av = ++cnt;
      step[av] = step[u] + 1;
      memcpy(ch[av], ch[v], sizeof(ch[v]));
      pre[av] = pre[v];
      pre[v] = pre[now] = av;
      while (u && ch[u][inde] == v)
        ch[u][inde] = av, u = pre[u];
    }
  }
}
int buc[M], sa[M];
void prepare() {
  for (int i = 1; i <= cnt; i++)
    buc[step[i]]++;
  for (int i = 1; i <= cnt; i++)
    buc[i] += buc[i - 1];
  for (int i = 1; i <= cnt; i++)
    sa[buc[step[i]]--] = i;

  for (int i = cnt; i >= 1; i--) {
    int t = sa[i];
    val[pre[t]] = 1;
  }
  val[1] = 0;
  for (int i = cnt; i >= 1; i--) {
    int t = sa[i];
    sum[t] = val[t];
    for (int j = 0; j < 26; j++)
      sum[t] += sum[ch[t][j]];
  }
}
void solve(int now, int k) {
  if (k <= val[now])
    return;
  k -= val[now];
  for (int i = 0; i < 26; i++) {
    if (sum[ch[now][i]] < k)
      k -= sum[ch[now][i]];
    else {
      printf("%c", 'a' + i);
      solve(ch[now][i], k);
      return;
    }
  }
}
int main() {
  scanf("%s", &str[1]);
  scanf("%d", &K);
  int len = strlen(&str[1]);
  last = root = cnt = 1;
  for (int i = 1; i <= len; i++)
    SAM(str[i] - 'a', i);
  prepare();
  solve(1, K);
  return 0;
}