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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <cmath>
#include <numeric>
using namespace std;
#define rep(i, a, b) for (int64_t i = (a); i < (b); ++i) // a ≦ i < b
#define Rrep(i, a, b)                                                          \
  for (int64_t i = (a); i >= (b); --i) // reverse repeat. a から b まで減少．
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend() // 逆イテレータ
#define INF 1000000000000000;
#define MOD 1000000007LL;
#define RANGE(a, b, c) (a).begin() + (b), (a).begin() + (c) // [b,c) イテレータ
typedef pair<int64_t, int64_t> PII;
typedef vector<int64_t> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef vector<PII> VP;
typedef int64_t i64;

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

  string s;
  int K;
  cin >> s >> K;

  VS subs;
  rep(i, 1, s.size() + 1) {
    string sub = s.substr(0, i);
    rep(j, 0, s.size() - i + 1) {
      subs.push_back(sub);
      sub.erase(0, 1);
      sub += s[j + i];
    }
  }
  sort(ALL(subs));
  unique(ALL(subs));

  cout << subs[K - 1] << endl;
}
// 書いて考える
// 境界，出力文字列 チェック
// 可読性優先．高速化次点．
// まずは全探索，分割統治，次にDP
// 制限を見る．境界に注意．求めたい量の変域．動かせる量．
// 偶奇，逆から，ソート，出現回数，出現位置，DP， 余事象，包除
// データ構造． 問題の特徴量．単調性，二分探索
// 存在判定：構成方法，入力の特徴
// gcd, lcm ,素因数分解．
// 例外を十分に含む一般化．想像力の限界
// 小さい系から例示
// 代数的処理
// 始めは過剰に例示・場合分けしてもいい．各場合を確実に対処．
// 自明な例から処理，除外．
// 小数のときは，精度の設定する．doubel 変数に数値を入力するときは 123. とする．
// テストケース作成は数表あり
// 実行エラー：vector添え字超え．0割り．


Fixed submission:
#include <bits/stdc++.h>
#include <cmath>
#include <numeric>
using namespace std;
#define rep(i, a, b) for (int64_t i = (a); i < (b); ++i) // a ≦ i < b
#define Rrep(i, a, b)                                                          \
  for (int64_t i = (a); i >= (b); --i) // reverse repeat. a から b まで減少．
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend() // 逆イテレータ
#define INF 1000000000000000;
#define MOD 1000000007LL;
#define RANGE(a, b, c) (a).begin() + (b), (a).begin() + (c) // [b,c) イテレータ
typedef pair<int64_t, int64_t> PII;
typedef vector<int64_t> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef vector<PII> VP;
typedef int64_t i64;

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

  string s;
  int K;
  cin >> s >> K;

  VS subs;
  rep(i, 1, K + 1) rep(j, 0, s.size() - i + 1) subs.push_back(s.substr(j, i));

  sort(ALL(subs));
  unique(ALL(subs));

  cout << subs[K - 1] << endl;
}
// 書いて考える
// 境界，出力文字列 チェック
// 可読性優先．高速化次点．
// まずは全探索，分割統治，次にDP
// 制限を見る．境界に注意．求めたい量の変域．動かせる量．
// 偶奇，逆から，ソート，出現回数，出現位置，DP， 余事象，包除
// データ構造． 問題の特徴量．単調性，二分探索
// 存在判定：構成方法，入力の特徴
// gcd, lcm ,素因数分解．
// 例外を十分に含む一般化．想像力の限界
// 小さい系から例示
// 代数的処理
// 始めは過剰に例示・場合分けしてもいい．各場合を確実に対処．
// 自明な例から処理，除外．
// 小数のときは，精度の設定する．doubel 変数に数値を入力するときは 123. とする．
// テストケース作成は数表あり
// 実行エラー：vector添え字超え．0割り．
Problem p03355 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
#include <string>
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORR(i, a, b) for (ll i = (a); i <= (b); i++)
#define repR(i, n) for (ll i = n; i >= 0; i--)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define F first
#define S second
#define pb push_back
#define pu push
#define COUT(x) cout << (x) << endl
#define PQ priority_queue<ll>
#define PQR priority_queue<ll, vector<ll>, greater<ll>>
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define mp make_pair
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
#define sz(x) (int)(x).size()
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll MOD = 1000000007LL;
const ll INF = 1LL << 60;
using vll = vector<ll>;
using vvll = vector<vll>;
using vstr = vector<string>;
using pll = pair<ll, ll>;
int main() {
  string s;
  cin >> s;
  ll k;
  cin >> k;
  ll n = s.size();
  set<string> h;
  rep(i, n) {
    ll l = min(n - i - 1, k);
    for (int j = 1; j <= l; j++) {
      string f = s.substr(i, j);
      h.insert(f);
    }
  }
  auto iu = h.begin();
  rep(i, k - 1) iu++;
  COUT(*iu);
}


Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
#include <string>
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORR(i, a, b) for (ll i = (a); i <= (b); i++)
#define repR(i, n) for (ll i = n; i >= 0; i--)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define F first
#define S second
#define pb push_back
#define pu push
#define COUT(x) cout << (x) << endl
#define PQ priority_queue<ll>
#define PQR priority_queue<ll, vector<ll>, greater<ll>>
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define mp make_pair
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
#define sz(x) (int)(x).size()
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll MOD = 1000000007LL;
const ll INF = 1LL << 60;
using vll = vector<ll>;
using vvll = vector<vll>;
using vstr = vector<string>;
using pll = pair<ll, ll>;
int main() {
  string s;
  cin >> s;
  ll k;
  cin >> k;
  ll n = s.size();
  set<string> h;
  rep(i, n) {
    ll l = min(n - i, k);
    for (int j = 1; j <= l; j++) {
      string f = s.substr(i, j);
      h.insert(f);
    }
  }
  auto iu = h.begin();
  rep(i, k - 1) iu++;
  COUT(*iu);
}
Problem p03355 (C++)

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

int main() {
  string s;
  int k;
  cin >> s >> k;
  vector<string> sub;
  for (int i = 1; i <= s.size(); i++) {
    for (int j = 0; j < s.size() - i + 1; j++) {
      sub.push_back(s.substr(j, i));
    }
  }
  sort(sub.begin(), sub.end());
  int now = 0;
  string ss = "0000";
  for (int i = 0; i < sub.size(); i++) {
    if (sub[i] != ss) {
      ss = sub[i];
      now++;
      if (now == k) {
        cout << ss << endl;
        break;
      }
    }
  }
}

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

int main() {
  string s;
  int k;
  cin >> s >> k;
  vector<string> sub;
  for (int i = 1; i <= k; i++) {
    for (int j = 0; j < s.size() - i + 1; j++) {
      sub.push_back(s.substr(j, i));
    }
  }
  sort(sub.begin(), sub.end());
  int now = 0;
  string ss = "0000";
  for (int i = 0; i < sub.size(); i++) {
    if (sub[i] != ss) {
      ss = sub[i];
      now++;
      if (now == k) {
        cout << ss << endl;
        break;
      }
    }
  }
}
Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <set>
using namespace std;
int main() {
  string s;
  int K;
  cin >> s >> K;
  set<string> S;
  for (int i = 0; i < s.length(); ++i) {
    for (int j = 0; j <= s.length() - i; ++j)
      S.insert(s.substr(i, j));
  }
  auto itr = S.begin();
  for (int i = 0; i < K; ++i) {
    // cout << *itr << endl;
    ++itr;
  }
  cout << *itr << endl;
  return 0;
}


Fixed submission:
#include <iostream>
#include <set>
using namespace std;
int main() {
  string s;
  int K;
  cin >> s >> K;
  set<string> S;
  for (int i = 0; i < s.length(); ++i) {
    for (int j = 0; j <= min((int)s.length() - i, K); ++j)
      S.insert(s.substr(i, j));
  }
  auto itr = S.begin();
  for (int i = 0; i < K; ++i) {
    // cout << *itr << endl;
    ++itr;
  }
  cout << *itr << endl;
  return 0;
}
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> myVec;
  for (int i = 0; i < s.length(); i++) {
    string temp;
    for (int j = i; j < s.length(); j++)
      temp.push_back(s[j]), myVec.insert(temp);
  }
  k--;
  while (k--)
    myVec.erase(myVec.begin());
  cout << *myVec.begin();
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  string s;
  int k;
  cin >> s >> k;
  set<string> myVec;
  for (int i = 0; i < s.length(); i++) {
    string temp;
    for (int j = i; j < s.length() && j < i + 10; j++)
      temp.push_back(s[j]), myVec.insert(temp);
  }
  k--;
  while (k--)
    myVec.erase(myVec.begin());
  cout << *myVec.begin();
}
Problem p03355 (C++)

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

using namespace std;

priority_queue<string /*, vector<string>, greater<string>*/> a;

map<string, bool> ap;

int n, k;

int cat;

string s;
string b;

struct elem {
  string x;
  int poz;
  int ok;
};

elem v[50001];

struct tip {
  int poz;
  char x;
};

tip s2[50001];

int mn = 99;

bool cmp(int a, int b) {
  if (a == b)
    return 0;

  return 1;
}

void baga() {
  int i, j;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= cat; j++) {
      if (v[j].poz < n && v[j].ok == 0) {
        string b = v[j].x;
        b.resize(i);
        b[i - 1] = s[v[j].poz];
        v[j].poz++;

        if (ap[b] == 0) {
          ap[b] = 1;

          if (a.size() < k)
            a.push(b);

          else if (a.top() > b) {
            a.pop();
            a.push(b);
          }

          else
            v[j].ok = 1;
        }

        v[j].x = b;
      }
    }
  }
}

struct cmps {
  bool operator()(const tip &a, const tip &b) {
    if (a.x != b.x)
      return a.x < b.x;

    return a.poz < b.poz;
  }
};

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

  n = s.size();

  int i;
  for (i = 1; i <= n; i++) {
    s2[i].poz = i - 1;
    s2[i].x = s[i - 1];
  }

  sort(s2 + 1, s2 + n + 1, cmps());

  char last = s2[min(n, k)].x;

  cat = 0;
  for (i = 1; i <= n && s2[i].x <= last; i++) {
    v[++cat].poz = s2[i].poz;
    v[cat].x.resize(1);
    v[cat].x[0] = s[s2[i].poz];
  }

  baga();

  cout << a.top() << "\n";

  return 0;
}


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

using namespace std;

priority_queue<string /*, vector<string>, greater<string>*/> a;

map<string, bool> ap;

int n, k;

int cat;

string s;
string b;

struct elem {
  string x;
  int poz;
  int ok;
};

elem v[50001];

struct tip {
  int poz;
  char x;
};

tip s2[50001];

int mn = 99;

bool cmp(int a, int b) {
  if (a == b)
    return 0;

  return 1;
}

void baga() {
  int i, j;
  for (i = 1; i <= min(n, k); i++) {
    for (j = 1; j <= cat; j++) {
      if (v[j].poz < n && v[j].ok == 0) {
        string b = v[j].x;
        b.resize(i);
        b[i - 1] = s[v[j].poz];
        v[j].poz++;

        if (ap[b] == 0) {
          ap[b] = 1;

          if (a.size() < k)
            a.push(b);

          else if (a.top() > b) {
            a.pop();
            a.push(b);
          }

          else
            v[j].ok = 1;
        }

        v[j].x = b;
      }
    }
  }
}

struct cmps {
  bool operator()(const tip &a, const tip &b) {
    if (a.x != b.x)
      return a.x < b.x;

    return a.poz < b.poz;
  }
};

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

  n = s.size();

  int i;
  for (i = 1; i <= n; i++) {
    s2[i].poz = i - 1;
    s2[i].x = s[i - 1];
  }

  sort(s2 + 1, s2 + n + 1, cmps());

  char last = s2[min(n, k)].x;

  cat = 0;
  for (i = 1; i <= n && s2[i].x <= last; i++) {
    v[++cat].poz = s2[i].poz;
    v[cat].x.resize(1);
    v[cat].x[0] = s[s2[i].poz];
  }

  baga();

  cout << a.top() << "\n";

  return 0;
}
Problem p03355 (C++)

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

#define N 5500 * 5500

string s;
string res[N];

int main() {
  int k;
  ios_base::sync_with_stdio(false);
  while (cin >> s >> k) {
    int cnt = 0;
    set<string> se;
    int len = (int)s.size();
    for (int i = 0; i < len; ++i) {
      // cout << i <<endl;
      string tmp = "";
      int cc = 0;
      for (int j = i; j < len && cc < 5; ++j) {
        tmp += s[j];
        // cout << tmp <<endl;
        se.insert(tmp);
      }
    }
    cnt = 1;
    for (set<string>::iterator it = se.begin(); it != se.end(); it++) {
      if (cnt == k) {
        cout << *it << '\n';
        break;
      }
      cnt++;
    }
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <set>
#include <stdio.h>
#include <string>
using namespace std;

#define N 5500 * 5500

string s;
string res[N];

int main() {
  int k;
  ios_base::sync_with_stdio(false);
  while (cin >> s >> k) {
    int cnt = 0;
    set<string> se;
    int len = (int)s.size();
    for (int i = 0; i < len; ++i) {
      // cout << i <<endl;
      string tmp = "";
      int cc = 0;
      for (int j = i; j < len && cc < 5; ++j) {
        tmp += s[j];
        // cout << tmp <<endl;
        se.insert(tmp);
        cc++;
      }
    }
    cnt = 1;
    for (set<string>::iterator it = se.begin(); it != se.end(); it++) {
      if (cnt == k) {
        cout << *it << '\n';
        break;
      }
      cnt++;
    }
  }
  return 0;
}
Problem p03355 (C++)

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

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef pair<ll, ll> pll;
typedef pair<double, int> pdi;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
#ifdef VC
#define scanf scanf_s
#endif
#define iINF (1 << 30)
#define INF (1LL << 59)
#define forn(i) for (int i = 0; i < n; ++i)
#define form(i, n) for (int i = 0; i < n; ++i)
#define NIL -1

const int mod = 1e9 + 7;
int n;
int m;
string s;
string as[5];

int main() {
  cin >> s;
  int k;
  cin >> k;
  form(i, k) { as[i] = "}"; }
  for (int i = 0; i < s.length(); i++) {
    for (int j = i; j < s.length(); j++) {
      form(l, k) {
        string temp;
        temp = s.substr(i, j - i + 1);
        if (temp < as[l]) {
          for (int p = k - 1; p > l; p--) {
            as[p] = as[p - 1];
          }
          as[l] = temp;
          break;
        } else if (temp == as[l])
          break;
      }
    }
  }
  cout << as[k - 1] << endl;
  return 0;
}


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

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef pair<ll, ll> pll;
typedef pair<double, int> pdi;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
#ifdef VC
#define scanf scanf_s
#endif
#define iINF (1 << 30)
#define INF (1LL << 59)
#define forn(i) for (int i = 0; i < n; ++i)
#define form(i, n) for (int i = 0; i < n; ++i)
#define NIL -1

const int mod = 1e9 + 7;
int n;
int m;
string s;
string as[5];

int main() {
  cin >> s;
  int k;
  cin >> k;
  form(i, k) { as[i] = "}"; }
  for (int i = 0; i < s.length(); i++) {
    for (int j = i; j < min((int)s.length(), i + 5); j++) {
      form(l, k) {
        string temp;
        temp = s.substr(i, j - i + 1);
        if (temp < as[l]) {
          for (int p = k - 1; p > l; p--) {
            as[p] = as[p - 1];
          }
          as[l] = temp;
          break;
        } else if (temp == as[l])
          break;
      }
    }
  }
  cout << as[k - 1] << endl;
  return 0;
}
Problem p03355 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
vector<string> v;
int main() {
  string s;
  int k;
  cin >> s >> k;
  for (int i = 0; i < s.size(); i++) {
    string cur;
    for (int j = i; j < s.size(); j++)
      cur += s[j], v.push_back(cur);
  }
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  cout << v[k - 1];
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
vector<string> v;
int main() {
  string s;
  int k;
  cin >> s >> k;
  for (int i = 0; i < s.size(); i++) {
    string cur;
    for (int j = i; j < s.size() && j < i + k; j++)
      cur += s[j], v.push_back(cur);
  }
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  cout << v[k - 1];
}
Problem p03355 (C++)

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string s;
  cin >> s;
  int k;
  cin >> k;
  int l = s.size();
  vector<string> top;
  for (int i = 0; i < l; i++) {
    for (int j = i; j < l; j++) {
      string st = s.substr(i, j - i + 1);
      bool flag = true;
      for (string a : top)
        flag &= (a != st);
      if (flag) {
        top.push_back(st);
        sort(top.begin(), top.end());
        if (top.size() == k + 1)
          top.resize(k);
      }
    }
  }
  cout << top[k - 1] << endl;
  return 0;
}

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string s;
  cin >> s;
  int k;
  cin >> k;
  int l = s.size();
  vector<string> top;
  for (int i = 0; i < l; i++) {
    for (int j = i; j < l && j < i + k; j++) {
      string st = s.substr(i, j - i + 1);
      bool flag = true;
      for (string a : top)
        flag &= (a != st);
      if (flag) {
        top.push_back(st);
        sort(top.begin(), top.end());
        if (top.size() == k + 1)
          top.resize(k);
      }
    }
  }
  cout << top[k - 1] << endl;
  return 0;
}
Problem p03355 (C++)

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

using namespace std;

char sir[5005];
char pp[5005];
int K;
set<string> myset;
vector<string> v;
int main() {
  cin >> sir;

  int lg = strlen(sir);
  cin >> K;

  for (int oLungime = 1; oLungime <= 5; ++oLungime) {
    for (int pos = 0; pos < lg - oLungime; ++pos) {
      string rr = "";
      for (int parc = pos; parc <= pos + oLungime - 1; ++parc) {
        rr = rr + char(sir[parc]);
      }
      if (myset.find(rr) == myset.end()) {
        myset.insert(rr);
        v.push_back(rr);
      }
    }
  }

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

  cout << v[K - 1] << '\n';

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <set>
#include <string>
#include <vector>

using namespace std;

char sir[5005];
char pp[5005];
int K;
set<string> myset;
vector<string> v;
int main() {
  cin >> sir;

  int lg = strlen(sir);
  cin >> K;

  for (int oLungime = 1; oLungime <= 5; ++oLungime) {
    for (int pos = 0; pos < lg - oLungime + 1; ++pos) {
      string rr = "";
      for (int parc = pos; parc <= pos + oLungime - 1; ++parc) {
        rr = rr + char(sir[parc]);
      }
      if (myset.find(rr) == myset.end()) {
        myset.insert(rr);
        v.push_back(rr);
      }
    }
  }

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

  cout << v[K - 1] << '\n';

  return 0;
}
Problem p03355 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ALL(v) v.begin(), v.end()
// greater<int>()
typedef long long ll;
ll GCD(ll x, ll y) { return y ? GCD(y, x % y) : x; }
ll LCM(ll x, ll y) { return x * y / GCD(x, y); }

using namespace std;
int main() {
  string s;
  int K;

  cin >> s >> K;
  vector<string> array;
  for (int i = 0; i < s.length(); i++) {
    for (int j = 1; j < s.length() - i + 1; j++) {
      array.push_back(s.substr(i, j));
    }
  }

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

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

Fixed submission:
#include <bits/stdc++.h>
#define ALL(v) v.begin(), v.end()
// greater<int>()
typedef long long ll;
ll GCD(ll x, ll y) { return y ? GCD(y, x % y) : x; }
ll LCM(ll x, ll y) { return x * y / GCD(x, y); }

using namespace std;
int main() {
  string s;
  int K;

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

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

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
int64_t MOD = 1000000007;

int main() {
  vector<string> ans;
  string S;
  cin >> S;
  int k = S.size();
  vector<string> T(k);
  for (int i = 0; i < k; i++) {
    char temp = S.at(i);
    T.at(i) = temp;
  }
  int a = min(k, 5);
  for (int i = 0; i < a; i++) {
    for (int j = 0; j < k - i; j++) {
      string d;
      if (i == 0)
        d = T.at(j);
      if (a > 1 && i == 1)
        d = T.at(j) + T.at(j + 1);
      if (a > 2 && i == 2)
        d = T.at(j) + T.at(j + 1) + T.at(j + 2);
      if (a > 3 && i == 3)
        d = T.at(j) + T.at(j + 1) + T.at(j + 2) + T.at(j + 3);
      if (a > 4 && i == 4)
        d = T.at(j) + T.at(j + 1) + T.at(j + 2) + T.at(j + 3) + T.at(j + 4);
      ans.push_back(d);
    }
  }
  int W;
  cin >> W;
  sort(ans.begin(), ans.end());
  int i = 1, count = 1;
  while (true) {
    if (ans.at(i) != ans.at(i - 1))
      count++;
    if (count == W) {
      cout << ans.at(i);
      break;
    }
    i++;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
int64_t MOD = 1000000007;

int main() {
  vector<string> ans;
  string S;
  cin >> S;
  int k = S.size();
  vector<string> T(k);
  for (int i = 0; i < k; i++) {
    char temp = S.at(i);
    T.at(i) = temp;
  }
  int a = min(k, 5);
  for (int i = 0; i < a; i++) {
    for (int j = 0; j < k - i; j++) {
      string d;
      if (i == 0)
        d = T.at(j);
      if (a > 1 && i == 1)
        d = T.at(j) + T.at(j + 1);
      if (a > 2 && i == 2)
        d = T.at(j) + T.at(j + 1) + T.at(j + 2);
      if (a > 3 && i == 3)
        d = T.at(j) + T.at(j + 1) + T.at(j + 2) + T.at(j + 3);
      if (a > 4 && i == 4)
        d = T.at(j) + T.at(j + 1) + T.at(j + 2) + T.at(j + 3) + T.at(j + 4);
      ans.push_back(d);
    }
  }
  int W;
  cin >> W;
  sort(ans.begin(), ans.end());
  int i = 1, count = 1;
  while (true) {
    if (S.size() == 1) {
      cout << S.at(0);
      break;
    }
    if (ans.at(i) != ans.at(i - 1))
      count++;
    if (count == W) {
      cout << ans.at(i);
      break;
    }
    i++;
  }
}
Problem p03355 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#include <set>
#include <string>
#define debug cout << "debug" << endl
using namespace std;
string s;
string sub;
set<string> se;
int k;

void debug1() {
  debug;
  set<string>::iterator it = se.begin();
  for (; it != se.end(); it++)
    cout << *it << endl;
  debug;
}

int main() {
  // std::ios::sync_with_stdio(false);
  cin >> s;
  cin >> k;
  int len = s.length();
  for (int i = 0; i < 26; i++) {
    if (se.size() > 5)
      break;
    char ch = 'a' + i;
    for (int j = 0; j < len; j++)
      for (int t = 1; t <= k; t++) {
        if (j + t >= len)
          break;
        sub = s.substr(j, t);
        se.insert(sub);
      }
  }
  set<string>::iterator it = se.begin();
  for (int i = 1; i < k && it != se.end(); i++, it++)
    ;
  cout << *it << endl;
  // return 0;
}


Fixed submission:
#include <cstdio>
#include <iostream>
#include <set>
#include <string>
#define debug cout << "debug" << endl
using namespace std;
string s;
string sub;
set<string> se;
int k;

void debug1() {
  debug;
  set<string>::iterator it = se.begin();
  for (; it != se.end(); it++)
    cout << *it << endl;
  debug;
}

int main() {
  // std::ios::sync_with_stdio(false);
  cin >> s;
  cin >> k;
  int len = s.length();
  for (int i = 0; i < 26; i++) {
    if (se.size() > 5)
      break;
    char ch = 'a' + i;
    for (int j = 0; j < len; j++)
      for (int t = 1; t <= k; t++) {
        if (j + t > len)
          break;
        sub = s.substr(j, t);
        se.insert(sub);
      }
  }
  set<string>::iterator it = se.begin();
  for (int i = 1; i < k && it != se.end(); i++, it++)
    ;
  cout << *it << endl;
  // return 0;
}
Problem p03355 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

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

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

Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

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

  set<string> st;
  for (int i = 1; i <= k; i++) {
    if (i > s.size()) {
      break;
    }
    for (int j = 0; j <= s.size() - i; j++) {
      st.insert(s.substr(j, i));
    }
  }
  auto a = st.begin();
  for (int i = 0; i < k - 1; i++) {
    a++;
  }
  cout << *a;
}
Problem p03355 (C++)

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

using namespace std;

string s;
int K;

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

  string s_ = s;
  sort(s_.begin(), s_.end());
  s_.erase(std::unique(s_.begin(), s_.end()), s_.end());

  set<string> substr;
  for (auto c : s_) {
    auto pos = s.find(c);
    while (pos != string::npos) {
      for (int i = 1; i <= s.size() - pos; ++i) {
        substr.insert(s.substr(pos, i));
      }
      pos = s.find(c, pos + 1);
    }
  }
  auto it = substr.begin();
  for (int i = 0; i < K - 1; ++i)
    ++it;
  cout << *it << endl;
}


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

using namespace std;

string s;
int K;

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

  string s_ = s;
  sort(s_.begin(), s_.end());
  s_.erase(std::unique(s_.begin(), s_.end()), s_.end());

  set<string> substr;
  for (auto c : s_) {
    auto pos = s.find(c);
    while (pos != string::npos) {
      for (int i = 1; i <= min((int)(s.size() - pos), 5); ++i) {
        substr.insert(s.substr(pos, i));
      }
      pos = s.find(c, pos + 1);
    }
  }
  auto it = substr.begin();
  for (int i = 0; i < K - 1; ++i)
    ++it;
  cout << *it << endl;
}
Problem p03355 (C++)

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

using namespace std;

const int MOD = 1e9 + 7;
const double PI = 3.14159265359;

int main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);

  string s;
  cin >> s;

  int k, n = s.size();
  cin >> k;

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

  return 0;
}

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

using namespace std;

const int MOD = 1e9 + 7;
const double PI = 3.14159265359;

int main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);

  string s;
  cin >> s;

  int k, n = s.size();
  cin >> k;

  vector<string> vs;
  for (int i = 0; i < n; i++) {
    for (int j = i; j < min(n, i + 5); j++) {
      vs.push_back(s.substr(i, j - i + 1));
    }
  }
  sort(vs.begin(), vs.end());
  unique(vs.begin(), vs.end());
  cout << vs[k - 1];

  return 0;
}
Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):

// Problem : C - K-th Substring
// Contest : AtCoder Regular Contest 097
// URL : https://atcoder.jp/contests/arc097/tasks/arc097_a
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include "bits/stdc++.h"
using ll = long long;
using namespace std;
#define size(x) (int)x.size()

const int N = 1e6 + 1, mod = 1e9 + 7;

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

int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin >> s >> k;
  for (int j = 1; j <= size(s); j++) {
    for (int i = 0; i <= size(s) - j; i++) {
      ms.insert(s.substr(i, j));
    }
  }
  for (int t = 1; t < k; t++) {
    ms.erase(ms.begin());
  }
  cout << *ms.begin();
}


Fixed submission:

// Problem : C - K-th Substring
// Contest : AtCoder Regular Contest 097
// URL : https://atcoder.jp/contests/arc097/tasks/arc097_a
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include "bits/stdc++.h"
using ll = long long;
using namespace std;
#define size(x) (int)x.size()

const int N = 1e6 + 1, mod = 1e9 + 7;

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

int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin >> s >> k;
  for (int j = 1; j <= min(k, size(s)); j++) {
    for (int i = 0; i <= size(s) - j; i++) {
      ms.insert(s.substr(i, j));
    }
  }
  for (int t = 1; t < k; t++) {
    ms.erase(ms.begin());
  }
  cout << *ms.begin();
}
Problem p03355 (C++)

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

using namespace std;

#define ll long long
#define db double
#define up(i, j, n) for (int i = j; i <= n; i++)
#define down(i, j, n) for (int i = j; i >= n; i--)
#define cadd(a, b) a = add(a, b)
#define cpop(a, b) a = pop(a, b)
#define cmul(a, b) a = mul(a, b)
#define pr pair<int, int>
#define fi first
#define se second
#define SZ(x) (int)x.size()
#define bin(i) (1 << (i))
#define Auto(i, node) for (int i = LINK[node]; i; i = e[i].next)

template <typename T> inline bool cmax(T &x, T y) {
  return y > x ? x = y, true : false;
}
template <typename T> inline bool cmin(T &x, T y) {
  return y < x ? x = y, true : false;
}

const int MAXN = 5005;
const int MAXM = MAXN * 5;

string ss[MAXN], s;
int K, cnt;

int main() {
  cin >> s >> K;
  int len = s.length();
  up(i, 0, len - 1) up(j, i, min(i + K - 1, len - 1)) ss[++cnt] =
      s.substr(i, j - i + 1);
  sort(ss + 1, ss + cnt + 1);
  cnt = unique(ss + 1, ss + cnt + 1) - ss - 1;
  cout << ss[K] << endl;
  return 0;
}


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

using namespace std;

#define ll long long
#define db double
#define up(i, j, n) for (int i = j; i <= n; i++)
#define down(i, j, n) for (int i = j; i >= n; i--)
#define cadd(a, b) a = add(a, b)
#define cpop(a, b) a = pop(a, b)
#define cmul(a, b) a = mul(a, b)
#define pr pair<int, int>
#define fi first
#define se second
#define SZ(x) (int)x.size()
#define bin(i) (1 << (i))
#define Auto(i, node) for (int i = LINK[node]; i; i = e[i].next)

template <typename T> inline bool cmax(T &x, T y) {
  return y > x ? x = y, true : false;
}
template <typename T> inline bool cmin(T &x, T y) {
  return y < x ? x = y, true : false;
}

const int MAXN = 5005;
const int MAXM = MAXN * 5;

string ss[MAXM], s;
int K, cnt;

int main() {
  cin >> s >> K;
  int len = s.length();
  up(i, 0, len - 1) up(j, i, min(i + K - 1, len - 1)) ss[++cnt] =
      s.substr(i, j - i + 1);
  sort(ss + 1, ss + cnt + 1);
  cnt = unique(ss + 1, ss + cnt + 1) - ss - 1;
  cout << ss[K] << endl;
  return 0;
}
Problem p03355 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
#include <string>
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORR(i, a, b) for (ll i = (a); i <= (b); i++)
#define repR(i, n) for (ll i = n; i >= 0; i--)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define F first
#define S second
#define pb push_back
#define pu push
#define COUT(x) cout << (x) << endl
#define PQ priority_queue<ll>
#define PQR priority_queue<ll, vector<ll>, greater<ll>>
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define mp make_pair
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
#define sz(x) (int)(x).size()
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll MOD = 1000000007LL;
const ll INF = 1LL << 60;
using vll = vector<ll>;
using vvll = vector<vll>;
using vstr = vector<string>;
using pll = pair<ll, ll>;
int main() {
  string s;
  cin >> s;
  ll k;
  cin >> k;
  set<string> h;
  string kk;
  int si = s.size();
  int p = 0;
  rep(i, si) {
    for (int j = 1; j <= k; j++) {
      if (j + i >= si)
        continue;
      kk = s.substr(i, j);
      h.insert(kk);
    }
  }
  auto g = h.begin();
  for (int i = 0; i < k - 1; i++) {
    g++;
  }
  COUT(*g);
}


Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
#include <string>
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORR(i, a, b) for (ll i = (a); i <= (b); i++)
#define repR(i, n) for (ll i = n; i >= 0; i--)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define F first
#define S second
#define pb push_back
#define pu push
#define COUT(x) cout << (x) << endl
#define PQ priority_queue<ll>
#define PQR priority_queue<ll, vector<ll>, greater<ll>>
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define mp make_pair
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
#define sz(x) (int)(x).size()
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll MOD = 1000000007LL;
const ll INF = 1LL << 60;
using vll = vector<ll>;
using vvll = vector<vll>;
using vstr = vector<string>;
using pll = pair<ll, ll>;
int main() {
  string s;
  cin >> s;
  ll k;
  cin >> k;
  set<string> h;
  string kk;
  int si = s.size();
  int p = 0;
  rep(i, si) {
    for (int j = 1; j <= k; j++) {
      kk = s.substr(i, j);
      h.insert(kk);
    }
  }
  auto g = h.begin();
  for (int i = 0; i < k - 1; i++) {
    g++;
  }
  COUT(*g);
}
Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <vector>
using namespace std;
string sss[12500000];
int main() {

  string s;
  cin >> s;
  int k;
  cin >> k;
  int i, j;
  string ss;
  int l = 0;
  // priority_queue<string, vector<string>, greater<string>> que;
  for (i = 0; i < s.size(); i++) {
    ss = "";
    for (j = i; j < s.size(); j++) {
      ss = ss + s[j];
      sss[l] = ss;
      l++;
    }
  }
  ss = "";
  sort(sss, sss + l);
  for (i = 0; i < l; i++) {
    if (ss != sss[i]) {
      ss = sss[i];
      k--;
      // cout << ss << endl;
    }
    if (k == 0) {
      cout << ss << endl;
      return 0;
    }
  }
}

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <vector>
using namespace std;
string sss[12500000];
int main() {

  string s;
  cin >> s;
  int k;
  cin >> k;
  int i, j;
  string ss;
  int l = 0;
  // priority_queue<string, vector<string>, greater<string>> que;
  for (i = 0; i < s.size(); i++) {
    ss = "";
    for (j = i; j < s.size(); j++) {
      ss = ss + s[j];
      sss[l] = ss;
      l++;
      if (j > i + 6)
        break;
    }
  }
  ss = "";
  sort(sss, sss + l);
  for (i = 0; i < l; i++) {
    if (ss != sss[i]) {
      ss = sss[i];
      k--;
      // cout << ss << endl;
    }
    if (k == 0) {
      cout << ss << endl;
      return 0;
    }
  }
}
Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
const int INF = (((1 << 30) - 1) << 1) + 1;
const int nINF = 1 << 31;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<char, int> pci;

/////////////////////////////////////////////////////////////////////

int main() {
  ios::sync_with_stdio(false);
  // cout << fixed << setprecision(7);
  int k;
  string s;
  cin >> s >> k;
  int n = s.size();
  set<string> v;
  REP(i, n) {
    string cur;
    FOR(j, i, n - 1) {
      cur.push_back(s[j]);
      v.insert(cur);
    }
  }
  for (auto &str : v) {
    if (!(--k)) {
      cout << str << endl;
      exit(0);
    }
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
const int INF = (((1 << 30) - 1) << 1) + 1;
const int nINF = 1 << 31;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<char, int> pci;

/////////////////////////////////////////////////////////////////////

int main() {
  ios::sync_with_stdio(false);
  // cout << fixed << setprecision(7);
  int k;
  string s;
  cin >> s >> k;
  int n = s.size();
  set<string> v;
  REP(i, n) {
    string cur;
    FOR(j, i, min(n - 1, i + 4)) {
      cur.push_back(s[j]);
      v.insert(cur);
    }
  }
  for (auto &str : v) {
    if (!(--k)) {
      cout << str << endl;
      exit(0);
    }
  }

  return 0;
}
Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vll = vector<long long>;
using vp = vector<pair<int, int>>;
using vvi = vector<vector<int>>;
using vvc = vector<vector<char>>;
using vvll = vector<vector<long long>>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

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

  string s;
  int k;
  cin >> s >> k;
  int n = sz(s);
  vvi idx(26);
  for (char c = 'a'; c <= 'z'; ++c)
    rep(i, n) if (s[i] == c) idx[c - 'a'].pb(i);
  for (char c = 'a'; c <= 'z'; ++c) {
    set<string> st;
    for (int i : idx[c - 'a']) {
      for (int j = i; j < n; ++j) {
        st.insert(s.substr(i, j - i + 1));
      }
    }
    if (sz(st) >= k) {
      for (string ans : st) {
        k--;
        if (k == 0) {
          cout << ans << endl;
          return 0;
        }
      }
    } else {
      k -= sz(st);
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vll = vector<long long>;
using vp = vector<pair<int, int>>;
using vvi = vector<vector<int>>;
using vvc = vector<vector<char>>;
using vvll = vector<vector<long long>>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

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

  string s;
  int k;
  cin >> s >> k;
  int n = sz(s);
  vvi idx(26);
  for (char c = 'a'; c <= 'z'; ++c)
    rep(i, n) if (s[i] == c) idx[c - 'a'].pb(i);
  for (char c = 'a'; c <= 'z'; ++c) {
    set<string> st;
    for (int i : idx[c - 'a']) {
      for (int j = i; j < min(i + 5, n); ++j) {
        st.insert(s.substr(i, j - i + 1));
      }
    }
    if (sz(st) >= k) {
      for (string ans : st) {
        k--;
        if (k == 0) {
          cout << ans << endl;
          return 0;
        }
      }
    } else {
      k -= sz(st);
    }
  }
}
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;
  int n = s.size();
  set<string> st;
  for (int i = 0; i < n; i++) {
    for (int j = 1; i + j <= n; j++) {
      st.emplace(s.substr(i, j));
    }
  }
  int count = 0;
  for (auto &t : st) {
    if (++count == k) {
      cout << t << endl;
      break;
    }
  }
  return 0;
}

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

int main() {
  string s;
  int k;
  cin >> s >> k;
  int n = s.size();
  set<string> st;
  for (int i = 0; i < n; i++) {
    for (int j = 1; j <= 5; j++) {
      st.emplace(s.substr(i, j));
    }
  }
  int count = 0;
  for (auto &t : st) {
    if (++count == k) {
      cout << t << endl;
      break;
    }
  }
  return 0;
}
Problem p03355 (C++)

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

string inp;

int main() {
  int k;
  cin >> inp >> k;
  vector<string> vec;
  for (int i = 0; i < (int)inp.size(); i++) {
    for (int l = 1; i + l <= (int)inp.size(); l++) {
      vec.push_back(inp.substr(i, l));
    }
  }
  sort(vec.begin(), vec.end());
  vec.erase(unique(vec.begin(), vec.end()), vec.end());
  cout << vec[k - 1] << endl;
}


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

string inp;

int main() {
  int k;
  cin >> inp >> k;
  vector<string> vec;
  for (int i = 0; i < (int)inp.size(); i++) {
    for (int l = 1; l <= 5 && i + l <= (int)inp.size(); l++) {
      vec.push_back(inp.substr(i, l));
    }
  }
  sort(vec.begin(), vec.end());
  vec.erase(unique(vec.begin(), vec.end()), vec.end());
  cout << vec[k - 1] << endl;
}
Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define all(v) v.begin(), v.end()
using namespace std;

int min(int a, int b) { return (a < b) ? a : b; }

int main() {
  string s;
  int k, n;
  cin >> s >> k;
  vector<string> vec;
  set<string> st;
  rep(i, s.size()) {
    for (int j = i; j < min(s.size(), j + k); j++) {
      string next = s.substr(i, j - i + 1);
      if (st.find(next) != st.end())
        continue;
      vec.pb(s.substr(i, j - i + 1));
      st.insert(next);
    }
  }
  sort(all(vec));
  cout << vec[k - 1] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define all(v) v.begin(), v.end()
using namespace std;

int min(int a, int b) { return (a < b) ? a : b; }

int main() {
  string s;
  int k, n;
  cin >> s >> k;
  vector<string> vec;
  set<string> st;
  rep(i, s.size()) {
    for (int j = i; j < min(s.size(), i + k); j++) {
      string next = s.substr(i, j - i + 1);
      if (st.find(next) != st.end())
        continue;
      vec.pb(s.substr(i, j - i + 1));
      st.insert(next);
    }
  }
  sort(all(vec));
  cout << vec[k - 1] << endl;
  return 0;
}
Problem p03355 (C++)

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

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

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

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

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

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

int main(void) {
  string s;
  cin >> s;
  int k;
  cin >> k;
  vector<string> v;
  rep(i, s.size()) {
    reps(j, 1, s.size() - i + 1) {
      v.push_back(s.substr(i, j));
      // cout << s.substr(i, j) << endl;
    }
  }
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  cout << v[k - 1] << endl;
  return 0;
}

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

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

int main(void) {
  string s;
  cin >> s;
  int k;
  cin >> k;
  vector<string> v;
  rep(i, s.size()) {
    reps(j, 1, min((int)s.size() - i + 1, 6)) {
      v.push_back(s.substr(i, j));
      // cout << s.substr(i, j) << endl;
    }
  }
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  cout << v[k - 1] << endl;
  return 0;
}
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;
  int N = s.size();
  priority_queue<string, vector<string>, greater<string>> pq;
  set<string> se;
  string temp;
  for (int i = 0; i < N; i++) {
    pq.push(s.substr(i, 1));
  }
  se.insert(pq.top());
  for (int i = 1; i < K; i++) {
    bool flag = false;
    for (int j = N - i; j < N; j++) {
      for (int k = 1; k <= N - j; k++) {
        if (pq.top() == s.substr(j, k)) {
          pq.pop();
          flag = true;
          break;
        }
      }
      if (flag) {
        break;
      }
    }
    if (se.count(pq.top())) {
      temp = pq.top();
      while (temp == pq.top()) {
        pq.pop();
        if (pq.empty()) {
          break;
        }
      }
      for (int j = 0; j < N - temp.size(); j++) {
        if (temp == s.substr(j, temp.size())) {
          pq.push(s.substr(j, temp.size() + 1));
        }
      }
    }
    se.insert(pq.top());
  }
  cout << *(next(se.begin(), K - 1)) << endl;
  return 0;
}


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

int main() {
  string s;
  cin >> s;
  int K;
  cin >> K;
  int N = s.size();
  priority_queue<string, vector<string>, greater<string>> pq;
  set<string> se;
  string temp;
  for (int i = 0; i < N; i++) {
    pq.push(s.substr(i, 1));
  }
  se.insert(pq.top());
  for (int i = 1; i < K; i++) {
    bool flag = false;
    temp = pq.top();
    for (int j = N - temp.size(); j < N; j++) {
      for (int k = 1; k <= N - j; k++) {
        if (pq.top() == s.substr(j, k)) {
          pq.pop();
          flag = true;
          break;
        }
      }
      if (flag) {
        break;
      }
    }
    if (se.count(pq.top())) {
      temp = pq.top();
      while (temp == pq.top()) {
        pq.pop();
        if (pq.empty()) {
          break;
        }
      }
      for (int j = 0; j < N - temp.size(); j++) {
        if (temp == s.substr(j, temp.size())) {
          pq.push(s.substr(j, temp.size() + 1));
        }
      }
    }
    se.insert(pq.top());
  }
  cout << *(next(se.begin(), K - 1)) << endl;
  return 0;
}