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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;
#define mod 1000000007
#define F first
#define S second
#define P pair<long long, long long>
#define all(a) a.begin(), a.end()
#define INF 1000000000000000000
std::vector<ll> uni(100010, -1);
ll root(ll a) {
  if (uni[a] < 0)
    return a;
  return uni[a] = root(uni[a]);
}
bool connect(ll a, ll b) {
  a = root(a);
  b = root(b);
  if (a == b)
    return false;
  if (uni[a] > uni[b])
    std::swap(a, b);
  uni[a] += uni[b];
  uni[b] = a;
  return true;
}

using namespace std;
int main(void) {
  ll n, m;
  cin >> n >> m;
  vector<ll> v(n);
  rep(i, n) {
    cin >> v[i];
    v[i]--;
  }
  ll x, y;
  rep(i, m) {
    cin >> x >> y;
    x--;
    y--;
    connect(x, y);
  }
  map<ll, vector<ll>> M;
  rep(i, n) { M[root(i)].push_back(i); }
  map<ll, vector<ll>>::iterator it = M.begin();
  ll ans = 0;
  while (it != M.end()) {
    vector<ll> V = (*it).S;
    vector<ll> C;
    rep(i, V.size()) C.push_back(v[V[i]]);
    rep(i, V.size()) rep(j, C.size()) {
      if (V[i] == C[j])
        ans++;
    }
    // rep(i,V.size())cout<<V[i]<<' ';
    // cout<<endl;
    // rep(i,C.size())cout<<C[i]<<' ';
    // cout<<endl<<endl;
    it++;
  }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;
#define mod 1000000007
#define F first
#define S second
#define P pair<long long, long long>
#define all(a) a.begin(), a.end()
#define INF 1000000000000000000
std::vector<ll> uni(100010, -1);
ll root(ll a) {
  if (uni[a] < 0)
    return a;
  return uni[a] = root(uni[a]);
}
bool connect(ll a, ll b) {
  a = root(a);
  b = root(b);
  if (a == b)
    return false;
  if (uni[a] > uni[b])
    std::swap(a, b);
  uni[a] += uni[b];
  uni[b] = a;
  return true;
}

using namespace std;
int main(void) {
  ll n, m;
  cin >> n >> m;
  vector<ll> v(n);
  rep(i, n) {
    cin >> v[i];
    v[i]--;
  }
  ll x, y;
  rep(i, m) {
    cin >> x >> y;
    x--;
    y--;
    connect(x, y);
  }
  map<ll, vector<ll>> M;
  rep(i, n) { M[root(i)].push_back(i); }
  map<ll, vector<ll>>::iterator it = M.begin();
  ll ans = 0;
  while (it != M.end()) {
    vector<ll> V = (*it).S;
    vector<ll> C;
    rep(i, V.size()) C.push_back(v[V[i]]);
    sort(all(V));
    sort(all(C));
    rep(i, V.size()) {
      if (binary_search(all(C), V[i]))
        ans++;
    }
    // rep(i,V.size())cout<<V[i]<<' ';
    // cout<<endl;
    // rep(i,C.size())cout<<C[i]<<' ';
    // cout<<endl<<endl;
    it++;
  }
  cout << ans;
}
Problem p03354 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
class UnionFind {
public:
  vector<ll> par; // 各元の親を表す配列
  vector<ll> siz; // 素集合のサイズを表す配列(1 で初期化)

  // Constructor
  UnionFind(ll sz_) : par(sz_), siz(sz_, 1) {
    for (ll i = 0; i < sz_; ++i)
      par[i] = i; // 初期では親は自分自身
  }
  void init(ll sz_) {
    par.resize(sz_);
    siz.assign(sz_, 1);
    for (ll i = 0; i < sz_; ++i)
      par[i] = i; // 初期では親は自分自身
  }

  // Member Function
  // Find
  ll root(ll x) { // 根の検索
    while (par[x] != x) {
      x = par[x] = par[par[x]]; // x の親の親を x の親とする
    }
    return x;
  }

  // Union(Unite, Merge)
  bool merge(ll x, ll y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    // merge technique（データ構造をマージするテク．小を大にくっつける）
    if (siz[x] < siz[y])
      swap(x, y);
    siz[x] += siz[y];
    par[y] = x;
    return true;
  }

  bool issame(ll x, ll y) { // 連結判定
    return root(x) == root(y);
  }

  ll size(ll x) { // 素集合のサイズ
    return siz[root(x)];
  }
};
#define MAXN 100005
int p[MAXN];
int main() {
  int n, m;
  cin >> n >> m;
  UnionFind tree(n);
  for (int i = 1; i <= n; i++) {
    cin >> p[i];
  }
  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;
    tree.merge(p[x], p[y]);
  }
  int count = 0;
  for (int i = 1; i <= n; i++) {
    if (tree.issame(i, p[i])) {
      count++;
    }
  }
  cout << count << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
class UnionFind {
public:
  vector<ll> par; // 各元の親を表す配列
  vector<ll> siz; // 素集合のサイズを表す配列(1 で初期化)

  // Constructor
  UnionFind(ll sz_) : par(sz_), siz(sz_, 1) {
    for (ll i = 0; i < sz_; ++i)
      par[i] = i; // 初期では親は自分自身
  }
  void init(ll sz_) {
    par.resize(sz_);
    siz.assign(sz_, 1);
    for (ll i = 0; i < sz_; ++i)
      par[i] = i; // 初期では親は自分自身
  }

  // Member Function
  // Find
  ll root(ll x) { // 根の検索
    while (par[x] != x) {
      x = par[x] = par[par[x]]; // x の親の親を x の親とする
    }
    return x;
  }

  // Union(Unite, Merge)
  bool merge(ll x, ll y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    // merge technique（データ構造をマージするテク．小を大にくっつける）
    if (siz[x] < siz[y])
      swap(x, y);
    siz[x] += siz[y];
    par[y] = x;
    return true;
  }

  bool issame(ll x, ll y) { // 連結判定
    return root(x) == root(y);
  }

  ll size(ll x) { // 素集合のサイズ
    return siz[root(x)];
  }
};
#define MAXN 100005
int p[MAXN];
int main() {
  int n, m;
  cin >> n >> m;
  UnionFind tree(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> p[i];
  }
  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;
    tree.merge(p[x], p[y]);
  }
  int count = 0;
  for (int i = 1; i <= n; i++) {
    if (tree.issame(i, p[i])) {
      count++;
    }
  }
  cout << count << endl;
  return 0;
}
Problem p03354 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int mod = 1e9 + 7;

struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) {}
  bool unionSet(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x])
        swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> pp(n);
  vector<int> p(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i];
    pp[i] = i + 1;
  }
  UnionFind uf(n);
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    uf.unionSet(x, y);
  }

  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (uf.root(p[i]) == uf.root(pp[i]))
      cnt++;
  }
  cout << cnt << endl;

  return 0;
}

//


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int mod = 1e9 + 7;

struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) {}
  bool unionSet(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x])
        swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool findSet(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> pp(n);
  vector<int> p(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i];
    pp[i] = i + 1;
  }
  UnionFind uf(n + 1);
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    uf.unionSet(x, y);
  }

  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (uf.root(p[i]) == uf.root(pp[i]))
      cnt++;
  }
  cout << cnt << endl;

  return 0;
}

//
Problem p03354 (C++)

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

int par[10005];
int high[10005];
// n要素で初期化
void init(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
    high[i] = 0;
  }
}
// 木の根を求める
int find(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}

// xとyの属する集合を併合
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;
  if (high[x] < high[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (high[x] == high[y])
      high[x]++;
  }
}
// xとyが同じ集合に属するか否か
bool same(int x, int y) { return find(x) == find(y); }
int main() {
  ll n, m;
  cin >> n >> m;
  init(n);
  int p[n];
  for (int i = 0; i < n; i++) {
    cin >> p[i];
    p[i]--;
  }
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    unite(p[x], p[y]);
  }
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (same(i, p[i])) {
      cnt++;
    }
  }
  cout << cnt << endl;
}

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

int par[100005];
int high[100005];
// n要素で初期化
void init(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
    high[i] = 0;
  }
}
// 木の根を求める
int find(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}

// xとyの属する集合を併合
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;
  if (high[x] < high[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (high[x] == high[y])
      high[x]++;
  }
}
// xとyが同じ集合に属するか否か
bool same(int x, int y) { return find(x) == find(y); }
int main() {
  ll n, m;
  cin >> n >> m;
  init(n);
  int p[n];
  for (int i = 0; i < n; i++) {
    cin >> p[i];
    p[i]--;
  }
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    unite(p[x], p[y]);
  }
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (same(i, p[i])) {
      cnt++;
    }
  }
  cout << cnt << endl;
}
Problem p03354 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define all(x) begin(x), end(x)
#define _ << ' ' <<
using namespace std;
using ll = long long;

int p[100000];

struct UF {
  vector<int> r, p;
  UF(int n) : r(n, 0), p(n, 0) { iota(p.begin(), p.end(), 0); }

  int f(int x) { return (p[x] == x ? x : p[x] = f(p[x])); }

  void u(int i, int j) {
    int x = f(i), y = f(j);
    if (x == y)
      return;

    if (r[x] >= r[y]) {
      p[y] = x;
      if (r[x] == r[y])
        r[x]++;
    } else
      p[x] = y;
  }

  bool s(int i, int j) { return f(i) == f(j); }
};

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; ++i)
    cin >> p[i], p[i]--;

  UF uf(n);
  for (int i = 0; i < m; ++i) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    uf.u(x, y);
  }
  vector<vector<int>> a(n), b(n);
  for (int i = 0; i < n; ++i) {
    a[uf.f(i)].push_back(i);
    b[uf.f(i)].push_back(p[i]);
  }
  int sol = 0;
  for (int i = 0; i < n; ++i) {
    sort(all(a[i]));
    sort(all(b[i]));
    vector<int> c(a.size());
    c.resize(set_intersection(all(a[i]), all(b[i]), c.begin()) - c.begin());
    sol += c.size();
  }
  cout << sol;
}


Fixed submission:
#include <bits/stdc++.h>
#define all(x) begin(x), end(x)
#define _ << ' ' <<
using namespace std;
using ll = long long;

int p[100000];

struct UF {
  vector<int> r, p;
  UF(int n) : r(n, 0), p(n, 0) { iota(p.begin(), p.end(), 0); }

  int f(int x) { return (p[x] == x ? x : p[x] = f(p[x])); }

  void u(int i, int j) {
    int x = f(i), y = f(j);
    if (x == y)
      return;

    if (r[x] >= r[y]) {
      p[y] = x;
      if (r[x] == r[y])
        r[x]++;
    } else
      p[x] = y;
  }

  bool s(int i, int j) { return f(i) == f(j); }
};

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; ++i)
    cin >> p[i], p[i]--;

  UF uf(n);
  for (int i = 0; i < m; ++i) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    uf.u(x, y);
  }
  vector<vector<int>> a(n), b(n);
  for (int i = 0; i < n; ++i) {
    a[uf.f(i)].push_back(i);
    b[uf.f(i)].push_back(p[i]);
  }
  int sol = 0;
  for (int i = 0; i < n; ++i) {
    sort(all(a[i]));
    sort(all(b[i]));
    vector<int> c(a[i].size());
    c.resize(set_intersection(all(a[i]), all(b[i]), c.begin()) - c.begin());
    sol += c.size();
  }
  cout << sol;
}
Problem p03355 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
string s;
int k;
vector<string> vs;
int main() {
  cin >> s >> k;
  for (int i = 1; i <= s.size() && i <= k; i++) {
    for (int j = 0; j < s.size() - i; j++) {
      string st = s.substr(j, i);
      vs.push_back(st);
    }
  }
  sort(vs.begin(), vs.end());
  int s = 1;
  string ans = vs[0];
  for (int i = 1; s < k; i++) {
    if (vs[i] != ans)
      s++, ans = vs[i];
  }
  cout << ans;
  return 0;
}

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using lli = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

string s;
lli k;

int main(void) {
  cin >> s >> k;
  lli n = s.size();
  priority_queue<string, vector<string>, greater<string>> p;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < n; j++) {
      if (j + i - 1 >= n)
        break;
      p.push(s.substr(j, i));
    }
  }
  unordered_map<string, lli> mp;
  while (k > 0) {
    string t = p.top();
    p.pop();
    if (mp[t])
      continue;
    else {
      k--;
      mp[t] = true;
    }
    if (k == 0) {
      cout << t << endl;
      break;
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using lli = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

string s;
lli k;

int main(void) {
  cin >> s >> k;
  lli n = s.size();
  priority_queue<string, vector<string>, greater<string>> p;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < n; j++) {
      if (j + i - 1 >= n)
        break;
      string t = s.substr(j, i);
      if (t.size() <= k)
        p.push(t);
    }
  }
  unordered_map<string, lli> mp;
  while (k > 0) {
    string t = p.top();
    p.pop();
    if (mp[t])
      continue;
    else {
      k--;
      mp[t] = true;
    }
    if (k == 0) {
      cout << t << endl;
      break;
    }
  }
  return 0;
}
Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
int main() {
  set<string> a;
  string s, t = "";
  int n;
  cin >> s >> n;
  for (int i = 0; i < s.length(); ++i) {
    t += s[i];
    a.insert(t);
    for (int j = i + 1; j < s.length(); ++j) {
      t += s[j];
      a.insert(t);
    }
    t = "";
  }
  auto ite = a.begin();
  for (int i = 1; i < n; ++i) {
    ++ite;
  }
  cout << *ite << "\n";
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
int main() {
  set<string> a;
  string s, t = "";
  int n;
  cin >> s >> n;
  for (int i = 0; i < s.length(); ++i) {
    t += s[i];
    a.insert(t);
    for (int j = 1; j <= n && i + j < s.length(); ++j) {
      t += s[i + j];
      a.insert(t);
    }
    t = "";
  }
  auto ite = a.begin();
  for (int i = 1; i < n; ++i) {
    ++ite;
  }
  cout << *ite << "\n";
}
Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

typedef long long ll;
// constexpr ll MOD = 998244353;
constexpr ll MOD = 1000000007;

// struct UnionFind {
//     vector<int> par;

//     UnionFind(int N): par(N) {
//         for (int i=0;i<N;i++) par[i] = i;
//     }

//     int root(int x) {
//         if (par[x] == x) return x;
//         else return par[x] = root(par[x]);
//     }

//     void unite(int x, int y) {
//         int rx = root(x);
//         int ry = root(y);
//         if (rx == ry) return;
//         par[rx] = ry;
//     }

//     bool same(int x, int y) {
//         int rx = root(x);
//         int ry = root(y);
//         return rx == ry;
//     }
// };

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

  // vector<set<string,greater<string>>> sub(N+1);
  // for (int i=0;i<N;i++) for (int j=1;j<=N-i;j++) {
  //     sub[j].insert(S.substr(i,j));
  // }
  // int j = N, v = 0;
  // for (j=N;j>=0 && v<K;j--) {
  //     v += sub[j].size();
  // }
  // auto itr = sub[j+1].begin();
  // for (int i=0;i<v-K-1;i++) itr++;
  // cout << *itr << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

typedef long long ll;
// constexpr ll MOD = 998244353;
constexpr ll MOD = 1000000007;

// struct UnionFind {
//     vector<int> par;

//     UnionFind(int N): par(N) {
//         for (int i=0;i<N;i++) par[i] = i;
//     }

//     int root(int x) {
//         if (par[x] == x) return x;
//         else return par[x] = root(par[x]);
//     }

//     void unite(int x, int y) {
//         int rx = root(x);
//         int ry = root(y);
//         if (rx == ry) return;
//         par[rx] = ry;
//     }

//     bool same(int x, int y) {
//         int rx = root(x);
//         int ry = root(y);
//         return rx == ry;
//     }
// };

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

  // vector<set<string,greater<string>>> sub(N+1);
  // for (int i=0;i<N;i++) for (int j=1;j<=N-i;j++) {
  //     sub[j].insert(S.substr(i,j));
  // }
  // int j = N, v = 0;
  // for (j=N;j>=0 && v<K;j--) {
  //     v += sub[j].size();
  // }
  // auto itr = sub[j+1].begin();
  // for (int i=0;i<v-K-1;i++) itr++;
  // cout << *itr << endl;
  return 0;
}
Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define UNIQUE(v) v.erase(unique(all(v)), v.end());
#define ZIP(v) sort(all(v)), UNIQUE(v)
#define repi(i, m, n) for (int i = m; i < n; i++)
#define drep(i, n, m) for (int i = n; i >= m; i--)
#define rep(i, n) repi(i, 0, n)
#define rrep(i, n) repi(i, 1, n + 1)
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define pb(x) push_back(x)
#define fi first
#define se second
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef vector<int> vi;
const int inf = 1e9 + 7;
const int INF = 1e18 + 7;
int mod = 1e9 + 7;

vector<string> s;
vector<string> sp;
map<string, int> mp;
signed main() {
  int k;
  string str;
  cin >> str;
  scanf("%lld", &k);
  int cnt = 0, n = str.size();
  rep(i, n) {
    rrep(j, n - i) {
      // cout << str.substr(i, j) << endl;
      string ss = str.substr(i, j);
      if (mp[ss])
        continue;
      mp[ss]++;
      s.pb(ss);
    }
  }
  int m = s.size();
  sort(all(s));
  cout << s[k - 1] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define UNIQUE(v) v.erase(unique(all(v)), v.end());
#define ZIP(v) sort(all(v)), UNIQUE(v)
#define repi(i, m, n) for (int i = m; i < n; i++)
#define drep(i, n, m) for (int i = n; i >= m; i--)
#define rep(i, n) repi(i, 0, n)
#define rrep(i, n) repi(i, 1, n + 1)
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define pb(x) push_back(x)
#define fi first
#define se second
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef vector<int> vi;
const int inf = 1e9 + 7;
const int INF = 1e18 + 7;
int mod = 1e9 + 7;

vector<string> s;
vector<string> sp;
map<string, int> mp;
signed main() {
  int k;
  string str;
  cin >> str;
  scanf("%lld", &k);
  int cnt = 0, n = str.size();
  rep(i, n) {
    int tmp = min(n - i, (int)10);
    rrep(j, tmp) {
      // cout << str.substr(i, j) << endl;
      string ss = str.substr(i, j);
      if (mp[ss])
        continue;
      mp[ss]++;
      s.pb(ss);
    }
  }
  int m = s.size();
  sort(all(s));
  cout << s[k - 1] << endl;
  return 0;
}
Problem p03355 (C++)

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

int main() {
  string s, sub;
  vector<string> sub_set;
  int k;
  cin >> s >> k;
  for (int i = 0; i < s.length(); i++) {
    for (int j = i; j - i + 1 <= k || j < s.length(); j++) {
      sub = s.substr(i, j - i + 1);
      sub_set.push_back(sub);
    }
  }
  sort(sub_set.begin(), sub_set.end());
  sub_set.erase(unique(sub_set.begin(), sub_set.end()), sub_set.end());
  cout << sub_set[k - 1] << endl;
  return 0;
}


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

int main() {
  string s, sub;
  vector<string> sub_set;
  int k;
  cin >> s >> k;
  for (int i = 0; i < s.length(); i++) {
    for (int j = i; j - i + 1 <= k && j < s.length(); j++) {
      sub = s.substr(i, j - i + 1);
      sub_set.push_back(sub);
    }
  }
  sort(sub_set.begin(), sub_set.end());
  sub_set.erase(unique(sub_set.begin(), sub_set.end()), sub_set.end());
  cout << sub_set[k - 1] << endl;
  return 0;
}
Problem p03355 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
const int MAX = 2e3 + 1;
int n, k, tot;
int num[26];
int pos[26][MAX];
string a;
string b[MAX << 3];
string Read() {
  string a = "";
  char ch = getchar();
  for (; ch > 'z' || ch < 'a'; ch = getchar())
    ;
  for (; ch >= 'a' && ch <= 'z'; a += ch, ch = getchar())
    ;
  return a;
}
int read() {
  int x = 0;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    ;
  for (; isdigit(ch); x = x * 10 + ch - 48, ch = getchar())
    ;
  return x;
}
int main() {
  a = Read(), n = a.length();
  k = read();
  // for(int i=0;i<n;++i)
  //   pos[a[i]-'a'][++num[a[i]-'a']]=i;
  for (int i = 0; i < n; ++i) {
    for (int j = 1; j <= k; ++j) {
      if (i + j - 1 >= n)
        break;
      if (j == 1)
        b[++tot] += a[i];
      else
        b[tot + 1] = b[tot] + a[i + j - 1], ++tot;
    }
  }
  sort(b + 1, b + 1 + tot);
  int L = unique(b + 1, b + 1 + tot) - b;
  cout << b[k];
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
const int MAX = 2e3 + 1;
int n, k, tot;
int num[26];
int pos[26][MAX];
string a;
string b[MAX << 7];
string Read() {
  string a = "";
  char ch = getchar();
  for (; ch > 'z' || ch < 'a'; ch = getchar())
    ;
  for (; ch >= 'a' && ch <= 'z'; a += ch, ch = getchar())
    ;
  return a;
}
int read() {
  int x = 0;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    ;
  for (; isdigit(ch); x = x * 10 + ch - 48, ch = getchar())
    ;
  return x;
}
int main() {
  a = Read(), n = a.length();
  k = read();
  // for(int i=0;i<n;++i)
  //   pos[a[i]-'a'][++num[a[i]-'a']]=i;
  for (int i = 0; i < n; ++i) {
    for (int j = 1; j <= k; ++j) {
      if (i + j - 1 >= n)
        break;
      if (j == 1)
        b[++tot] += a[i];
      else
        b[tot + 1] = b[tot] + a[i + j - 1], ++tot;
    }
  }
  sort(b + 1, b + 1 + tot);
  int L = unique(b + 1, b + 1 + tot) - b;
  cout << b[k];
  return 0;
}
Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
/**
 *   @Author : MeGaCrazy
 *   @InitTime : Thu Jul 18 03:14:33 2019
 *   @Idea :
 *
 *
 *
 *
 *
 *  Happy Solving :)
 **/
#include <bits/stdc++.h>
using namespace std;

// const int N=3e5+7;
// int l[N],r[N];
int main() {
#ifndef ONLINE_JUDGE
  // freopen("in","r",stdin);
  // freopen("out","w",stdout);
#endif
  vector<pair<char, int>> v;
  string s;
  int k;
  cin >> s >> k;
  set<string> st;
  for (int i = 0; i < s.size(); i++) {
    string cur = "";
    for (int j = i; j < s.size(); j++) {
      cur += s[j];
      st.insert(cur);
    }
  }
  int cnt = 0;
  string ans = "";
  for (auto it = st.begin(); it != st.end(); it++) {
    ans = *it;
    if (cnt + 1 == k)
      break;
    cnt++;
  }
  cout << ans << endl;
}

Fixed submission:
/**
 *   @Author : MeGaCrazy
 *   @InitTime : Thu Jul 18 03:14:33 2019
 *   @Idea :
 *
 *
 *
 *
 *
 *  Happy Solving :)
 **/
#include <bits/stdc++.h>
using namespace std;

// const int N=3e5+7;
// int l[N],r[N];
int main() {
#ifndef ONLINE_JUDGE
  // freopen("in","r",stdin);
  // freopen("out","w",stdout);
#endif
  vector<pair<char, int>> v;
  string s;
  int k;
  cin >> s >> k;
  set<string> st;
  for (int i = 0; i < s.size(); i++) {
    string cur = "";
    for (int j = i; j < s.size() && cur.size() < 5; j++) {
      cur += s[j];
      st.insert(cur);
    }
  }
  int cnt = 0;
  string ans = "";
  for (auto it = st.begin(); it != st.end(); it++) {
    ans = *it;
    if (cnt + 1 == k)
      break;
    cnt++;
  }
  cout << ans << endl;
}
Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

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

int main() {
  string s;
  cin >> s;
  int K;
  cin >> K;
  int N = s.size();
  vector<string> ans;

  for (int i = 0; i < N; i++) {
    for (int j = i; j < N; j++) {
      string ss = s.substr(i, j - i + 1);
      bool flag = true;
      for (int k = 0; k < ans.size(); k++) {
        if (ans[k] == ss)
          flag = false;
      }
      if (flag) {
        ans.push_back(ss);
        sort(ans.begin(), ans.end());
        if (ans.size() == K + 1) {
          ans.pop_back();
        }
      }
    }
  }
  cout << ans.back() << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

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

int main() {
  string s;
  cin >> s;
  int K;
  cin >> K;
  int N = s.size();
  vector<string> ans;

  for (int i = 0; i < N; i++) {
    for (int j = 1; j <= 5; j++) {
      string ss = s.substr(i, j);
      bool flag = true;
      for (int k = 0; k < ans.size(); k++) {
        if (ans[k] == ss)
          flag = false;
      }
      if (flag) {
        ans.push_back(ss);
        sort(ans.begin(), ans.end());
        if (ans.size() == K + 1) {
          ans.pop_back();
        }
      }
    }
  }
  cout << ans.back() << endl;
  return 0;
}
Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  string str;
  int k;
  cin >> str >> k;
  set<string> s;
  int sz = str.size();
  for (int i = 0; i < sz; i++) {
    string temp;
    for (int j = i; j < sz; j++) {
      temp += str[j];
      s.insert(temp);
    }
  }
  auto it = s.begin();
  // cout << s.size() << "\n";
  for (int i = 1; i < k; i++) {
    // cout << *it << "\n";
    it++;
  }
  cout << *it << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  string str;
  int k;
  cin >> str >> k;
  set<string> s;
  int sz = str.size();
  for (int i = 0; i < sz; i++) {
    string temp;
    for (int j = i; j < min(sz, i + k); j++) {
      temp += str[j];
      s.insert(temp);
    }
  }
  auto it = s.begin();
  // cout << s.size() << "\n";
  for (int i = 1; i < k; i++) {
    // cout << *it << "\n";
    it++;
  }
  cout << *it << "\n";
  return 0;
}
Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define IFOR(i, m, n) for (ll i = n - 1; i >= m; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define FOREACH(x, a) for (auto &(x) : (a))
#define ALL(v) (v).begin(), (v).end()
#define SZ(x) ll(x.size())

int main() {
  string s;
  ll k;
  cin >> s >> k;
  set<string> st;
  ll n = SZ(s);
  REP(i, n) FOR(j, 1, n + 1) if (i + j - 1 < n) { st.insert(s.substr(i, j)); }
  // DUMP(SZ(st));
  auto it = next(st.begin(), k - 1);
  cout << *it << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define IFOR(i, m, n) for (ll i = n - 1; i >= m; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define FOREACH(x, a) for (auto &(x) : (a))
#define ALL(v) (v).begin(), (v).end()
#define SZ(x) ll(x.size())

int main() {
  string s;
  ll k;
  cin >> s >> k;
  set<string> st;
  ll n = SZ(s);
  if (n <= 100) {
    REP(i, n) FOR(j, 1, n + 1) if (i + j - 1 < n) { st.insert(s.substr(i, j)); }
  } else {
    REP(i, n) FOR(j, 1, k + 1) if (i + j - 1 < n) { st.insert(s.substr(i, j)); }
  }
  // DUMP(SZ(st));
  auto it = next(st.begin(), k - 1);
  cout << *it << endl;
}
Problem p03355 (C++)

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

using namespace std;

const int MAX_N = 5000;

int n, k;
string str;

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

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

using namespace std;

const int MAX_N = 5000;

int n, k;
string str;

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

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

string s;
int n, k;

int main() {
  cin >> s >> k;
  n = s.length();
  vector<string> v;
  for (int i = 0; i < n; ++i)
    for (int j = 0; i + j - 1 < n; ++j)
      v.push_back(s.substr(i, j + 1));
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  cout << v[k - 1] << '\n';
  return 0;
}

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

string s;
int n, k;

int main() {
  cin >> s >> k;
  n = s.length();
  vector<string> v;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j + 1 <= 5 && i + j - 1 < n; ++j)
      v.push_back(s.substr(i, j + 1));
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  cout << v[k - 1] << '\n';
  return 0;
}
Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

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

  vector<string> subs;
  map<string, bool> memo;

  rep(start, s.size()) {
    string si = s.substr(start, 1);
    if (subs.size() == k + 1 and si > subs[k])
      continue;
    rep(li, s.size() - start) {
      int l = li + 1;
      string si = s.substr(start, l);
      auto it = lower_bound(subs.begin(), subs.end(), si);
      if (memo.count(si))
        continue;
      // cout << si << endl;

      subs.insert(it, si);
      if (subs.size() > k)
        subs.resize(k + 1);
      memo[si] = true;
    }
  }
  cout << subs[k] << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

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

  vector<string> subs;
  map<string, bool> memo;

  rep(start, s.size()) {
    string si = s.substr(start, 1);
    if (subs.size() == k + 1 and si > subs[k])
      continue;
    rep(li, s.size() - start) {
      int l = li + 1;
      string si = s.substr(start, l);
      if (subs.size() == k + 1 and si > subs[k])
        break;
      auto it = lower_bound(subs.begin(), subs.end(), si);
      if (memo.count(si))
        continue;
      // cout << si << endl;

      subs.insert(it, si);
      if (subs.size() > k)
        subs.resize(k + 1);
      memo[si] = true;
    }
  }
  cout << subs[k] << endl;
}
Problem p03355 (C++)

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

using namespace std;

#define rep2(x, fr, to) for (int(x) = (fr); (x) < (to); (x)++)
#define rep(x, to) for (int(x) = 0; (x) < (to); (x)++)
#define repr(x, fr, to) for (int(x) = (fr); (x) >= (to); (x)--)
#define all(c) (c).begin(), (c).end()
#define sz(v) (int)(v).size()

typedef int64_t ll;
typedef vector<int> VI;
typedef pair<ll, ll> pii;
const int MD = 1e9 + 7;
typedef vector<ll> VL;

void dbg() { cerr << "\n"; }
template <typename T, typename... T2> void dbg(const T &fst, const T2 &...rst) {
  cerr << fst << ": ";
  dbg(rst...);
}

int main() {

  // cin.tie(0); ios_base::sync_with_stdio(false);
  string s;
  int k;
  cin >> s >> k;

  set<string> st;
  int n = sz(s);
  rep(i, n) {
    rep2(j, 1, n - i + 1) st.insert(s.substr(i, j));
    while (sz(st) > k + 1)
      st.erase(next(st.begin(), k + 1));
  }
  // for(auto x :st) cout<<x <<" "; puts("");

  cout << *next(st.begin(), k - 1) << "\n";
  return 0;
}


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

using namespace std;

#define rep2(x, fr, to) for (int(x) = (fr); (x) < (to); (x)++)
#define rep(x, to) for (int(x) = 0; (x) < (to); (x)++)
#define repr(x, fr, to) for (int(x) = (fr); (x) >= (to); (x)--)
#define all(c) (c).begin(), (c).end()
#define sz(v) (int)(v).size()

typedef int64_t ll;
typedef vector<int> VI;
typedef pair<ll, ll> pii;
const int MD = 1e9 + 7;
typedef vector<ll> VL;

void dbg() { cerr << "\n"; }
template <typename T, typename... T2> void dbg(const T &fst, const T2 &...rst) {
  cerr << fst << ": ";
  dbg(rst...);
}

int main() {

  // cin.tie(0); ios_base::sync_with_stdio(false);
  string s;
  int k;
  cin >> s >> k;

  set<string> st;
  int n = sz(s);
  rep(i, n) {
    rep2(j, 1, n - i + 1) {
      string cr = s.substr(i, j);
      if (sz(st) > k && cr >= *next(st.begin(), k))
        break;
      st.insert(cr);
    }
    while (sz(st) > k + 1)
      st.erase(next(st.begin(), k + 1));
  }
  // for(auto x :st) cout<<x <<" "; puts("");

  cout << *next(st.begin(), k - 1) << "\n";
  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)
#define ALL(c) (c).begin(), (c).end()
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
using namespace std;

using ll = long long;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const int INF = 1e9;
const long long INFL = 1LL << 60;

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

  string s;
  cin >> s;
  int k;
  cin >> k;

  vector<vector<int>> pos(26, vector<int>());
  rep(i, s.size()) { pos[s[i] - 'a'].push_back(i); }

  set<string> ss;
  rep(c, 26) {
    for (int &i : pos[c]) {
      for (int j = 1; i + j <= s.size(); j++) {
        ss.insert(s.substr(i, j));
      }
    }
    if (ss.size() >= k)
      break;
  }

  auto itr = ss.begin();
  rep(i, k - 1) itr++;

  cout << *itr << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define ALL(c) (c).begin(), (c).end()
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
using namespace std;

using ll = long long;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const int INF = 1e9;
const long long INFL = 1LL << 60;

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

  string s;
  cin >> s;
  int k;
  cin >> k;

  vector<vector<int>> pos(26, vector<int>());
  rep(i, s.size()) { pos[s[i] - 'a'].push_back(i); }

  set<string> ss;
  rep(c, 26) {
    for (int &i : pos[c]) {
      for (int j = 1; i + j <= s.size(); j++) {
        ss.insert(s.substr(i, j));
        if (j >= k)
          break;
      }
    }
    if (ss.size() >= k)
      break;
  }

  auto itr = ss.begin();
  rep(i, k - 1) itr++;

  cout << *itr << endl;

  return 0;
}
Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
// iostream is too mainstream
#include <cstdio>
// bitch please
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <time.h>
#include <vector>
#define dibs reserve
#define OVER9000 1234567890
#define ALL_THE(CAKE, LIE)                                                     \
  for (auto LIE = CAKE.begin(); LIE != CAKE.end(); LIE++)
#define tisic 47
#define soclose 1e-8
#define chocolate win
// so much chocolate
#define patkan 9
#define ff first
#define ss second
#define abs(x) ((x < 0) ? -(x) : x)
#define uint unsigned int
#define dbl long double
#define pi 3.14159265358979323846
using namespace std;
// mylittledoge

typedef long long cat;

#ifdef DONLINE_JUDGE
// palindromic tree is better than splay tree!
#define lld I64d
#endif

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(10);
  string S;
  int N, K;
  cin >> S >> K;
  N = S.length();

  vector<string> V;
  for (int i = 0; i < N; i++)
    for (int j = 1; j <= N - i; j++)
      V.push_back(S.substr(i, j));
  sort(begin(V), end(V));
  for (int i = 0; i < (int)V.size(); i++)
    if (i == 0 || V[i] != V[i - 1]) {
      K--;
      if (K == 0) {
        cout << V[i] << "\n";
        return 0;
      }
    }
  return 0;
}

// look at my code
// my code is amazing


Fixed submission:
#include <bits/stdc++.h>
// iostream is too mainstream
#include <cstdio>
// bitch please
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <time.h>
#include <vector>
#define dibs reserve
#define OVER9000 1234567890
#define ALL_THE(CAKE, LIE)                                                     \
  for (auto LIE = CAKE.begin(); LIE != CAKE.end(); LIE++)
#define tisic 47
#define soclose 1e-8
#define chocolate win
// so much chocolate
#define patkan 9
#define ff first
#define ss second
#define abs(x) ((x < 0) ? -(x) : x)
#define uint unsigned int
#define dbl long double
#define pi 3.14159265358979323846
using namespace std;
// mylittledoge

typedef long long cat;

#ifdef DONLINE_JUDGE
// palindromic tree is better than splay tree!
#define lld I64d
#endif

int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(10);
  string S;
  int N, K;
  cin >> S >> K;
  N = S.length();

  vector<string> V;
  for (int i = 0; i < N; i++)
    for (int j = 1; j <= min(K, N - i); j++)
      V.push_back(S.substr(i, j));
  sort(begin(V), end(V));
  for (int i = 0; i < (int)V.size(); i++)
    if (i == 0 || V[i] != V[i - 1]) {
      K--;
      if (K == 0) {
        cout << V[i] << "\n";
        return 0;
      }
    }
  return 0;
}

// look at my code
// my code is amazing
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> vec;

  for (int i = 0; i < s.size(); i++)
    for (int j = 1; j < K; j++)
      vec.push_back(s.substr(i, j));

  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;

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

  vector<string> vec;

  for (int i = 0; i < s.size(); i++)
    for (int j = 1; j < K + 1; j++)
      vec.push_back(s.substr(i, j));

  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: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <tuple>
#include <vector>
std::string S;
std::vector<std::string> A;
int K;
int main() {
  std::cin >> S >> K;
  for (int i = 0; i < S.size(); i++) {
    for (int j = 1; j <= S.size() - i; j++) {
      A.emplace_back(S.substr(i, j));
    }
  }
  std::sort(A.begin(), A.end());
  A.erase(std::unique(A.begin(), A.end()), A.end());
  std::cout << A[K - 1] << std::endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <tuple>
#include <vector>
std::string S;
std::vector<std::string> A;
int K;
int main() {
  std::cin >> S >> K;
  for (int i = 0; i < S.size(); i++) {
    for (int j = 1; j <= K; j++) {
      A.emplace_back(S.substr(i, j));
    }
  }
  std::sort(A.begin(), A.end());
  A.erase(std::unique(A.begin(), A.end()), A.end());
  std::cout << A[K - 1] << std::endl;
  return 0;
}
Problem p03355 (C++)

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

using namespace std;

using ll = long long;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s;
  cin >> s;
  int k;
  cin >> k;
  int n = s.length();
  vector<string> v;
  for (int i = 0; i < n; i++) {
    string tmp = "";
    for (int j = i; j < n; j++) {
      tmp += s[j];
      v.push_back(tmp);
    }
  }

  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;

using ll = long long;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s;
  cin >> s;
  int k;
  cin >> k;
  int n = s.length();
  vector<string> v;
  for (int i = 0; i < n; i++) {
    string tmp = "";
    for (int j = i; j < min(n, i + 5); j++) {
      tmp += s[j];
      v.push_back(tmp);
    }
  }

  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 <iostream>
#include <string>
#include <vector>

int main(int argc, char *argv[]) {
  std::string s;
  int n;
  std::getline(std::cin, s);
  std::cin >> n;
  std::vector<std::string> ss;

  for (std::size_t i = 0; i < s.length(); i++) {
    for (std::size_t len = 1; len <= s.length(); len++) {
      if (len + i > s.length()) {
        break;
      }
      std::string a = s.substr(i, len);
      if (ss.size() == 0) {
        ss.push_back(a);
      } else if (ss.size() >= 5) {
        if (ss[5] <= a) {
          break;
        }
      }
      for (size_t order = 0; order < 5; order++) {
        if (ss[order] > a) {
          ss.insert(ss.begin() + order, a);
          break;
        } else if (ss[order] == a) {
          break;
        } else if (order == ss.size() - 1) {
          ss.push_back(a);
          break;
        }
      }
    }
  }

  std::cout << ss[n - 1] << std::endl;

  return 0;
}

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

int main(int argc, char *argv[]) {
  std::string s;
  int n;
  std::getline(std::cin, s);
  std::cin >> n;
  std::vector<std::string> ss;

  for (std::size_t i = 0; i < s.length(); i++) {
    for (std::size_t len = 1; len <= s.length(); len++) {
      if (len + i > s.length()) {
        break;
      }
      std::string a = s.substr(i, len);
      if (ss.size() == 0) {
        ss.push_back(a);
      } else if (ss.size() >= 5) {
        if (ss[4] <= a) {
          break;
        }
      }
      for (size_t order = 0; order < 5; order++) {
        if (ss[order] > a) {
          ss.insert(ss.begin() + order, a);
          break;
        } else if (ss[order] == a) {
          break;
        } else if (order == ss.size() - 1) {
          ss.push_back(a);
          break;
        }
      }
    }
  }

  std::cout << ss[n - 1] << std::endl;

  return 0;
}
Problem p03355 (C++)

Buggy submission (status: Runtime Error):
#include "algorithm"
#include "iostream"
#include "map"
#include "vector"

using namespace std;

string s;
int K;
vector<string> sub;

int main() {

  cin >> s >> K;

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

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

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

Fixed submission:
#include "algorithm"
#include "iostream"
#include "map"
#include "vector"

using namespace std;

string s;
int K;
vector<string> sub;

int main() {

  cin >> s >> K;

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

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

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