prithivMLmods/Coder-Stat huggingface dataset:
Problem p03355 (C++)

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

int main() {
  string s;
  int k;
  cin >> s >> k;
  set<string> st;
  int n = s.size();
  for (int i = 0; i < n; ++i)
    for (int j = 1; i + j <= n; ++j) {
      st.insert(s.substr(i, j));
    }
  auto it = st.begin();
  for (int i = 1; i < k; ++i)
    ++it;
  cout << *it << endl;
}

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

int main() {
  string s;
  int k;
  cin >> s >> k;
  set<string> st;
  int n = s.size();
  for (int i = 0; i < n; ++i)
    for (int j = 1; i + j <= n; ++j) {
      string ns = s.substr(i, j);
      if (st.size() > 5) {
        if (*st.rbegin() > ns) {
          st.insert(ns);
          st.erase(*st.rbegin());
        }
      } else
        st.insert(ns);
    }
  auto it = st.begin();
  for (int i = 1; i < k; ++i)
    ++it;
  cout << *it << endl;
}
Problem p03355 (C++)

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

int main() {
  string S;
  cin >> S;
  int K;
  cin >> K;
  vector<string> a;
  for (int i = 0; i < S.size(); i++) {
    for (int j = 1; j <= S.size() - i; j++) {
      a.push_back(S.substr(i, j));
    }
  }

  sort(a.begin(), a.end());
  unique(a.begin(), a.end());

  cout << a[K - 1] << endl;
}


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

int main() {
  string S;
  cin >> S;
  int K;
  cin >> K;
  vector<string> a;
  for (int i = 0; i < S.size(); i++) {
    for (int j = 1; j <= 5 && j <= S.size() - i; j++) {
      a.push_back(S.substr(i, j));
    }
  }

  sort(a.begin(), a.end());
  unique(a.begin(), a.end());

  cout << a[K - 1] << endl;
}
Problem p03355 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

int ri() {
  int n;
  scanf("%d", &n);
  return n;
}

int main() {
  std::string s;
  std::cin >> s;
  int k = ri();

  std::vector<std::string> all;
  for (int i = 0; i < (int)s.size(); i++) {
    std::string cur;
    for (int j = 1; j <= (int)s.size() - i; j++) {
      cur.push_back(s[i + j - 1]);
      all.push_back(cur);
    }
  }
  std::sort(all.begin(), all.end());
  all.erase(std::unique(all.begin(), all.end()), all.end());
  printf("%s\n", all[k - 1].c_str());

  return 0;
}


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

int ri() {
  int n;
  scanf("%d", &n);
  return n;
}

int main() {
  std::string s;
  std::cin >> s;
  int k = ri();

  std::vector<std::string> all;
  for (int i = 0; i < (int)s.size(); i++) {
    std::string cur;
    for (int j = 1; j <= (int)s.size() - i && j <= k; j++) {
      cur.push_back(s[i + j - 1]);
      all.push_back(cur);
    }
  }
  std::sort(all.begin(), all.end());
  all.erase(std::unique(all.begin(), all.end()), all.end());
  printf("%s\n", all[k - 1].c_str());

  return 0;
}
Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <set>
#include <string>

using namespace std;

int main() {

  string str;
  cin >> str;
  int num = 0;
  cin >> num;
  set<string> s;
  int size = str.size();

  for (int i = 0; i < size; i++) {
    for (int j = 1; i <= 5; j++) {
      s.insert(str.substr(i, j));
    }
  }

  int cnt = 0;
  for (auto itr = s.begin(); itr != s.end(); ++itr) {
    cnt++;
    if (cnt == num) {
      cout << *itr << endl;
    }
    if (cnt >= 10)
      break;
  }

  return 0;
}

Fixed submission:
#include <iostream>
#include <set>
#include <string>

using namespace std;

int main() {

  string str;
  cin >> str;
  int num = 0;
  cin >> num;
  set<string> s;
  int size = str.size();

  for (int i = 0; i < size; i++) {
    for (int j = 1; j <= 5; j++) {
      s.insert(str.substr(i, j));
    }
  }

  int cnt = 0;
  for (auto itr = s.begin(); itr != s.end(); ++itr) {
    cnt++;
    if (cnt == num) {
      cout << *itr << endl;
    }
    if (cnt >= 10)
      break;
  }

  return 0;
}
Problem p03355 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
string s;
int k;
string t[30000];
int main(void) {
  cin >> s >> k;
  if (s.size() == 1) {
    cout << s << endl;
    return 0;
  }
  int c = 0;
  for (int i = 0; i < s.size(); i++) {
    string S = "";
    int m;
    if (s.size() <= i + k) {
      m = s.size();
    } else {
      m = i + k;
    }
    for (int j = i; j < m; j++) {
      S += s[j];
      t[c] = S;
      c++;
    }
  }
  sort(t, t + c);
  int I = 1;
  int J = 1;
  while (true) {
    if (t[I] != t[I - 1]) {
      J++;
      if (J == k) {
        cout << t[I] << endl;
        return 0;
      }
    }
    I++;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
string s;
int k;
string t[30000];
int main(void) {
  cin >> s >> k;
  if (s.size() == 1) {
    cout << s << endl;
    return 0;
  }
  int c = 0;
  for (int i = 0; i < s.size(); i++) {
    string S = "";
    int m;
    if (s.size() <= i + k) {
      m = s.size();
    } else {
      m = i + k;
    }
    for (int j = i; j < m; j++) {
      S += s[j];
      t[c] = S;
      c++;
    }
  }
  sort(t, t + c);
  if (k == 1) {
    cout << t[0] << endl;
    return 0;
  }
  int I = 1;
  int J = 1;
  while (true) {
    if (t[I] != t[I - 1]) {
      J++;
      if (J == k) {
        cout << t[I] << endl;
        return 0;
      }
    }
    I++;
  }
}
Problem p03355 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
char s[5005];
int root, last, sz, ch[5005][26], fa[5005], sum[5005], l[5005];
int n, m, b[5005], cnt[5005];
void extend(int x) {
  int c = s[x] - 'a';
  int p = last, np = ++sz;
  last = np;
  l[np] = x + 1;
  for (; p && !ch[p][c]; p = fa[p])
    ch[p][c] = np;
  if (!p)
    fa[np] = root;
  else {
    int q = ch[p][c];
    if (l[p] + 1 == l[q])
      fa[np] = q;
    else {
      int nq = ++sz;
      l[nq] = l[p] + 1;
      memcpy(ch[nq], ch[q], sizeof(ch[q]));
      fa[nq] = fa[q];
      fa[np] = fa[q] = nq;
      for (; p && q == ch[p][c]; p = fa[p])
        ch[p][c] = nq;
    }
  }
}
void calc() {
  for (int i = 1; i <= sz; i++)
    cnt[l[i]]++;
  for (int i = 1; i <= n; i++)
    cnt[i] += cnt[i - 1];
  for (int i = 1; i <= sz; i++)
    b[cnt[l[i]]--] = i;
  for (int i = sz; i; i--) {
    int p = b[i];
    sum[p] = 1;
    for (int j = 0; j < 26; j++)
      sum[p] += sum[ch[p][j]];
  }
}

int main() {
  scanf("%s", s);
  n = strlen(s);
  root = last = ++sz;
  for (int i = 0; i < n; i++)
    extend(i);
  calc();
  int x, p = root;
  scanf("%d", &x);
  while (x) {
    for (int c = 0; c < 26; c++)
      if (ch[p][c]) {
        if (sum[ch[p][c]] >= x) {
          putchar(c + 'a');
          x--;
          p = ch[p][c];
          break;
        } else
          x -= sum[ch[p][c]];
      }
  }
  puts("");
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
char s[50005];
int root, last, sz, ch[50005][26], fa[50005], sum[50005], l[50005];
int n, m, b[50005], cnt[50005];
void extend(int x) {
  int c = s[x] - 'a';
  int p = last, np = ++sz;
  last = np;
  l[np] = x + 1;
  for (; p && !ch[p][c]; p = fa[p])
    ch[p][c] = np;
  if (!p)
    fa[np] = root;
  else {
    int q = ch[p][c];
    if (l[p] + 1 == l[q])
      fa[np] = q;
    else {
      int nq = ++sz;
      l[nq] = l[p] + 1;
      memcpy(ch[nq], ch[q], sizeof(ch[q]));
      fa[nq] = fa[q];
      fa[np] = fa[q] = nq;
      for (; p && q == ch[p][c]; p = fa[p])
        ch[p][c] = nq;
    }
  }
}
void calc() {
  for (int i = 1; i <= sz; i++)
    cnt[l[i]]++;
  for (int i = 1; i <= n; i++)
    cnt[i] += cnt[i - 1];
  for (int i = 1; i <= sz; i++)
    b[cnt[l[i]]--] = i;
  for (int i = sz; i; i--) {
    int p = b[i];
    sum[p] = 1;
    for (int j = 0; j < 26; j++)
      sum[p] += sum[ch[p][j]];
  }
}

int main() {
  scanf("%s", s);
  n = strlen(s);
  root = last = ++sz;
  for (int i = 0; i < n; i++)
    extend(i);
  calc();
  int x, p = root;
  scanf("%d", &x);
  while (x) {
    for (int c = 0; c < 26; c++)
      if (ch[p][c]) {
        if (sum[ch[p][c]] >= x) {
          putchar(c + 'a');
          x--;
          p = ch[p][c];
          break;
        } else
          x -= sum[ch[p][c]];
      }
  }
  puts("");
  return 0;
}
Problem p03355 (C++)

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

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vvvi = vector<vector<vector<int>>>;
using mii = map<int, int>;
using msi = map<string, int>;
using pii = pair<int, int>;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string s;
  int k;
  cin >> s >> k;
  vector<string> ss;
  REP(i, s.size()) {
    for (int j = 1; j <= k || i + j < s.size(); ++j) {
      ss.push_back(s.substr(i, j));
    }
  }
  sort(ss.begin(), ss.end());
  unique(ss.begin(), ss.end());
  cout << ss[k - 1] << endl;

  return 0;
}

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

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vvvi = vector<vector<vector<int>>>;
using mii = map<int, int>;
using msi = map<string, int>;
using pii = pair<int, int>;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string s;
  int k;
  cin >> s >> k;
  vector<string> ss;
  REP(i, s.size()) {
    for (int j = 1; j <= k /* && i + j < s.size()*/; ++j) {
      ss.push_back(s.substr(i, j));
    }
  }
  sort(ss.begin(), ss.end());
  unique(ss.begin(), ss.end());
  cout << ss[k - 1] << endl;

  return 0;
}
Problem p03355 (C++)

Buggy submission (status: Runtime Error):
// In The Name Of God
#include <bits/stdc++.h>

#define sqr(A) ((A) * (A))
#define F first
#define S second
#define mp make_pair
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
const int MOD = 1e9 + 7;
const int MOD2 = 1e9 + 9;
const int HP = 727;
const int INF = INT_MAX;
const ll LINF = LLONG_MAX;

string s;
vector<string> v;
int k;

int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> s >> k;
  for (int i = 0; i < s.size(); i++)
    for (int j = i; j < s.size(); j++)
      v.push_back(s.substr(i, j - i + 1));
  sort(v.begin(), v.end());
  v.resize(unique(v.begin(), v.end()) - v.begin());
  cout << v[k - 1] << endl;
}

Fixed submission:
// In The Name Of God
#include <bits/stdc++.h>

#define sqr(A) ((A) * (A))
#define F first
#define S second
#define mp make_pair
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
const int MOD = 1e9 + 7;
const int MOD2 = 1e9 + 9;
const int HP = 727;
const int INF = INT_MAX;
const ll LINF = LLONG_MAX;

string s;
vector<string> v;
int k;

int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> s >> k;
  for (int sz = 1; sz < 7; sz++)
    for (int i = 0; i <= (int)s.size() - sz; i++)
      v.push_back(s.substr(i, sz));
  sort(v.begin(), v.end());
  v.resize(unique(v.begin(), v.end()) - v.begin());
  cout << v[k - 1] << endl;
}
Problem p03355 (C++)

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

int main() {
  string str;
  int K;
  cin >> str >> K;
  int N;
  N = (int)str.size();

  vector<string> sub;
  for (int i = 0; i < N; i++) {
    for (int j = 1; j < N - i + 1; j++) {
      sub.push_back(str.substr(i, j));
    }
  }
  int v_size;
  v_size = (int)sub.size();
  sort(sub.begin(), sub.end());
  unique(sub.begin(), sub.end());
  cout << sub[K - 1] << "\n";
  return 0;
}


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

int main() {
  string str;
  int K;
  cin >> str >> K;
  int N;
  N = (int)str.size();

  vector<string> sub;
  for (int i = 0; i < N; i++) {
    for (int j = 1; j < K + 1; j++) {
      sub.push_back(str.substr(i, j));
    }
  }
  int v_size;
  v_size = (int)sub.size();
  sort(sub.begin(), sub.end());
  unique(sub.begin(), sub.end());
  cout << sub[K - 1] << "\n";
  return 0;
}
Problem p03355 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define cout16 cout << setprecision(16)
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, f, n) for (int i = f; i < n; i++)
#define SORT(A) sort(A.begin(), A.end())
#define REV(A) reverse(A.begin(), A.end())
typedef long long int ll;
using vi = std::vector<int>;
using vvi = std::vector<std::vector<int>>;
using vll = std::vector<ll>;
using vvll = std::vector<std::vector<ll>>;
using P = std::pair<int, int>;
using vp = std::vector<P>;
using namespace std;
#define INF 1001001001
#define LL_INF 1001001001001001001
#define fi first
#define se second

int main(void) {
  string s;
  cin >> s;
  int n = s.size();
  int k;
  cin >> k;
  vector<string> t;
  for (int l = 0; l < n; l++) {
    for (int r = l; r < n; r++) {
      t.push_back(s.substr(l, r - l + 1));
    }
  }
  SORT(t);
  t.erase(unique(t.begin(), t.end()), t.end());
  cout << t[k - 1] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define cout16 cout << setprecision(16)
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, f, n) for (int i = f; i < n; i++)
#define SORT(A) sort(A.begin(), A.end())
#define REV(A) reverse(A.begin(), A.end())
typedef long long int ll;
using vi = std::vector<int>;
using vvi = std::vector<std::vector<int>>;
using vll = std::vector<ll>;
using vvll = std::vector<std::vector<ll>>;
using P = std::pair<int, int>;
using vp = std::vector<P>;
using namespace std;
#define INF 1001001001
#define LL_INF 1001001001001001001
#define fi first
#define se second

int main(void) {
  string s;
  cin >> s;
  int n = s.size();
  int k;
  cin >> k;
  vector<string> t;
  for (int l = 0; l < n; l++) {
    for (int r = l; r < min(n, l + k); r++) {
      t.push_back(s.substr(l, r - l + 1));
    }
  }
  SORT(t);
  t.erase(unique(t.begin(), t.end()), t.end());
  cout << t[k - 1] << endl;
}
Problem p03355 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int k;
vector<string> ans;
int main() {
  // freopen("readin.txt","r",stdin);
  string s;
  cin >> s;
  scanf("%d", &k);
  for (int i = 0; i < s.size(); i++)
    for (int j = 1; j + i - 1 < s.size(); j++)
      ans.push_back(s.substr(i, j));
  sort(ans.begin(), ans.end());
  ans.resize(unique(ans.begin(), ans.end()) - ans.begin());
  cout << ans[k - 1] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int k;
vector<string> ans;
int main() {
  // freopen("readin.txt","r",stdin);
  string s;
  cin >> s;
  scanf("%d", &k);
  for (int i = 0; i < s.size(); i++)
    for (int j = 1; j <= k && i + j - 1 < s.size(); j++)
      ans.push_back(s.substr(i, j));
  sort(ans.begin(), ans.end());
  ans.resize(unique(ans.begin(), ans.end()) - ans.begin());
  cout << ans[k - 1] << endl;
}
Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author HARSHAD
 */

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const ld PI = 3.14159265358979323846;

#define f(i, n) for (int i = 0; i < n; i++)
#define rep(i, a, b) for (int i = a; i <= b; i++)

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

class CKthSubstring {
public:
  void solve(std::istream &in, std::ostream &out) {
    string s;
    in >> s;
    int k;
    in >> k;
    map<char, vi> Map;
    set<string> Set;
    int n = s.length();
    f(i, n) { Map[s[i]].push_back(i); }
    for (char c = 'a'; c <= 'z'; ++c) {
      rep(len, 1, n) {
        for (int i : Map[c]) {
          if (i + len - 1 >= n)
            break;
          Set.insert(s.substr(i, len));
        }
      }
      if ((int)Set.size() >= k) {
        int cnt = 0;
        for (string w : Set) {
          ++cnt;
          if (cnt == k) {
            out << w;
            return;
          }
        }
      }
    }
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  CKthSubstring solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}


Fixed submission:
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author HARSHAD
 */

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const ld PI = 3.14159265358979323846;

#define f(i, n) for (int i = 0; i < n; i++)
#define rep(i, a, b) for (int i = a; i <= b; i++)

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

class CKthSubstring {
public:
  void solve(std::istream &in, std::ostream &out) {
    string s;
    in >> s;
    int k;
    in >> k;
    map<char, vi> Map;
    set<string> Set;
    int n = s.length();
    f(i, n) { Map[s[i]].push_back(i); }
    for (char c = 'a'; c <= 'z'; ++c) {
      rep(len, 1, k) {
        for (int i : Map[c]) {
          if (i + len - 1 >= n)
            break;
          Set.insert(s.substr(i, len));
        }
      }
      if ((int)Set.size() >= k) {
        int cnt = 0;
        for (string w : Set) {
          ++cnt;
          if (cnt == k) {
            out << w;
            return;
          }
        }
      }
    }
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  CKthSubstring solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}
Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <set>
#include <stdio.h>
#include <string.h>
#define MAX_L 5005

using namespace std;

int n, K;
char s[MAX_L];
set<string> st;

int main() {
  scanf("%s%d", s + 1, &K);
  n = strlen(s + 1);
  for (int i = 1; i <= n; i++) {
    string t;
    for (int j = i; j <= n; j++) {
      t += s[j];
      st.insert(t);
    }
  }
  set<string>::iterator it = st.begin();
  for (int i = 1; i < K; i++)
    it++;
  cout << (*it) << endl;
}


Fixed submission:
#include <iostream>
#include <set>
#include <stdio.h>
#include <string.h>
#define MAX_L 5005

using namespace std;

int n, K;
char s[MAX_L];
set<string> st;

int main() {
  scanf("%s%d", s + 1, &K);
  n = strlen(s + 1);
  for (int i = 1; i <= n; i++) {
    string t;
    for (int j = i; j <= min(n, i + 4); j++) {
      t += s[j];
      st.insert(t);
    }
  }
  set<string>::iterator it = st.begin();
  for (int i = 1; i < K; i++)
    it++;
  cout << (*it) << endl;
}
Problem p03355 (C++)

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

typedef long long ll;
typedef pair<int, int> pii;
#define f(i, n) for (int i = 0; i < (int)(n); ++i)
#define f1(i, x, n) for (int i = x; i < (int)(n); ++i)
const int mod = 1000000007;
const int inf = 2012345678;
const double PI = acos(-1);
const ll infl = 9012345678901234567;
const double eps = 1e-8;
const int N = 1e5;
set<string> se;
int main() {
#if !ONLINE_JUDGE
  freopen("In.txt", "r", stdin);
#endif
  string s;
  int k;
  cin >> s >> k;
  f(i, s.length()) {
    string w = "";
    f1(j, i, i + 5) {
      if (j == s.length())
        break;
      w += s[j];
      se.insert(w);
    }
  }
  set<string>::iterator it = se.begin();
  --k;
  while (k--)
    ++it;
  cout << (*it) << endl;
}

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

typedef long long ll;
typedef pair<int, int> pii;
#define f(i, n) for (int i = 0; i < (int)(n); ++i)
#define f1(i, x, n) for (int i = x; i < (int)(n); ++i)
const int mod = 1000000007;
const int inf = 2012345678;
const double PI = acos(-1);
const ll infl = 9012345678901234567;
const double eps = 1e-8;
const int N = 1e5;
set<string> se;
int main() {
  string s;
  int k;
  cin >> s >> k;
  f(i, s.length()) {
    string w = "";
    f1(j, i, i + 5) {
      if (j == s.length())
        break;
      w += s[j];
      se.insert(w);
    }
  }
  set<string>::iterator it = se.begin();
  --k;
  while (k--)
    ++it;
  cout << (*it) << endl;
}
Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, n) for (int i = 0; i < (n); i++)
#define FORR(x, arr) for (auto &x : arr)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define MEM(a, x) memset(a, x, sizeof(a))
#define ALL(a) a.begin(), a.end()
#define UNIQUE(a) a.erase(unique(ALL(a)), a.end())
typedef long long ll;
typedef pair<int, int> P;

string s;
int k;

int main(int argc, char const *argv[]) {
  ios_base::sync_with_stdio(false);
  cin >> s >> k;
  set<string> st;
  FOR(i, s.size()) {
    for (int j = 1; i + j <= s.size(); j++) {
      string sub = s.substr(i, j);
      st.insert(sub);
    }
  }
  int cnt = 0;
  string ans;
  FORR(x, st) {
    cnt++;
    if (cnt == k) {
      ans = x;
      break;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, n) for (int i = 0; i < (n); i++)
#define FORR(x, arr) for (auto &x : arr)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define MEM(a, x) memset(a, x, sizeof(a))
#define ALL(a) a.begin(), a.end()
#define UNIQUE(a) a.erase(unique(ALL(a)), a.end())
typedef long long ll;
typedef pair<int, int> P;

string s;
int k;

int main(int argc, char const *argv[]) {
  ios_base::sync_with_stdio(false);
  cin >> s >> k;
  set<string> st;
  FOR(i, s.size()) {
    for (int j = 1; i + j <= s.size() && j <= k; j++) {
      string sub = s.substr(i, j);
      st.insert(sub);
    }
  }
  int cnt = 0;
  string ans;
  FORR(x, st) {
    cnt++;
    if (cnt == k) {
      ans = x;
      break;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <string>
using namespace std;
const int maxn = 5e5 + 100;
set<string> st;
char s[maxn];
int main() {
  int k;
  scanf("%s", s);
  scanf("%d", &k);
  int len = strlen(s);
  set<string>::iterator it;
  st.clear();
  for (int i = 0; i < len; i++) {
    string tmp;
    tmp.clear();
    for (int j = i; j < len; j++) {
      tmp.push_back(s[j]);
      if (st.size() == 0 || st.find(tmp) == st.end()) {
        st.insert(tmp);
      }
      while (st.size() > k) {
        it = st.end();
        it--;
        st.erase(*it);
      }
    }
  }
  it = st.end();
  it--;
  cout << *it << endl;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <string>
using namespace std;
const int maxn = 5e5 + 100;
set<string> st;
char s[maxn];
int main() {
  int k;
  scanf("%s", s);
  scanf("%d", &k);
  int len = strlen(s);
  set<string>::iterator it;
  st.clear();
  for (int i = 0; i < len; i++) {
    string tmp;
    tmp.clear();
    for (int j = i; j < len; j++) {
      if (j - i + 1 > k)
        break;
      tmp.push_back(s[j]);
      if (st.size() == 0 || st.find(tmp) == st.end()) {
        st.insert(tmp);
      }
      while (st.size() > k) {
        it = st.end();
        it--;
        st.erase(*it);
      }
    }
  }
  it = st.end();
  it--;
  cout << *it << endl;
}
Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <set>
#include <string>
int main() {
  int i, j, k;
  std::string s;
  std::set<std::string> subs;
  std::cin >> s >> k;
  for (i = 0; i <= s.length(); i++) {
    for (j = 1; i + j <= s.length(); j++) {
      subs.insert(s.substr(i, j));
    }
  }
  for (i = 1; i < k; i++)
    subs.erase(*subs.begin());
  std::cout << *subs.begin();
  return 0;
}

Fixed submission:
#include <iostream>
#include <set>
#include <string>
int main() {
  int i, j, k;
  std::string s;
  std::set<std::string> subs;
  std::cin >> s >> k;
  for (i = 0; i < s.length(); i++) {
    for (j = 1; j <= 5; j++) {
      subs.insert(s.substr(i, j));
    }
  }
  for (i = 1; i < k; i++)
    subs.erase(*subs.begin());
  std::cout << *subs.begin();
  return 0;
}
Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <set>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
signed main() {
  string s;
  ll k, t = 0;
  set<string> list;
  cin >> s >> k;
  for (int i = 0; i < s.size(); i++) {
    for (int j = 1; i + j <= s.size(); j++) {
      list.insert(s.substr(i, j));
    }
  }
  auto tmp = list.begin();
  for (int i = 1; i < k; i++) {
    tmp++;
  }
  cout << *tmp << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <set>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
signed main() {
  string s;
  ll k, t = 0;
  set<string> list;
  cin >> s >> k;
  for (int i = 0; i < s.size(); i++) {
    for (int j = 1; i + j <= s.size() && j <= k; j++) {
      list.insert(s.substr(i, j));
    }
  }
  auto tmp = list.begin();
  for (int i = 1; i < k; i++) {
    tmp++;
  }
  cout << *tmp << endl;
}
Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using lint = long long;

int main() {
  std::string s;
  std::cin >> s;

  lint k;
  scanf("%lld", &k);

  std::set<std::string> subs;

  for (int i = 0; i < s.size(); ++i) {
    for (int j = 0; j < k || i + j < s.size(); j++) {
      subs.insert(s.substr(i, j + 1));
    }
  }

  auto ans_iter = subs.begin();

  for (int i = 0; i < k - 1; ++i) {
    ans_iter++;
  }

  std::cout << *ans_iter << "\n";

  return 0;
}


Fixed submission:
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using lint = long long;

int main() {
  std::string s;
  std::cin >> s;

  lint k;
  scanf("%lld", &k);

  std::set<std::string> subs;

  for (int i = 0; i < s.size(); ++i) {
    for (int j = 0; j < k && i + j < s.size(); j++) {
      subs.insert(s.substr(i, j + 1));
    }
  }

  auto ans_iter = subs.begin();

  for (int i = 0; i < k - 1; ++i) {
    ans_iter++;
  }

  std::cout << *ans_iter << "\n";

  return 0;
}
Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) range(i, 0, n)
#define range(i, a, n) for (int i = a; i < (n); i++)
using namespace std;

int main() {
  int k;
  string s;
  cin >> s >> k;
  vector<set<int>> a(26);
  rep(i, s.size()) a[s[i] - 'a'].insert(i);

  set<string> ans;
  rep(p, 26) {
    range(l, 1, s.size() + 1) {
      for (auto i : a[p])
        if (i + l <= s.size())
          ans.insert(s.substr(i, l));
    }
    if (ans.size() >= k)
      break;
  }
  auto itr = ans.begin();
  rep(i, k - 1) itr++;
  cout << *itr << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) range(i, 0, n)
#define range(i, a, n) for (int i = a; i < (n); i++)
using namespace std;

int main() {
  int k;
  string s;
  cin >> s >> k;
  vector<set<int>> a(26);
  rep(i, s.size()) a[s[i] - 'a'].insert(i);

  set<string> ans;
  rep(p, 26) {
    range(l, 1, k + 1) {
      for (auto i : a[p])
        if (i + l <= s.size())
          ans.insert(s.substr(i, l));
    }
    if (ans.size() >= k)
      break;
  }
  auto itr = ans.begin();
  rep(i, k - 1) itr++;
  cout << *itr << endl;
  return 0;
}
Problem p03355 (C++)

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

#define ll long long
#define debug(x) cerr << fixed << #x << " is " << x << endl;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  string s;
  int k;
  cin >> s >> k;
  set<string> m;
  int n = s.size();

  for (int i = 0; i < n; i++)
    for (int j = 1; j <= n - i; j++)
      m.insert(s.substr(i, j));
  for (auto i : m) {
    k--;
    if (k == 0) {
      cout << i << endl;
      return 0;
    }
  }

  return 0;
}


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

#define ll long long
#define debug(x) cerr << fixed << #x << " is " << x << endl;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  string s;
  int k;
  cin >> s >> k;
  set<string> m;
  int n = s.size();

  for (int i = 0; i < n; i++)
    for (int j = 1; j <= min(5, n - i); j++)
      m.insert(s.substr(i, j));
  for (auto i : m) {
    k--;
    if (k == 0) {
      cout << i << endl;
      return 0;
    }
  }

  return 0;
}
Problem p03355 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

int main() {
  string s;
  int k;
  cin >> s >> k;
  int n = s.size();

  vector<string> vec;
  rep(i, n) {
    for (int j = 1; j <= n - i; j++) {
      string t = s.substr(i, j);
      vec.push_back(t);
    }
  }
  sort(vec.begin(), vec.end());
  vec.erase(unique(vec.begin(), vec.end()), vec.end());
  cout << vec[k - 1] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

int main() {
  string s;
  int k;
  cin >> s >> k;
  int n = s.size();

  vector<string> vec;
  rep(i, n) {
    for (int j = 1; j <= k; j++) {
      string t = s.substr(i, j);
      vec.push_back(t);
    }
  }
  sort(vec.begin(), vec.end());
  vec.erase(unique(vec.begin(), vec.end()), vec.end());
  cout << vec[k - 1] << endl;
  return 0;
}
Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
/**
 * @copyright (c) 2020 Daisuke Hashimoto
 */

#include <bits/stdc++.h>
using namespace std;
using Pair = pair<int64_t, int64_t>;
constexpr int64_t kInf = INT64_MAX / 2L;

// std::cout << std::setprecision(20) << 1.1 << endl;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string S;
  int64_t K;
  cin >> S >> K;
  const int64_t L = static_cast<int64_t>(S.length());
  vector<int64_t> A[26];
  for (int64_t i = 0; i < L; ++i) {
    A[S[i] - 'a'].emplace_back(i);
  }

  queue<int64_t> q;
  int64_t num = 0;
  for (int64_t a = 0; a < 26; ++a) {
    if (A[a].size() > 0) {
      ++num;
      for (auto &i : A[a]) {
        q.emplace(i);
      }
      if (num > K) {
        break;
      }
    }
  }

  vector<string> result;
  unordered_map<string, int32_t> ump;
  while (!q.empty()) {
    const int64_t i = q.front();
    q.pop();
    for (int64_t j = i; j < L; ++j) {
      string temp = S.substr(i, j - i + 1);
      if (ump.find(temp) == ump.end()) {
        result.emplace_back(temp);
        ump[temp] = 0;
      }
    }
  }
  nth_element(result.begin(), result.begin() + (K - 1), result.end());
  cout << result[K - 1] << endl;
  return 0;
}


Fixed submission:
/**
 * @copyright (c) 2020 Daisuke Hashimoto
 */

#include <bits/stdc++.h>
using namespace std;
using Pair = pair<int64_t, int64_t>;
constexpr int64_t kInf = INT64_MAX / 2L;

// std::cout << std::setprecision(20) << 1.1 << endl;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string S;
  int64_t K;
  cin >> S >> K;
  const int64_t L = static_cast<int64_t>(S.length());
  vector<int64_t> A[26];
  for (int64_t i = 0; i < L; ++i) {
    A[S[i] - 'a'].emplace_back(i);
  }

  queue<int64_t> q;
  int64_t num = 0;
  for (int64_t a = 0; a < 26; ++a) {
    if (A[a].size() > 0) {
      ++num;
      for (auto &i : A[a]) {
        q.emplace(i);
      }
      if (num > K) {
        break;
      }
    }
  }

  vector<string> result;
  unordered_map<string, int32_t> ump;
  while (!q.empty()) {
    const int64_t i = q.front();
    q.pop();
    for (int64_t j = i; j < L && j - i + 1 <= K; ++j) {
      string temp = S.substr(i, j - i + 1);
      if (ump.find(temp) == ump.end()) {
        result.emplace_back(temp);
        ump[temp] = 0;
      }
    }
  }
  nth_element(result.begin(), result.begin() + (K - 1), result.end());
  cout << result[K - 1] << endl;
  return 0;
}
Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define REP(i, n) FOR(i, 0, n - 1)
#define NREP(i, n) FOR(i, 1, n)
using ll = long long;
using pii = pair<int, int>;
using piii = pair<pii, pii>;
const int dx[4] = {0, -1, 1, 0};
const int dy[4] = {-1, 0, 0, 1};
const int INF = 1e9 + 7;
int gcd(int x, int y) {
  if (x < y)
    swap(x, y);
  if (y == 0)
    return x;
  return gcd(y, x % y);
}

template <class T1, class T2> void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}

template <class T1, class T2> void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
template <class T> void Add(T &a, const T &b, const T &mod = 1000000007) {
  int val = ((a % mod) + (b % mod)) % mod;
  if (val < 0) {
    val += mod;
  }
  a = val;
}
////////////////////////////////////////

int main() {
  string S;
  int K;
  cin >> S;
  cin >> K;
  set<string> V;
  for (int i = 0; i < S.size(); ++i) {
    for (int j = 1; i + j <= S.size(); ++j) {
      V.insert(S.substr(i, j));
    }
  }
  auto itr = V.begin();
  for (int i = 0; i < K - 1; ++i) {
    ++itr;
  }
  cout << *itr << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define REP(i, n) FOR(i, 0, n - 1)
#define NREP(i, n) FOR(i, 1, n)
using ll = long long;
using pii = pair<int, int>;
using piii = pair<pii, pii>;
const int dx[4] = {0, -1, 1, 0};
const int dy[4] = {-1, 0, 0, 1};
const int INF = 1e9 + 7;
int gcd(int x, int y) {
  if (x < y)
    swap(x, y);
  if (y == 0)
    return x;
  return gcd(y, x % y);
}

template <class T1, class T2> void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}

template <class T1, class T2> void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
template <class T> void Add(T &a, const T &b, const T &mod = 1000000007) {
  int val = ((a % mod) + (b % mod)) % mod;
  if (val < 0) {
    val += mod;
  }
  a = val;
}
////////////////////////////////////////

int main() {
  string S;
  int K;
  cin >> S;
  cin >> K;
  set<string> V;
  for (int i = 0; i < S.size(); ++i) {
    for (int j = 1; i + j <= S.size(); ++j) {
      if (j <= 5) {
        V.insert(S.substr(i, j));
      }
    }
  }
  auto itr = V.begin();
  for (int i = 0; i < K - 1; ++i) {
    ++itr;
  }
  cout << *itr << endl;
  return 0;
}
Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define mp make_pair
#define pb push_back
#define st first
#define nd second

typedef long long ll;
typedef vector<ll> vl;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;

int main() {
  int n, k;
  char ch[5010];
  vector<string> vs;
  vs.clear();
  scanf("%s", ch);
  string smain = ch;
  scanf("%d", &k);
  map<string, bool> dict;
  dict.clear();
  string smax = "";
  string smin[5];
  for (int i = 0; i < 5; ++i) {
    smin[i] = "";
    for (int j = 0; j < 5010; ++j)
      smin[i] += "z";
  }
  int len = strlen(ch);
  for (int i = 1; i <= len; ++i) {
    for (int j = 0; j <= len - i; ++j) {
      string stemp = smain.substr(j, i);
      if (!dict[stemp]) {
        vs.pb(stemp);
        dict[stemp] = true;
        if (stemp < smin[0])
          smin[0] = stemp;
        if (stemp > smax)
          smax = stemp;
      }
    }
  }
  int sz = vs.size();
  // cout << smin[0] << endl << smax << endl;
  // sort(vs.begin(), vs.end());
  // for(int j = 0; j < sz; ++j)
  // cout << i << " " << vs[i] << endl;
  for (int i = 1; i < 5; ++i) {
    // cout << i << " " << vs[i] << endl;
    for (int j = 0; j < sz; ++j) {
      if (smin[i] > vs[j] && vs[j] > smin[i - 1]) {
        smin[i] = vs[j];
      }
    }
  }
  // for(int i = 0; i < 5; ++i) cout << smin[i] << endl;
  printf("%s\n", smin[k - 1].c_str());
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define mp make_pair
#define pb push_back
#define st first
#define nd second

typedef long long ll;
typedef vector<ll> vl;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;

int main() {
  int n, k;
  char ch[5010];
  vector<string> vs;
  vs.clear();
  scanf("%s", ch);
  string smain = ch;
  scanf("%d", &k);
  map<string, bool> dict;
  dict.clear();
  string smax = "";
  string smin[5];
  for (int i = 0; i < 5; ++i) {
    smin[i] = "";
    for (int j = 0; j < 5010; ++j)
      smin[i] += "z";
  }
  int len = strlen(ch);
  for (int i = 1; i <= 6; ++i) {
    for (int j = 0; j <= len - i; ++j) {
      string stemp = smain.substr(j, i);
      if (!dict[stemp]) {
        vs.pb(stemp);
        dict[stemp] = true;
        if (stemp < smin[0])
          smin[0] = stemp;
        if (stemp > smax)
          smax = stemp;
      }
    }
  }
  int sz = vs.size();
  // cout << smin[0] << endl << smax << endl;
  // sort(vs.begin(), vs.end());
  // for(int j = 0; j < sz; ++j)
  // cout << i << " " << vs[i] << endl;
  for (int i = 1; i < 5; ++i) {
    // cout << i << " " << vs[i] << endl;
    for (int j = 0; j < sz; ++j) {
      if (smin[i] > vs[j] && vs[j] > smin[i - 1]) {
        smin[i] = vs[j];
      }
    }
  }
  // for(int i = 0; i < 5; ++i) cout << smin[i] << endl;
  printf("%s\n", smin[k - 1].c_str());
  return 0;
}
Problem p03355 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

int main(int argc, char *argv[]) {
  std::string s;
  int K;
  std::vector<std::string> vs;

  std::cin >> s >> K;

  int length = 1;
  for (int length = 1; length <= s.size(); length++) {
    for (int spos = 0; spos < s.size(); spos++) {
      std::string tmp = s.substr(spos, length);
      vs.push_back(tmp);
    }
  }

  std::sort(vs.begin(), vs.end());
  std::unique(vs.begin(), vs.end());
  std::cout << vs[K - 1] << std::endl;

  return 0;
}


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

int main(int argc, char *argv[]) {
  std::string s;
  int K;
  std::vector<std::string> vs;

  std::cin >> s >> K;

  int length = 1;
  for (int length = 1; length <= K; length++) {
    for (int spos = 0; spos < s.size(); spos++) {
      std::string tmp = s.substr(spos, length);
      vs.push_back(tmp);
    }
  }

  std::sort(vs.begin(), vs.end());
  std::unique(vs.begin(), vs.end());
  std::cout << vs[K - 1] << std::endl;

  return 0;
}
Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> using vt = vector<T>;
template <class T> using vvt = vector<vt<T>>;
template <class T> using ttt = tuple<T, T>;
using tii = tuple<int, int>;
using vi = vector<int>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
#define mt make_tuple
#define ALL(a) (a).begin(), (a).end()
#define FST first
#define SEC second
#define DEB cerr << "!" << endl
#define SHOW(a, b) cerr << (a) << " " << (b) << endl
#define DIV int(1e9 + 7)
const int INF = (INT_MAX / 2);
const ll LLINF = (LLONG_MAX / 2);
const double eps = 1e-8;
// const double PI = M_PI;
inline ll pow(ll x, ll n, ll m) {
  ll r = 1;
  while (n > 0) {
    if ((n & 1) == 1)
      r = r * x % m;
    x = x * x % m;
    n >>= 1;
  }
  return r % m;
}
inline ll lcm(ll d1, ll d2) { return d1 / __gcd(d1, d2) * d2; }

/*
願はくは花の下にて春死なんそのきさらぎの望月のころ（西行 1118-1190）
--This is old Japanese poem expressing the desire to die under tree on the full
moon night.(written by Saigyo) In my research, there is possibility that the
tree may be seg-tree.
*/

/*Coding Space*/
set<string> cnt;
int main() {
  string s;
  cin >> s;
  int k;
  cin >> k;
  rep(i, s.size()) {
    for (int j = 1; j + i <= (int)s.size() || j <= 5; j++)
      cnt.insert(s.substr(i, j));
  }
  auto itr = cnt.begin();
  rep(i, k - 1) itr++;
  cout << *itr << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> using vt = vector<T>;
template <class T> using vvt = vector<vt<T>>;
template <class T> using ttt = tuple<T, T>;
using tii = tuple<int, int>;
using vi = vector<int>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
#define mt make_tuple
#define ALL(a) (a).begin(), (a).end()
#define FST first
#define SEC second
#define DEB cerr << "!" << endl
#define SHOW(a, b) cerr << (a) << " " << (b) << endl
#define DIV int(1e9 + 7)
const int INF = (INT_MAX / 2);
const ll LLINF = (LLONG_MAX / 2);
const double eps = 1e-8;
// const double PI = M_PI;
inline ll pow(ll x, ll n, ll m) {
  ll r = 1;
  while (n > 0) {
    if ((n & 1) == 1)
      r = r * x % m;
    x = x * x % m;
    n >>= 1;
  }
  return r % m;
}
inline ll lcm(ll d1, ll d2) { return d1 / __gcd(d1, d2) * d2; }

/*
願はくは花の下にて春死なんそのきさらぎの望月のころ（西行 1118-1190）
--This is old Japanese poem expressing the desire to die under tree on the full
moon night.(written by Saigyo) In my research, there is possibility that the
tree may be seg-tree.
*/

/*Coding Space*/
set<string> cnt;
int main() {
  string s;
  cin >> s;
  int k;
  cin >> k;
  rep(i, s.size()) {
    for (int j = 1; j + i <= (int)s.size() && j <= 5; j++)
      cnt.insert(s.substr(i, j));
  }
  auto itr = cnt.begin();
  rep(i, k - 1) itr++;
  cout << *itr << endl;
}
Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <set>
#include <string>

using namespace std;

string get_ans(string x, int K) {
  set<string> all_str;
  int curr_start = 0;
  while (curr_start < x.length()) {
    int curr_end = curr_start;
    while (curr_end < x.length()) {
      all_str.insert(x.substr(curr_start, curr_end - curr_start + 1));
      curr_end++;
    }
    curr_start++;
  }
  return *next(all_str.begin(), K - 1);
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  string s;
  cin >> s;
  int K;
  cin >> K;
  cout << get_ans(s, K);
}

Fixed submission:
#include <iostream>
#include <set>
#include <string>

using namespace std;

string get_ans(string x, int K) {
  set<string> all_str;
  int curr_start = 0;
  while (curr_start < x.length()) {
    int curr_end = curr_start;
    // since each given length has at least 1 unique substr, max len = K. Thus
    // we only check to K.
    while (curr_end < x.length() && curr_end < curr_start + K) {
      all_str.insert(x.substr(curr_start, curr_end - curr_start + 1));
      curr_end++;
    }
    curr_start++;
  }
  return *next(all_str.begin(), K - 1);
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  string s;
  cin >> s;
  int K;
  cin >> K;
  cout << get_ans(s, K);
}
Problem p03355 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  string s;
  vector<string> v;
  int k;

  cin >> s >> k;

  for (int i = 1; i <= k; i++) {
    for (int j = 0; j <= s.length() - i; j++) {
      v.push_back(s.substr(j, i));
    }
  }

  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());

  cout << v[k - 1];

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  string s;
  vector<string> v;
  int k;

  cin >> s >> k;

  for (int i = 1; i <= k && i <= s.length(); i++) {
    for (int j = 0; j <= s.length() - i; j++) {
      v.push_back(s.substr(j, i));
    }
  }

  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());

  cout << v[k - 1];

  return 0;
}
Problem p03355 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define fi first
#define se second

vector<string> v;

int main() {
  int k;
  string s;
  cin >> s >> k;
  for (int i = 0; i < s.size(); i++) {
    for (int j = 1; j <= 5 || j + i - 1 < s.size(); j++) {
      v.push_back(s.substr(i, j));
    }
  } // 全文字列を生成
  sort(v.begin(), v.end());
  unique(v.begin(), v.end());

  cout << v.at(k - 1) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define fi first
#define se second

vector<string> v;

int main() {
  int k;
  string s;
  cin >> s >> k;
  for (int i = 0; i < s.size(); i++) {
    for (int j = 1; j <= 5 && j + i - 1 < s.size(); j++) {
      v.push_back(s.substr(i, j));
    }
  } // 全文字列を生成
  sort(v.begin(), v.end());
  unique(v.begin(), v.end());

  cout << v.at(k - 1) << endl;
}
Problem p03355 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define N 10000

int n, m, xb, t, c[N], ans[N], pw[N];
char s[N];
map<int, bool> vis;

int main() {
  scanf("%s%d", s + 1, &m);
  n = strlen(s + 1);
  pw[0] = 1;
  for (int i = 1; i <= m; ++i)
    pw[i] = pw[i - 1] * 30;
  for (int i = 1; i <= n; ++i) {
    int x = 0;
    for (int j = i; j < i + m && j <= n; ++j) {
      x = x * 30 + s[j] - 'a' + 1;
      int y = x * pw[m - (j - i)];
      if (!vis.count(y)) {
        vis[y] = 1;
        c[++xb] = y;
      }
    }
  }
  sort(c + 1, c + xb + 1);
  for (int x = c[m]; x; x /= 30)
    if (x % 30)
      ans[++t] = x % 30 + 'a' - 1;
  for (; t; --t)
    putchar(ans[t]);

  return 0;
}


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

using namespace std;

#define N 100000

int n, m, xb, t, c[N], ans[N], pw[N];
char s[N];
map<int, bool> vis;

int main() {
  scanf("%s%d", s + 1, &m);
  n = strlen(s + 1);
  pw[0] = 1;
  for (int i = 1; i <= m; ++i)
    pw[i] = pw[i - 1] * 30;
  for (int i = 1; i <= n; ++i) {
    int x = 0;
    for (int j = i; j < i + m && j <= n; ++j) {
      x = x * 30 + s[j] - 'a' + 1;
      int y = x * pw[m - (j - i)];
      if (!vis.count(y)) {
        vis[y] = 1;
        c[++xb] = y;
      }
    }
  }
  sort(c + 1, c + xb + 1);
  for (int x = c[m]; x; x /= 30)
    if (x % 30)
      ans[++t] = x % 30 + 'a' - 1;
  for (; t; --t)
    putchar(ans[t]);

  return 0;
}
Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;
set<string> sp;
int main() {
  string s;
  int k;
  cin >> s >> k;
  for (int i = 0; i < s.size(); ++i) {
    for (int j = i; j < s.size(); ++j) {
      sp.insert(s.substr(i, j - i + 1));
    }
  }
  set<string>::iterator iter = sp.begin();
  for (int i = 0; i < k; ++i, ++iter)
    ;
  iter--;
  cout << *iter << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;
set<string> sp;
int main() {
  string s;
  int k;
  cin >> s >> k;
  for (int i = 0; i < s.size(); ++i) {
    for (int j = 0; j < k; ++j) {
      if (i + j + 1 > s.size()) {
        break;
      }
      sp.insert(s.substr(i, j + 1));
    }
  }
  set<string>::iterator iter = sp.begin();
  for (int i = 0; i < k; ++i, ++iter)
    ;
  iter--;
  cout << *iter << endl;
  return 0;
}