Problem p03355 (C++)

Buggy submission (status: Time Limit Exceeded):
// Nobody_hacks_me
// by lzt
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <cwchar>
#include <cwctype>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <math.h>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <stdio.h>
#include <stdlib.h>
#include <streambuf>
#include <string.h>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;

#define file(x) freopen(x ".in", "r", stdin), freopen(x ".out", "w", stdout)
#define pb push_back
#define mp make_pair
#define x first
#define y second
#define fuck cerr << "fuck" << endl
#define debug(x) cerr << #x << "=" << x << endl
#define lc (i << 1)
#define rc (i << 1 | 1)
#define clr(x) memset(x, 0, sizeof(x))
#define file(x) freopen(x ".in", "r", stdin), freopen(x ".out", "w", stdout)
#define SZ(x) ((int)x.size())
#define geti(x) x = read()
#define getii(x, y) geti(x), geti(y)
#define getiii(x, y, z) getii(x, y), geti(z)
#define puti(x) print(x), putchar(' ')
#define putii(x, y) puti(x), puti(y)
#define putiii(x, y, z) putii(x, y), puti(z)
#define putsi(x) print(x), putchar('\n')
#define putsii(x, y) puti(x), putsi(y)
#define putsiii(x, y, z) putii(x, y), putsi(z)
#define rep(x, a, b) for (register int x = (int)a; x <= (int)b; x++)
#define rrep(x, a, b) for (register int x = (int)a; x >= (int)b; x--)

ll read() {
  ll x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}

template <class T> inline void print(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  register short a[20] = {}, sz = 0;
  while (x)
    a[sz++] = x % 10, x /= 10;
  if (sz == 0)
    putchar('0');
  for (int i = sz - 1; i >= 0; i--)
    putchar('0' + a[i]);
}

const ll inf = 2147483647;
const ll llinf = 2e18;
const int mod = 998244353;
const ull MAXMOD = (ull)mod * (mod - 1);

//------------head-------------

// Problem No.

string s;
int k;
string a, b, c, d, e;

int main() {
#ifdef LZT
  freopen("in", "r", stdin);
#endif
  getline(cin, s);
  geti(k);
  for (int i = 0; i < s.size(); i++) {
    string nw = "";
    for (int j = i; j < s.size(); j++) {
      nw = nw + s[j];
      if (a == nw || b == nw || c == nw || d == nw || e == nw)
        continue;
      if (a == "" || a > nw) {
        e = d, d = c, c = b, b = a, a = nw;
      } else if (b == "" || b > nw) {
        e = d, d = c, c = b, b = nw;
      } else if (c == "" || c > nw) {
        e = d, d = c, c = nw;
      } else if (d == "" || d > nw) {
        e = d, d = nw;
      } else if (e == "" || e > nw) {
        e = nw;
      }
    }
  }
  if (k == 1)
    cout << a << endl;
  else if (k == 2)
    cout << b << endl;
  else if (k == 3)
    cout << c << endl;
  else if (k == 4)
    cout << d << endl;
  else
    cout << e << endl;
  return 0;
}

Fixed submission:
// Nobody_hacks_me
// by lzt
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <cwchar>
#include <cwctype>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <math.h>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <stdio.h>
#include <stdlib.h>
#include <streambuf>
#include <string.h>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;

#define file(x) freopen(x ".in", "r", stdin), freopen(x ".out", "w", stdout)
#define pb push_back
#define mp make_pair
#define x first
#define y second
#define fuck cerr << "fuck" << endl
#define debug(x) cerr << #x << "=" << x << endl
#define lc (i << 1)
#define rc (i << 1 | 1)
#define clr(x) memset(x, 0, sizeof(x))
#define file(x) freopen(x ".in", "r", stdin), freopen(x ".out", "w", stdout)
#define SZ(x) ((int)x.size())
#define geti(x) x = read()
#define getii(x, y) geti(x), geti(y)
#define getiii(x, y, z) getii(x, y), geti(z)
#define puti(x) print(x), putchar(' ')
#define putii(x, y) puti(x), puti(y)
#define putiii(x, y, z) putii(x, y), puti(z)
#define putsi(x) print(x), putchar('\n')
#define putsii(x, y) puti(x), putsi(y)
#define putsiii(x, y, z) putii(x, y), putsi(z)
#define rep(x, a, b) for (register int x = (int)a; x <= (int)b; x++)
#define rrep(x, a, b) for (register int x = (int)a; x >= (int)b; x--)

ll read() {
  ll x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}

template <class T> inline void print(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  register short a[20] = {}, sz = 0;
  while (x)
    a[sz++] = x % 10, x /= 10;
  if (sz == 0)
    putchar('0');
  for (int i = sz - 1; i >= 0; i--)
    putchar('0' + a[i]);
}

const ll inf = 2147483647;
const ll llinf = 2e18;
const int mod = 998244353;
const ull MAXMOD = (ull)mod * (mod - 1);

//------------head-------------

// Problem No.

string s;
int k;
string a, b, c, d, e;

int main() {
#ifdef LZT
  freopen("in", "r", stdin);
#endif
  getline(cin, s);
  geti(k);
  for (int i = 0; i < s.size(); i++) {
    string nw = "";
    for (int j = i; j < min(i + 5, (int)s.size()); j++) {
      nw = nw + s[j];
      if (a == nw || b == nw || c == nw || d == nw || e == nw)
        continue;
      if (a == "" || a > nw) {
        e = d, d = c, c = b, b = a, a = nw;
      } else if (b == "" || b > nw) {
        e = d, d = c, c = b, b = nw;
      } else if (c == "" || c > nw) {
        e = d, d = c, c = nw;
      } else if (d == "" || d > nw) {
        e = d, d = nw;
      } else if (e == "" || e > nw) {
        e = nw;
      }
    }
  }
  if (k == 1)
    cout << a << endl;
  else if (k == 2)
    cout << b << endl;
  else if (k == 3)
    cout << c << endl;
  else if (k == 4)
    cout << d << endl;
  else
    cout << e << endl;
  return 0;
}