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

Buggy submission (status: Time Limit Exceeded):
/*------------------------------------
........Bismillahir Rahmanir Rahim....
..........created by Abdul Aziz.......
------------------------------------*/
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <unordered_map>
#include <vector>
#define mod 998244353
#define int long long
#define ld long double
#define pb push_back
#define vi vector<int>
#define dbg(x) cerr << #x << " = " << x << '\n'
#define sz(x) (int)x.size()
#define all(a) (a.begin(), a.end())
#define ff first
#define ss second
#define pii pair<int, int>
#define lcm(a, b) (a * b) / __gcd(a, b)
using namespace std;

inline void solve() {
  map<string, bool> cnt;
  string c = "";
  c += (char)('z' + 1);
  string res[6] = {c, c, c, c, c, c};
  string s;
  int k;
  cin >> s >> k;
  for (int i = 0; i < sz(s); i++) {
    string h = "";
    for (int j = i; j < sz(s); j++) {
      // sort(res.begin(),res.end());
      h += s[j];
      if (!cnt[h]) {
        for (int k = 0; k < 6; k++) {
          if (h < res[k]) {
            for (int ii = 5; ii > k; ii--)
              swap(res[ii], res[ii - 1]);
            res[k] = h;
            break;
          }
        }
      }
      cnt[h] = true;
    }
  }
  cout << res[k - 1] << endl;
}

signed main() {
  int n = 1; // cin>>n;
  while (n--)
    solve();
  return 0;
}


Fixed submission:
/*------------------------------------
........Bismillahir Rahmanir Rahim....
..........created by Abdul Aziz.......
------------------------------------*/
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <unordered_map>
#include <vector>
#define mod 998244353
#define int long long
#define ld long double
#define pb push_back
#define vi vector<int>
#define dbg(x) cerr << #x << " = " << x << '\n'
#define sz(x) (int)x.size()
#define all(a) (a.begin(), a.end())
#define ff first
#define ss second
#define pii pair<int, int>
#define lcm(a, b) (a * b) / __gcd(a, b)
using namespace std;

inline void solve() {
  map<string, bool> cnt;
  string c = "";
  c += (char)('z' + 1);
  string res[6] = {c, c, c, c, c, c};
  string s;
  int k;
  cin >> s >> k;
  for (int i = 0; i < sz(s); i++) {
    string h = "";
    for (int j = i; j < sz(s); j++) {
      // sort(res.begin(),res.end());
      h += s[j];
      if (h > res[5])
        break;
      if (!cnt[h]) {
        for (int k = 0; k < 6; k++) {
          if (h < res[k]) {
            for (int ii = 5; ii > k; ii--)
              swap(res[ii], res[ii - 1]);
            res[k] = h;
            break;
          }
        }
      }
      cnt[h] = true;
    }
  }
  cout << res[k - 1] << endl;
}

signed main() {
  int n = 1; // cin>>n;
  while (n--)
    solve();
  return 0;
}
Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using ll = long long;

#define rep(i, n) for (ll i = 0; i < (int)(n); i++)
#define reps(i, x) for (ll i = 1; i <= (int)(x); i++)
#define rrep(i, x) for (ll i = ((int)(x)-1); i >= 0; i--)
#define rreps(i, x) for (ll i = (int)(x); i > 0; i--)
#define all(x) (x).begin(), (x).end()
#define m0(x) memset(x, 0, sizeof(x))
#define vll vector<ll>
#define vi vector<int>
#define mp make_pair
#define vpll vector<pair<ll, ll>>
#define vpi vector<pair<int, int>>
#define mod 1000000007
using namespace std;
ll n;
int main() {
  int k;
  string s;
  cin >> s >> k;
  string cur = "zzz";
  int mx = (5, s.size());
  rep(i, s.size() - mx + 1) {
    string tm = s.substr(i, mx);
    if (tm < cur) {
      cur = tm;
    }
  }
  set<string> st;
  for (int i = 0; i < mx; i++) {
    for (int j = i + 1; j <= mx; j++) {
      st.insert(cur.substr(i, j - i));
    }
  }
  st.insert(cur);
  string ans;
  int i = 0;
  for (auto e : st) {
    i++;
    if (i == k) {
      cout << e << endl;
      break;
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using ll = long long;

#define rep(i, n) for (ll i = 0; i < (int)(n); i++)
#define reps(i, x) for (ll i = 1; i <= (int)(x); i++)
#define rrep(i, x) for (ll i = ((int)(x)-1); i >= 0; i--)
#define rreps(i, x) for (ll i = (int)(x); i > 0; i--)
#define all(x) (x).begin(), (x).end()
#define m0(x) memset(x, 0, sizeof(x))
#define vll vector<ll>
#define vi vector<int>
#define mp make_pair
#define vpll vector<pair<ll, ll>>
#define vpi vector<pair<int, int>>
#define mod 1000000007
using namespace std;
ll n;
int main() {
  int k;
  string s;
  cin >> s >> k;
  string cur = "zzz";
  int mx = min(5, (int)s.size());
  rep(i, s.size() - mx + 1) {
    string tm = s.substr(i, mx);
    if (tm < cur) {
      cur = tm;
    }
  }
  set<string> st;
  for (int i = 0; i < mx; i++) {
    for (int j = i + 1; j <= mx; j++) {
      st.insert(cur.substr(i, j - i));
    }
  }
  st.insert(cur);
  string ans;
  int i = 0;
  for (auto e : st) {
    i++;
    if (i == k) {
      cout << e << endl;
      break;
    }
  }
  return 0;
}
Problem p03355 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <math.h>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using lint = long long;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};

const lint INF = 1ll << 60;
const int mod = 1000000007;
const int inf = INT_MAX;
void speed() {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
  return;
}

int main(void) {
  speed();
  string s;
  cin >> s;
  int k;
  cin >> k;
  vector<string> d;
  for (int i = 0; i < (int)s.length(); i++) {
    for (int j = 1; j <= k; j++) {
      if (i + j >= (int)s.length())
        continue;
      d.emplace_back(s.substr(i, j));
    }
  }
  sort(d.begin(), d.end());
  d.erase(unique(d.begin(), d.end()), d.end());
  cout << d[k - 1] << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <math.h>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using lint = long long;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};

const lint INF = 1ll << 60;
const int mod = 1000000007;
const int inf = INT_MAX;
void speed() {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
  return;
}

int main(void) {
  speed();
  string s;
  cin >> s;
  int k;
  cin >> k;
  vector<string> d;
  for (int i = 0; i < (int)s.length(); i++) {
    for (int j = 1; j <= k; j++) {
      if (i + j - 1 >= (int)s.length())
        continue;
      d.emplace_back(s.substr(i, j));
    }
  }
  sort(d.begin(), d.end());
  d.erase(unique(d.begin(), d.end()), d.end());
  cout << d[k - 1] << endl;

  return 0;
}
Problem p03355 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define all(vec) vec.begin(), vec.end()
typedef long long int ll;
typedef pair<int, int> P;
const ll MOD = 1000000007;
const ll INF = 1000000010;
const ll LINF = 4000000000000000010LL;
const int MAX = 310;
const double EPS = 1e-9;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int main() {
  string s;
  cin >> s;
  int k;
  cin >> k;
  vector<string> v;
  int n = s.length();
  for (int i = 0; i < n; i++) {
    for (int j = i; j < n; j++) {
      v.push_back(s.substr(i, j - i + 1));
    }
  }
  sort(all(v));
  v.erase(unique(all(v)), v.end());
  cout << v[k - 1] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define all(vec) vec.begin(), vec.end()
typedef long long int ll;
typedef pair<int, int> P;
const ll MOD = 1000000007;
const ll INF = 1000000010;
const ll LINF = 4000000000000000010LL;
const int MAX = 310;
const double EPS = 1e-9;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int main() {
  string s;
  cin >> s;
  int k;
  cin >> k;
  vector<string> v;
  int n = s.length();
  for (int i = 0; i < n; i++) {
    for (int j = i; j < min(i + k, n); j++) {
      v.push_back(s.substr(i, j - i + 1));
    }
  }
  sort(all(v));
  v.erase(unique(all(v)), v.end());
  cout << v[k - 1] << endl;
  return 0;
}
Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define ll long long
#define imfor(i, n) for (int i = 0; i < n; i++)
using namespace std;
ll MOD = 1000000007;

int main() {
  int k;
  string s;
  cin >> s;
  cin >> k;
  set<string> st;
  for (int i = 1; i <= s.size(); i++) {
    for (int j = 0; j <= s.size() - i; j++) {
      st.insert(s.substr(j, i));
    }
  }
  int cnt = 0;
  for (auto x : st) {
    cnt++;
    if (cnt == k) {
      cout << x << endl;
      break;
    }
  }
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define ll long long
#define imfor(i, n) for (int i = 0; i < n; i++)
using namespace std;
ll MOD = 1000000007;

int main() {
  int k;
  string s;
  cin >> s;
  cin >> k;
  set<string> st;
  int l = min((int)s.size(), 10);
  for (int i = 1; i <= l; i++) {
    for (int j = 0; j <= s.size() - i; j++) {
      st.insert(s.substr(j, i));
    }
  }
  int cnt = 0;
  for (auto x : st) {
    cnt++;
    if (cnt == k) {
      cout << x << endl;
      break;
    }
  }
}
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> v;
  for (int i = 0; s.size() > i; i++) {
    for (int j = 1; i + j - 1 < s.size(); ++j) {
      v.push_back(s.substr(i, j));
    }
  }
  set<string> st;
  while (k--) {
    string mn = " ";
    mn[0] = char('z' + 1);
    for (string i : v) {
      if (st.find(i) != st.end())
        ;
      else
        mn = min(mn, i);
    }
    if (!k)
      cout << mn << endl;
    st.insert(mn);
  }
}


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

int main() {
  string s;
  cin >> s;
  int k;
  cin >> k;
  vector<string> v;
  for (int i = 0; s.size() > i; i++) {
    for (int j = 1; i + j - 1 < s.size(); ++j) {
      if (j > k + 2)
        break;
      v.push_back(s.substr(i, j));
    }
  }
  set<string> st;
  while (k--) {
    string mn = " ";
    mn[0] = char('z' + 1);
    for (string i : v) {
      if (st.find(i) != st.end())
        ;
      else
        mn = min(mn, i);
    }
    if (!k)
      cout << mn << endl;
    st.insert(mn);
  }
}
Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <functional>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;

int main(void) {
  string s;
  int K;
  cin >> s;
  cin >> K;

  auto sss = vector<string>(K, "~");
  for (int i = 0; i < s.size(); i++) {
    for (int k = 1; k <= s.size() - i; k++) {
      auto ss = s.substr(i, k);
      for (int k = 0; k < K; k++) {
        if (ss < sss[k]) {
          for (int t = K - 1; t > k; t--) {
            sss[t] = sss[t - 1];
          }
          sss[k] = ss;
          break;
        } else if (ss == sss[k]) {
          break;
        }
      }
    }
  }

  cout << sss[K - 1] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;

int main(void) {
  string s;
  int K;
  cin >> s;
  cin >> K;

  auto sss = vector<string>(K, "~");
  for (int i = 0; i < s.size(); i++) {
    for (int j = 1; j <= s.size() - i; j++) {
      if (j > 5)
        continue;
      auto ss = s.substr(i, j);
      for (int k = 0; k < K; k++) {
        if (ss < sss[k]) {
          for (int t = K - 1; t > k; t--) {
            sss[t] = sss[t - 1];
          }
          sss[k] = ss;
          break;
        } else if (ss == sss[k]) {
          break;
        }
      }
    }
  }

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

Buggy submission (status: Time Limit Exceeded):
// 20:05
#include <bits/stdc++.h>
using namespace std;
const int N = 5e3 + 10;
char A[N];
map<string, int> Q;
int main() {
  // freopen("/home/zz7/CFInput","r",stdin);
  scanf("%s", A);
  int len = strlen(A);
  int k;
  scanf("%d", &k);
  string a = A;
  for (int i = 0; i < len; i++) {
    for (int j = 1; j <= len - i; j++) {
      Q[a.substr(i, j)]++;
    }
  }
  int cnt = 0;
  for (auto j : Q) {
    if (++cnt == k)
      return cout << j.first << '\n', 0;
  }
}


Fixed submission:
// 20:05
#include <bits/stdc++.h>
using namespace std;
const int N = 5e3 + 10;
char A[N];
map<string, int> Q;
int main() {
  // freopen("/home/zz7/CFInput","r",stdin);
  scanf("%s", A);
  int len = strlen(A);
  int k;
  scanf("%d", &k);
  string a = A;
  for (int i = 0; i < len; i++) {
    for (int j = 1; j <= min(len - i, 5); j++) {
      Q[a.substr(i, j)]++;
    }
  }
  int cnt = 0;
  for (auto j : Q) {
    if (++cnt == k)
      return cout << j.first << '\n', 0;
  }
}
Problem p03355 (C++)

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

string solve() {
  string S;
  int K;
  cin >> S >> K;
  set<string> st;
  for (int i = 0; i < S.size(); i++) {
    for (int j = 1; i + j <= S.size(); j++) {
      st.insert(S.substr(i, j));
      if (st.size() > K)
        st.erase(prev(st.end()));
    }
  }
  return *prev(st.end());
}

int main() {
  auto ans = solve();
  cout << ans << "\n";
  return 0;
}

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

string solve() {
  string S;
  int K;
  cin >> S >> K;
  set<string> st;
  for (int i = 0; i < S.size(); i++) {
    for (int j = 1; i + j <= S.size(); j++) {
      if (st.size() == K) {
        if (*prev(st.end()) <= S.substr(i, j))
          break;
      }
      st.insert(S.substr(i, j));
      if (st.size() > K)
        st.erase(prev(st.end()));
    }
  }
  return *prev(st.end());
}

int main() {
  auto ans = solve();
  cout << ans << "\n";
  return 0;
}
Problem p03355 (C++)

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

#define INF 1e9
#define llINF 1e18
#define MOD 1e9 + 7
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long
using namespace std;
int main() {
  string s;
  int n;
  cin >> s >> n;
  vector<string> str;
  for (int i = 0; i < min((int)s.size(), n); i++) {
    for (int k = 0; k < s.size() - i; k++) {
      for (int j = k; j < s.size() - i; j++) {
        str.pb(s.substr(j, i + 1));
      }
    }
  }
  sort(str.begin(), str.end());
  int cnt = 0, cnt2 = 0;
  string ans = "";
  while (cnt != n) {
    if (ans != str[cnt2]) {
      cnt++;
      ans = str[cnt2];
    }
    cnt2++;
  }
  cout << ans << endl;
  return 0;
}


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

#define INF 1e9
#define llINF 1e18
#define MOD 1e9 + 7
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long
using namespace std;
int main() {
  string s;
  int n;
  cin >> s >> n;
  vector<string> str;
  for (int i = 0; i < min((int)s.size(), n); i++) {
    for (int k = 0; k < s.size() - i; k++) {
      str.pb(s.substr(k, i + 1));
    }
  }
  sort(str.begin(), str.end());
  int cnt = 0, cnt2 = 0;
  string ans = "";
  while (cnt != n) {
    if (ans != str[cnt2]) {
      cnt++;
      ans = str[cnt2];
    }
    cnt2++;
  }
  cout << ans << endl;
  return 0;
}
Problem p03355 (C++)

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

#ifdef LOCAL
#define debug(var) cerr << (#var) << " = " << (var) << endl;
#else
#define debug(var)
#endif

void init() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}

void solve() {
  string s;
  int k;
  cin >> s >> k;
  set<string> ss;
  vector<string> vs;
  for (int i = 0; i < (int)s.size(); ++i) {
    vs.emplace_back();
    for (string &e : vs) {
      e += s[i];
      ss.insert(e);
    }
  }
  for (const string &s : ss) {
    if (--k)
      continue;
    cout << s;
    break;
  }
}

int main() {
  init();
  int t = 1; // scanf("%d", &t);
  while (t--) {
    solve();
  }
  return 0;
}


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

#ifdef LOCAL
#define debug(var) cerr << (#var) << " = " << (var) << endl;
#else
#define debug(var)
#endif

void init() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}

void solve() {
  string s;
  int k;
  cin >> s >> k;
  set<string> ss;
  vector<string> vs;
  for (int i = 0; i < (int)s.size(); ++i) {
    vs.emplace_back();
    for (string &e : vs) {
      if (e.size() > k)
        continue;
      e += s[i];
      ss.insert(e);
    }
  }
  for (const string &s : ss) {
    if (--k)
      continue;
    cout << s;
    break;
  }
}

int main() {
  init();
  int t = 1; // scanf("%d", &t);
  while (t--) {
    solve();
  }
  return 0;
}
Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF 1e9
#define MOD 1000000007
#define MAX_N 100001
#define MAX_S 5001
#define mod(x) ((x % MOD) + MOD) % MOD

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
vector<string> s_list;

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

  int len = (int)s.size();

  string top_5[] = {"", "", "", "", ""};

  for (int i = 0; i < len; ++i) {
    for (int j = i + 1; j <= len; ++j) {
      string sub_s = s.substr(i, j - i);
      int insert_pos = -1;
      for (int k = 0; k < 5; ++k) {
        if (top_5[k] == sub_s)
          break;
        if (top_5[k] == "" || top_5[k] > sub_s) {
          insert_pos = k;
          break;
        }
      }
      if (insert_pos != -1) {
        for (int k = 4; k > insert_pos; --k) {
          top_5[k] = top_5[k - 1];
        }
        top_5[insert_pos] = sub_s;
      }
    }
  }

  cout << top_5[K - 1] << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF 1e9
#define MOD 1000000007
#define MAX_N 100001
#define MAX_S 5001
#define mod(x) ((x % MOD) + MOD) % MOD

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
vector<string> s_list;

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

  int len = (int)s.size();

  string top_5[] = {"", "", "", "", ""};

  for (int i = 0; i < len; ++i) {
    for (int j = i + 1; j <= min(i + 5, len); ++j) {
      string sub_s = s.substr(i, j - i);
      int insert_pos = -1;
      for (int k = 0; k < 5; ++k) {
        if (top_5[k] == sub_s)
          break;
        if (top_5[k] == "" || top_5[k] > sub_s) {
          insert_pos = k;
          break;
        }
      }
      if (insert_pos != -1) {
        for (int k = 4; k > insert_pos; --k) {
          top_5[k] = top_5[k - 1];
        }
        top_5[insert_pos] = sub_s;
      }
    }
  }

  cout << top_5[K - 1] << endl;

  return 0;
}
Problem p03355 (Python)

Buggy submission (status: Time Limit Exceeded):
S = input()
N = len(S)
K = int(input())
ans = set()
for c in "abcdefghijklmnopqrstuvwxyz":
    for i in range(N):
        if S[i] == c:
            for j in range(i + 1, N + 1):
                ans.add(S[i:j])
    if len(ans) >= K:
        break
print(sorted(list(ans))[K - 1])


Fixed submission:
S = input()
N = len(S)
K = int(input())
ans = set()
for c in "abcdefghijklmnopqrstuvwxyz":
    for i in range(N):
        if S[i] == c:
            for j in range(i + 1, i + K + 1):
                ans.add(S[i:j])
    if len(ans) >= K:
        break
print(sorted(list(ans))[K - 1])
Problem p03355 (Python)

Buggy submission (status: Time Limit Exceeded):
import heapq

S = input()
K = int(input())

q = []

for i in range(len(S)):
    for j in range(i, len(S) + 1):
        heapq.heappush(q, S[i:j])

last = ""
while K > 0:
    s = heapq.heappop(q)
    if s != last:
        K -= 1
        last = s

print(last)


Fixed submission:
import heapq

S = input()
K = int(input())

q = []

for i in range(len(S)):
    for j in range(i, min(len(S), i + K) + 1):
        heapq.heappush(q, S[i:j])

last = ""
while K > 0:
    s = heapq.heappop(q)
    if s != last:
        K -= 1
        last = s

print(last)
Problem p03355 (Python)

Buggy submission (status: Time Limit Exceeded):
S = input()
K = int(input())
n = len(S)

ans = []
st = set()

for i in range(n):
    for j in range(i + 1, n + 1):
        sub = S[i:j]
        # print(sub)
        if sub in st:
            continue
        st.add(sub)
        ans.append(sub)
        ans.sort()
        # print(ans)
        if len(ans) > K:
            ans = ans[:K]

print(ans[-1])


Fixed submission:
S = input()
K = int(input())
n = len(S)

ans = []
st = set()

for i in range(n):
    for j in range(i + 1, min(n + 1, i + 1 + K)):
        sub = S[i:j]
        # print(sub)
        if sub in st:
            continue
        st.add(sub)
        ans.append(sub)
        ans.sort()
        # print(ans)
        if len(ans) > K:
            ans = ans[:K]

print(ans[-1])
Problem p03355 (Python)

Buggy submission (status: Time Limit Exceeded):
import sys
from heapq import heapify, heappop

sys.setrecursionlimit(10**7)
f_inf = float("inf")
mod = 10**9 + 7


def resolve():
    S = input()
    k = int(input())
    n = len(S)

    SS = set()
    for i in range(n):
        ss = [S[i]]
        SS.add("".join(ss))
        for j in range(i + 1, n):
            ss.append(S[j])
            SS.add("".join(ss))

    SS = list(SS)
    heapify(SS)
    for _ in range(k):
        res = heappop(SS)
    print(res)


if __name__ == "__main__":
    resolve()


Fixed submission:
import sys
from heapq import heapify, heappop

sys.setrecursionlimit(10**7)
f_inf = float("inf")
mod = 10**9 + 7


def resolve():
    S = input()
    k = int(input())
    n = len(S)

    SS = set()
    for i in range(n):
        ss = S[i]
        SS.add(ss)
        for j in range(i + 1, min(n, i + k)):
            ss += S[j]
            SS.add(ss)

    SS = list(SS)
    heapify(SS)
    for _ in range(k):
        res = heappop(SS)
    print(res)


if __name__ == "__main__":
    resolve()
Problem p03355 (Python)

Buggy submission (status: Time Limit Exceeded):
# kが極端に少ないので、下位K個を管理してよさそうな気がする
# -> なんかダメ
# じゃ決め打ちして全探索することにしてみる どうせ長さは最大でKなので
# 時間的に間に合うか微妙な気がする
# -> はい

s = list(input())
k = int(input())
d = {}
for i in range(len(s)):
    for j in range(i, len(s)):
        t = "".join(s[i : j + 1])
        if t not in d:
            d[t] = True
dk = sorted(d.keys())
print(dk[k - 1])


Fixed submission:
# kが極端に少ないので、下位K個を管理してよさそうな気がする
# -> なんかダメ
# じゃ決め打ちして全探索することにしてみる どうせ長さは最大でKなので
# 時間的に間に合うか微妙な気がする
# -> はい

s = list(input())
k = int(input())
d = {}
for i in range(len(s)):
    for j in range(i, min(i + k, len(s))):
        t = "".join(s[i : j + 1])
        if t not in d:
            d[t] = True
dk = sorted(d.keys())
print(dk[k - 1])
Problem p03355 (Python)

Buggy submission (status: Time Limit Exceeded):
s = input()
K = int(input())
ans_lis = []
s_length = len(s)
lis = [[] for i in range(26)]

for i in range(s_length):
    lis[ord(s[i]) - ord("a")].append(i)


for i in range(26):
    for j in lis[i]:
        k = j
        while k <= K + 10000:
            ans_lis.append(s[j : k + 1])
            k += 1
    ans_lis = sorted(list(set(ans_lis)))
    if len(ans_lis) >= K:
        print(ans_lis[K - 1])
        exit()


Fixed submission:
s = input()
K = int(input())
ans_lis = []
s_length = len(s)
lis = [[] for i in range(26)]

for i in range(s_length):
    lis[ord(s[i]) - ord("a")].append(i)


for i in range(26):
    k = 1
    while k <= K:
        for j in lis[i]:
            ans_lis.append(s[j : j + k])
        k += 1
        ans_lis = sorted(list(set(ans_lis)))
    if len(ans_lis) >= K:
        print(ans_lis[K - 1])
        exit()
Problem p03355 (Python)

Buggy submission (status: Runtime Error):
S = input()
k = int(input())
n = len(S)

orders = set()

for i in range(n):
    for j in range(1, 6):
        if i + j < n:
            orders.add(S[i : i + j])

print(sorted(list(orders))[k - 1])


Fixed submission:
S = input()
k = int(input())
n = len(S)

orders = set()

for i in range(n):
    for j in range(1, 6):
        if i + j < n + 1:
            orders.add(S[i : i + j])

print(sorted(list(orders))[k - 1])
Problem p03355 (Python)

Buggy submission (status: Time Limit Exceeded):
import sys
import math  # noqa
import bisect  # noqa
import queue  # noqa


def input():
    return sys.stdin.readline().rstrip()


def main():
    S = input()
    K = int(input())

    N = len(S)
    substring = set()
    for i in range(N):
        for j in range(i + 1, N + 1):
            substring.add(S[i:j])

    substring = list(substring)
    substring.sort()
    print(substring[K - 1])


if __name__ == "__main__":
    main()


Fixed submission:
import sys
import math  # noqa
import bisect  # noqa
import queue  # noqa


def input():
    return sys.stdin.readline().rstrip()


def main():
    S = input()
    K = int(input())

    N = len(S)
    substring = set()
    for i in range(N):
        for j in range(1, 6):
            substring.add(S[i : i + j])

    substring = list(substring)
    substring.sort()
    print(substring[K - 1])


if __name__ == "__main__":
    main()
Problem p03355 (C++)

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

using namespace std;

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

  vector<string> t;
  for (int i = 0; i < s.length(); i++) {
    for (int j = 1; j <= s.length() - i; j++) {
      t.emplace_back(s.substr(i, j));
    }
  }

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

  cout << t[k - 1] << endl;

  return 0;
}

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

using namespace std;

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

  vector<string> t;
  for (int i = 0; i < s.length(); i++) {
    for (int j = 1; j <= min(7, (int)s.length() - i); j++) {
      t.emplace_back(s.substr(i, j));
    }
  }

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

  cout << t[k - 1] << endl;

  return 0;
}
Problem p03355 (C++)

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define INF 1e9

typedef long long unsigned int ll;

string s;
set<string> st;
int k;

void solve() {
  rep(i, s.length()) {
    rep(j, s.length() - i) {
      string sub = s.substr(i, j + 1);
      st.insert(sub);
    }
  }
  int cnt = 0;
  for (auto itr = st.begin(); itr != st.end(); ++itr) {
    if (cnt == k - 1)
      cout << *itr << endl;
    cnt++;
  }
}

int main() {
  cin >> s;
  cin >> k;
  solve();
}

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define INF 1e9

typedef long long unsigned int ll;

string s;
set<string> st;
int k;

void solve() {
  for (int i = 0; i < s.size(); i++) {
    for (int j = 1; j <= 5 && s.size();
         j++) { // k < 5なので文字以上の分割は考えなくていい
      string sub = s.substr(i, j);
      st.insert(sub);
    }
  }
  int cnt = 0;
  for (auto itr = st.begin(); itr != st.end(); ++itr) {
    if (cnt == k - 1)
      cout << *itr << endl;
    cnt++;
  }
}

int main() {
  cin >> s;
  cin >> k;
  solve();
}
Problem p03355 (C++)

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define pb push_back
#define rep(i, a, n) for (int i = (a); i < (n); i++)
#define dep(i, a, n) for (int i = (a); i >= (n); i--)
#define mod (ll)(1e9 + 7)

__attribute__((constructor)) void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

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

  map<char, vector<int>> mp;
  rep(i, 0, s.length()) { mp[s[i]].pb(i); }

  map<string, int> ans;
  for (auto it = mp.begin(); it != mp.end(); it++) {
    rep(i, 0, it->second.size()) {
      string ss = "";
      rep(j, it->second[i], s.length()) {
        ss += s[j];
        ans[ss] = 1;
      }
    }
    if (ans.size() >= k)
      break;
  }
  int id = 0;
  for (auto it = ans.begin(); it != ans.end(); it++) {
    id++;
    if (id >= k) {
      cout << it->first << endl;
      return 0;
    }
  }
}


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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define pb push_back
#define rep(i, a, n) for (int i = (a); i < (n); i++)
#define dep(i, a, n) for (int i = (a); i >= (n); i--)
#define mod (ll)(1e9 + 7)

__attribute__((constructor)) void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

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

  map<char, vector<int>> mp;
  rep(i, 0, s.length()) { mp[s[i]].pb(i); }

  map<string, int> ans;
  for (auto it = mp.begin(); it != mp.end(); it++) {
    rep(i, 0, it->second.size()) {
      string ss = "";
      rep(j, it->second[i], min((int)s.length(), it->second[i] + 5)) {
        ss += s[j];
        ans[ss] = 1;
      }
    }
    if (ans.size() >= k)
      break;
  }
  int id = 0;
  for (auto it = ans.begin(); it != ans.end(); it++) {
    id++;
    if (id >= k) {
      cout << it->first << endl;
      return 0;
    }
  }
}
Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define rep(n) for (int i = 0; i < n; i++)
#define repp(j, n) for (int j = 0; j < n; j++)
#define reppp(i, m, n) for (int i = m; i < n; i++)
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define MOD 1000000007
#define EPS 1e-9

using namespace std;

typedef long long ll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pii;
struct edge {
  int from, to;
  ll cost;
};

int main() {
  std::ios::sync_with_stdio(0);
  cin.tie(0);
  string s;
  int k, kk = 0;
  cin >> s >> k;

  int n = (int)s.length();
  vector<string> v;
  map<string, bool> mp;
  rep(n) {
    string t = "";
    reppp(j, 1, n - i + 1) {
      t += s[i + j - 1];
      if (!mp[t])
        v.push_back(t);
      mp[t] = true;
    }
  }
  sort(all(v));
  cout << v[k - 1] << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define rep(n) for (int i = 0; i < n; i++)
#define repp(j, n) for (int j = 0; j < n; j++)
#define reppp(i, m, n) for (int i = m; i < n; i++)
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define MOD 1000000007
#define EPS 1e-9

using namespace std;

typedef long long ll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pii;
struct edge {
  int from, to;
  ll cost;
};

int main() {
  std::ios::sync_with_stdio(0);
  cin.tie(0);
  string s;
  int k, kk = 0;
  cin >> s >> k;

  int n = (int)s.length();
  vector<string> v;
  map<string, bool> mp;
  rep(n) {
    string t = "";
    reppp(j, 1, min(k, n - i) + 1) {
      t += s[i + j - 1];
      if (!mp[t])
        v.push_back(t);
      mp[t] = true;
    }
  }
  sort(all(v));
  cout << v[k - 1] << endl;

  return 0;
}
Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, a, n) for (ll i = ((ll)a); i < ((ll)n); i++)
using namespace std;
typedef long long ll;

int main(void) {
  string S;
  ll K;
  cin >> S >> K;

  vector<string> sub;
  REP(i, 0, S.size()) REP(j, i, S.size()) {
    sub.push_back(S.substr(i, j + 1 - i));
    sort(sub.begin(), sub.end());
    sub.erase(unique(sub.begin(), sub.end()), sub.end());
    if (sub.size() > 5)
      sub.resize(5);
  }

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


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

int main(void) {
  string S;
  ll K;
  cin >> S >> K;

  vector<string> sub;
  REP(i, 0, S.size()) REP(j, i, min(i + 5, (ll)S.size())) {
    sub.push_back(S.substr(i, j + 1 - i));
    sort(sub.begin(), sub.end());
    sub.erase(unique(sub.begin(), sub.end()), sub.end());
    if (sub.size() > 5)
      sub.resize(5);
  }

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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

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

// clear memory
#define CLR(a) memset((a), 0, sizeof(a))
#define SORT(c) sort((c).begin(), (c).end())
#define vi(m, a) vector<int> m(a)
#define vti(m, a, i) vector<vector<int>> m(a, vector<int>(i))
#define ALL(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define Fe(it, a) for (auto &it : a)
#define all(a) begin(a), end(a)

using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;
#define WARU 1000000007;

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

ll kyori(int a, int b, int c) {
  int k = max(a, b);
  int l = min(a, b);
  return (min(abs(k - l), abs(k - l - c)));
}

int main() {
  set<string> ss;
  string str;
  cin >> str;
  int count;
  cin >> count;

  rep(i, str.length()) {
    rep(k, str.length()) { ss.insert(str.substr(i, k + 1)); }
  }

  int current = 0;
  ALL(it, ss) {
    current++;
    if (count == current) {
      cout << *it << endl;
      break;
    }
  }
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

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

// clear memory
#define CLR(a) memset((a), 0, sizeof(a))
#define SORT(c) sort((c).begin(), (c).end())
#define vi(m, a) vector<int> m(a)
#define vti(m, a, i) vector<vector<int>> m(a, vector<int>(i))
#define ALL(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define Fe(it, a) for (auto &it : a)
#define all(a) begin(a), end(a)

using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;
#define WARU 1000000007;

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

ll kyori(int a, int b, int c) {
  int k = max(a, b);
  int l = min(a, b);
  return (min(abs(k - l), abs(k - l - c)));
}

int main() {
  set<string> ss;
  string str;
  cin >> str;
  int count;
  cin >> count;

  rep(i, str.length()) {
    rep(k, str.length()) {
      if (k > count)
        break;
      ss.insert(str.substr(i, k + 1));
    }
  }

  int current = 0;
  ALL(it, ss) {
    current++;
    if (count == current) {
      cout << *it << endl;
      break;
    }
  }
}
Problem p03355 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
// #include <cstdio>
// #include <functional>
#include <iostream>
// #include <cfloat>
// #include <climits>
// #include <cstdlib>
// #include <cstring>
// #include <cmath>
// #include <map>
// #include <queue>
// #include <set>
// #include <sstream>
// #include <stack>
#include <string>
// #include <time.h>
#include <vector>
// #include <random>
// #include <unordered_set>
// #include <complex>
#include <string.h>
#define rt0 return 0
#define rep(i, n) for (int i = 0; i < n; i++)
#define LL long long
#define pii pair<int, int>
using namespace std;
int main() {
  string s;
  int k, m = 0;
  cin >> s >> k;
  vector<string> a;
  rep(i, s.size()) {
    string tmp = "";
    for (int j = i; j < s.size() and j < j + k; j++)
      tmp.push_back(s[j]), a.push_back(tmp), m++;
  }
  sort(a.begin(), a.end());
  // cout<<a.size()<<endl;
  rep(i, m - 1) if (a[i] == a[i + 1]) a.erase(a.begin() + i), i--;
  // if(a[0]==a[1]) a.erase(a.begin());
  // cout<<a[0]<<endl<<a[1]<<endl<<a[2]<<endl<<a[3]<<endl<<a[4]<<endl<<a[5]<<endl;
  cout << a[k - 1];
  rt0;
}

Fixed submission:
#include <algorithm>
// #include <cstdio>
// #include <functional>
#include <iostream>
// #include <cfloat>
// #include <climits>
// #include <cstdlib>
// #include <cstring>
// #include <cmath>
// #include <map>
// #include <queue>
// #include <set>
// #include <sstream>
// #include <stack>
#include <string>
// #include <time.h>
#include <vector>
// #include <random>
// #include <unordered_set>
// #include <complex>
#include <string.h>
#define rt0 return 0
#define rep(i, n) for (int i = 0; i < n; i++)
#define LL long long
#define pii pair<int, int>
using namespace std;
int main() {
  string s;
  int k, m = 0;
  cin >> s >> k;
  vector<string> a;
  rep(i, s.size()) {
    string tmp = "";
    for (int j = i; j < s.size() and j < i + k; j++)
      tmp.push_back(s[j]), a.push_back(tmp), m++;
  }
  sort(a.begin(), a.end());
  // cout<<a.size()<<endl;
  rep(i, m - 1) if (a[i] == a[i + 1]) a.erase(a.begin() + i), i--;
  // if(a[0]==a[1]) a.erase(a.begin());
  // cout<<a[0]<<endl<<a[1]<<endl<<a[2]<<endl<<a[3]<<endl<<a[4]<<endl<<a[5]<<endl;
  cout << a[k - 1];
  rt0;
}
Problem p03355 (C++)

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

using namespace std;

int main(void) {
  string s;
  int K;
  cin >> s >> K;

  set<string> st;
  for (int i = 0; i < s.size(); i++) {
    for (int j = 1; j < s.size() - i + 1; j++) {
      st.insert(s.substr(i, j));
    }
  }

  for (int i = 0; i < K - 1; i++) {
    st.erase(st.begin());
  }

  cout << *st.begin() << endl;
  return 0;
}


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

using namespace std;

int main(void) {
  string s;
  int K;
  cin >> s >> K;

  set<string> st;
  for (int i = 0; i < s.size(); i++) {
    for (int j = 1; j < min((int)s.size() - i + 1, K + 1); j++) {
      st.insert(s.substr(i, j));
    }
  }

  for (int i = 0; i < K - 1; i++) {
    st.erase(st.begin());
  }

  cout << *st.begin() << endl;
  return 0;
}
Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define MOD (1000000007l)
#define rep(i, n) for (long i = 0; i < (n); i++)

using namespace std;

void solve() {
  string s;
  long K;
  cin >> s >> K;

  set<string> st;
  for (long i = 0; i < s.size(); i++) {
    for (long j = 1; i + j <= s.size(); j++) {
      st.insert(s.substr(i, j));
    }
  }

  auto it = st.begin();
  rep(i, K - 1) it++;

  cout << *it << endl;
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout.precision(12);
  solve();
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define MOD (1000000007l)
#define rep(i, n) for (long i = 0; i < (n); i++)

using namespace std;

void solve() {
  string s;
  long K;
  cin >> s >> K;

  set<string> st;
  for (long i = 0; i < s.size(); i++) {
    for (long j = 1; i + j <= s.size() && j <= 5; j++) {
      st.insert(s.substr(i, j));
    }
  }

  auto it = st.begin();
  rep(i, K - 1) it++;

  cout << *it << endl;
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout.precision(12);
  solve();
  return 0;
}
Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)

string s;
int k;
set<string> st;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> s >> k;
  int n = s.size();
  rep(i, n) for (int j = i + 1; j <= n; j++) {
    st.insert(s.substr(i, j - i));
    if (st.size() > k)
      st.erase(--st.end());
  }
  cout << *prev(st.end()) << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)

string s;
int k;
set<string> st;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> s >> k;
  int n = s.size();
  rep(i, n) for (int j = i + 1; j <= n; j++) {
    if (j - i <= k)
      st.insert(s.substr(i, j - i));
    if (st.size() > k)
      st.erase(--st.end());
  }
  cout << *prev(st.end()) << endl;
  return 0;
}
Problem p03355 (C++)

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

int main() {
  string s, a;
  cin >> s;
  int K, u, c, N;
  N = s.size();
  cin >> K;
  u = 0;
  c = 0;
  vector<string> S(200000);
  for (int i = 0; i < N - 4; i++) {
    a = s[i];
    a += s[i + 1];
    a += s[i + 2];
    a += s[i + 3];
    a += s[i + 4];
    S[i] = a;
    u = i + 1;
  }

  for (int i = u; i < u + N - 3; i++) {
    a = s[i - u];
    a += s[i - u + 1];
    a += s[i - u + 2];
    a += s[i - u + 3];
    S[i] = a;
    c = i + 1;
  }
  for (int i = c; i < c + N - 2; i++) {
    a = s[i - c];
    a += s[i - c + 1];
    a += s[i - c + 2];
    S[i] = a;
    u = i + 1;
  }
  for (int i = u; i < u + N - 1; i++) {
    a = s[i - u];
    a += s[i - u + 1];
    S[i] = a;
    c = i + 1;
  }
  for (int i = c; i < c + N; i++) {
    S[i] = s[i - c];
  }
  c = 1;

  if (N != 1) {
    sort(S.begin(), S.end());
  }
  for (int i = 0; i < 200000; i++) {
    if (S[i] == "") {
      continue;
    }
    if (S[i] == S[i - 1]) {
      continue;
    } else {
      if (K == c) {
        cout << S[i] << endl;
        return 0;
      } else {
        c++;
        continue;
      }
    }
  }
}


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

int main() {
  string s, a;
  cin >> s;
  int K, u, c, N;
  N = s.size();
  cin >> K;
  u = 0;
  c = 0;
  vector<string> S(200000);
  for (int i = 0; i < N - 4; i++) {
    a = s[i];
    a += s[i + 1];
    a += s[i + 2];
    a += s[i + 3];
    a += s[i + 4];
    S[i] = a;
    u = i + 1;
  }

  for (int i = u; i < u + N - 3; i++) {
    a = s[i - u];
    a += s[i - u + 1];
    a += s[i - u + 2];
    a += s[i - u + 3];
    S[i] = a;
    c = i + 1;
  }
  for (int i = c; i < c + N - 2; i++) {
    a = s[i - c];
    a += s[i - c + 1];
    a += s[i - c + 2];
    S[i] = a;
    u = i + 1;
  }
  for (int i = u; i < u + N - 1; i++) {
    a = s[i - u];
    a += s[i - u + 1];
    S[i] = a;
    c = i + 1;
  }
  for (int i = c; i < c + N; i++) {
    S[i] = s[i - c];
  }
  c = 1;

  if (N != 1) {
    sort(S.begin(), S.end());
  } else {
    cout << S[0] << endl;
    return 0;
  }
  for (int i = 0; i < 200000; i++) {
    if (S[i] == "") {
      continue;
    }
    if (S[i] == S[i - 1]) {
      continue;
    } else {
      if (K == c) {
        cout << S[i] << endl;
        return 0;
      } else {
        c++;
        continue;
      }
    }
  }
}
Problem p03355 (C++)

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

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const ull mod = 1e9 + 7;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)

int main() {
  string s, t;
  int K;
  cin >> s >> K;
  int N = s.length();
  set<string> st;
  REP(i, N) {
    for (int j = i + 1; j < N + 6; j++) {
      t = s.substr(i, j - i);
      st.insert(t);
    }
  }
  set<string>::iterator itr = st.begin();
  REP(i, K - 1) { itr++; }
  cout << *itr << endl;
  return 0;
}

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

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const ull mod = 1e9 + 7;
#define REP(i, n) for (int i = 0; i < (int)n; ++i)

int main() {
  string s, t;
  int K;
  cin >> s >> K;
  int N = s.length();
  set<string> st;
  REP(i, N) {
    for (int j = i + 1; j < i + 6; j++) {
      t = s.substr(i, j - i);
      st.insert(t);
    }
  }
  set<string>::iterator itr = st.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;

unordered_set<string> seen;
bool visited[26];

int main() {
  string s;
  int k;
  cin >> s >> k;
  int n = (int)s.length();
  while ((int)seen.size() < k) {
    vector<int> pos;
    int minn = 27;
    for (int i = 0; i < n; i++) {
      int key = s[i] - 'a';
      if (!visited[key]) {
        if (key < minn) {
          pos.clear();
          pos.push_back(i);
          minn = key;
        } else if (key == minn) {
          pos.push_back(i);
        }
      }
    }
    visited[minn] = true;
    for (int x : pos) {
      for (int j = 1; j + x <= n; j++) {
        seen.insert(s.substr(x, j));
      }
    }
  }
  vector<string> vec;
  for (string str : seen)
    vec.push_back(str);
  sort(vec.begin(), vec.end());
  cout << vec[k - 1] << endl;
  return 0;
}


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

using namespace std;

unordered_set<string> seen;
bool visited[26];

int main() {
  string s;
  int k;
  cin >> s >> k;
  int n = (int)s.length();
  for (int i = 0; i < n; i++) {
    for (int j = 1; j <= k && i + j <= n; j++) {
      seen.insert(s.substr(i, j));
    }
  }
  vector<string> vec;
  for (string str : seen)
    vec.push_back(str);
  sort(vec.begin(), vec.end());
  cout << vec[k - 1] << endl;
  return 0;
}