Problem p03629 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define e1 first
#define e2 second
#define pb push_back
#define mp make_pair
#define boost ios_base::sync_with_stdio(false)
#define eb emplace_back
#define OUT(x)                                                                 \
  {                                                                            \
    cout << x;                                                                 \
    exit(0);                                                                   \
  }
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define scanf(...) scanf(__VA_ARGS__) ?: 0
typedef long long int ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef pair<PLL, int> PLLI;
typedef pair<PLL, PLL> PP;
typedef pair<PII, int> PPI;
typedef pair<ll, int> PLI;
typedef unsigned int ui;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
const ll MOD = 1e9 + 696969;
const long long INF = 1e18 + 3;
#define maxn 300100
string s;
bool jest[1500];
int ile = 26, n;
int newbegin[1500], dp[maxn], wynLen;

inline bool tryMatch(int i, int skad, int lit) {
  while (skad < n && s[skad] != lit + 'a')
    ++skad;
  if (skad == n)
    return 1;
  int bedzie = dp[skad + 1] + 1;
  if (bedzie + i <= wynLen)
    return 1;
  return 0;
}

int main() {
  boost;
  cin >> s;
  n = s.length();
  string ret = "";
  for (int i = n - 1; i >= 0; --i) {
    dp[i] = dp[i + 1];
    if (!jest[s[i] - 'a'])
      --ile;
    jest[s[i] - 'a'] = 1;
    if (ile == 0) {
      dp[i]++;
      FOR(j, 0, 25) jest[j] = 0;
      ile = 26;
    }
  }

  wynLen = dp[0] + 1;
  // optymalna dlugosc wyniku to "wynLen"
  newbegin[0] = 0;

  string overallRet = "";
  for (int i = 0; i < wynLen; ++i) {
    for (int j = 0; j < 26; ++j)
      if (tryMatch(i + 1, newbegin[i], j)) {
        int sz = newbegin[i];
        while (sz < n && s[sz] != j + 'a')
          ++sz;
        newbegin[i + 1] = sz + 1;
        overallRet += (char)(j + 'a');
        break;
      }
  }

  cout << overallRet;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define e1 first
#define e2 second
#define pb push_back
#define mp make_pair
#define boost ios_base::sync_with_stdio(false)
#define eb emplace_back
#define OUT(x)                                                                 \
  {                                                                            \
    cout << x;                                                                 \
    exit(0);                                                                   \
  }
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define scanf(...) scanf(__VA_ARGS__) ?: 0
typedef long long int ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef pair<PLL, int> PLLI;
typedef pair<PLL, PLL> PP;
typedef pair<PII, int> PPI;
typedef pair<ll, int> PLI;
typedef unsigned int ui;
const int mod = 1e9 + 7;
const int inf = 1e9 + 9;
const ll MOD = 1e9 + 696969;
const long long INF = 1e18 + 3;
#define maxn 300100
string s;
bool jest[1500];
int ile = 26, n;
int newbegin[maxn], dp[maxn], wynLen;

inline bool tryMatch(int i, int skad, int lit) {
  while (skad < n && s[skad] != lit + 'a')
    ++skad;
  if (skad == n)
    return 1;
  int bedzie = dp[skad + 1] + 1;
  if (bedzie + i <= wynLen)
    return 1;
  return 0;
}

int main() {
  boost;
  cin >> s;
  n = s.length();
  string ret = "";
  for (int i = n - 1; i >= 0; --i) {
    dp[i] = dp[i + 1];
    if (!jest[s[i] - 'a'])
      --ile;
    jest[s[i] - 'a'] = 1;
    if (ile == 0) {
      dp[i]++;
      FOR(j, 0, 25) jest[j] = 0;
      ile = 26;
    }
  }

  wynLen = dp[0] + 1;
  // optymalna dlugosc wyniku to "wynLen"
  newbegin[0] = 0;

  string overallRet = "";
  for (int i = 0; i < wynLen; ++i) {
    for (int j = 0; j < 26; ++j)
      if (tryMatch(i + 1, newbegin[i], j)) {
        int sz = newbegin[i];
        while (sz < n && s[sz] != j + 'a')
          ++sz;
        newbegin[i + 1] = sz + 1;
        overallRet += (char)(j + 'a');
        break;
      }
  }

  cout << overallRet;
}