Problem p03629 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <cmath>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <vector>

// #define DEBUG 1

#define int long long
#define for0(i, n) for (int i = 0; i < n; i++)
#define iter(c) for (auto it = c.begin(); it != c.end(); it++)
#define vec(x) vector<x>
#define pb push_back
#define ms(a, z) memset(a, z, sizeof(a));
#define mp make_pair
#define X first
#define Y second
#define sqr(x) 1LL * (x) * (x)
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define all(a) a.begin(), a.end()
#define sz(x) (int)(x).size()
#ifdef int
#define read(x) scanf("%lld", &x);
#else
#define read(x) scanf("%d", &x);
#endif

#ifdef DEBUG
#define nl cout << "\n";
#define pr(x) cout << (x) << " ";
#define prl(x) cout << #x " = " << x << endl;
#define prp(x) cout << "(" << (x).first << " " << (x).second << ") ";
#define printv(v)                                                              \
  {                                                                            \
    for (int _ = 0; _ < sz(v); _++)                                            \
      cout << v[_] << " ";                                                     \
    cout << "\n";                                                              \
  }
#define printa(a, s)                                                           \
  {                                                                            \
    for (int _ = 0; _ < s; _++)                                                \
      cout << a[_] << " ";                                                     \
    cout << "\n";                                                              \
  }
#define print2D(a, m, n)                                                       \
  {                                                                            \
    for (int _ = 0; _ < m; _++) {                                              \
      for (int __ = 0; __ < n; __++)                                           \
        cout << a[_][__] << " ";                                               \
      cout << "\n";                                                            \
    }                                                                          \
    cout << "\n";                                                              \
  }
#define debug cout << "ok at line " << __LINE__ << endl;
#else
#define nl
#define pr(x)
#define prl(x)
#define prp(x)
#define printv(v)
#define printa(a, s)
#define print2D(a, m, n)
#define debug
#endif
#define ALPH_SIZE 3

using namespace std;

typedef long long ll;

const int INF = 2147483647;
const long long INFL = 9223372036854775807LL;
const double EPSILON = 0.00000001;
const long long MOD = 1000000007;

string s;
int freq[ALPH_SIZE];
bool processed[ALPH_SIZE];
bool is_option[10000][26];
vector<int> children[10000][26];
int child[10000][26];

bool check_zero() {
  for0(i, ALPH_SIZE) {
    if (freq[i] == 0)
      return true;
  }
  return false;
}

void process_segment(int seg, int i, int j) {
  ms(processed, false);
  for (int k = i; k < j; k++) {
    int c = s[k] - 'a';
    freq[c]--;
    if (not processed[c] and check_zero()) {
      is_option[seg][c] = true;
      for0(c2, ALPH_SIZE) {
        if (freq[c2] == 0)
          children[seg][c].pb(c2);
      }
    }
    processed[c] = true;
  }
}

string search(int num_segs) {
  ms(child, -1);
  for (int seg = num_segs - 2; seg >= 0; seg--) {
    for0(c, ALPH_SIZE) {
      if (is_option[seg][c]) {
        bool found = false;
        iter(children[seg][c]) {
          if (is_option[seg + 1][*it]) {
            found = true;
            child[seg][c] = *it;
            break;
          }
        }
        if (!found)
          is_option[seg][c] = false;
      }
    }
  }
  print2D(is_option, num_segs, ALPH_SIZE) print2D(child, num_segs, ALPH_SIZE)

      string ret = "";
  int cur;
  for0(c, ALPH_SIZE) {
    if (is_option[0][c]) {
      cur = c;
      break;
    }
  }
  for (int seg = 0; seg < num_segs; seg++) {
    ret += 'a' + cur;
    cur = child[seg][cur];
  }
  return ret;
}

int32_t main() {
#ifdef DEBUG
  freopen("C.txt", "r", stdin);
// freopen("","w",stdout);
#endif

  ms(is_option, false);
  cin >> s;
  int n = s.length();

  int i = 0;
  bool done = false;
  int seg = 0;

  while (i < n) {
    ms(freq, 0);
    int j = i;

    done = false;
    while (j < n and not done) {
      freq[s[j] - 'a']++;
      j++;
      done = not check_zero();
    }
    pr(i) pr(j) nl pr(s.substr(i, j - i)) nl

        if (done) {
      process_segment(seg, i, j);
    }
    else {
      for0(c, ALPH_SIZE) {
        if (freq[c] == 0)
          is_option[seg][c] = true;
      }
    }

    i = j;
    seg++;
  }

  if (done) {
    for0(c, ALPH_SIZE) is_option[seg][c] = true;
    seg++;
  }

  print2D(is_option, seg, ALPH_SIZE) string ans = search(seg);
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <cmath>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <vector>

// #define DEBUG 1

#define int long long
#define for0(i, n) for (int i = 0; i < n; i++)
#define iter(c) for (auto it = c.begin(); it != c.end(); it++)
#define vec(x) vector<x>
#define pb push_back
#define ms(a, z) memset(a, z, sizeof(a));
#define mp make_pair
#define X first
#define Y second
#define sqr(x) 1LL * (x) * (x)
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define all(a) a.begin(), a.end()
#define sz(x) (int)(x).size()
#ifdef int
#define read(x) scanf("%lld", &x);
#else
#define read(x) scanf("%d", &x);
#endif

#ifdef DEBUG
#define nl cout << "\n";
#define pr(x) cout << (x) << " ";
#define prl(x) cout << #x " = " << x << endl;
#define prp(x) cout << "(" << (x).first << " " << (x).second << ") ";
#define printv(v)                                                              \
  {                                                                            \
    for (int _ = 0; _ < sz(v); _++)                                            \
      cout << v[_] << " ";                                                     \
    cout << "\n";                                                              \
  }
#define printa(a, s)                                                           \
  {                                                                            \
    for (int _ = 0; _ < s; _++)                                                \
      cout << a[_] << " ";                                                     \
    cout << "\n";                                                              \
  }
#define print2D(a, m, n)                                                       \
  {                                                                            \
    for (int _ = 0; _ < m; _++) {                                              \
      for (int __ = 0; __ < n; __++)                                           \
        cout << a[_][__] << " ";                                               \
      cout << "\n";                                                            \
    }                                                                          \
    cout << "\n";                                                              \
  }
#define debug cout << "ok at line " << __LINE__ << endl;
#else
#define nl
#define pr(x)
#define prl(x)
#define prp(x)
#define printv(v)
#define printa(a, s)
#define print2D(a, m, n)
#define debug
#endif
#define ALPH_SIZE 26

using namespace std;

typedef long long ll;

const int INF = 2147483647;
const long long INFL = 9223372036854775807LL;
const double EPSILON = 0.00000001;
const long long MOD = 1000000007;

string s;
int freq[ALPH_SIZE];
bool processed[ALPH_SIZE];
bool is_option[10000][26];
vector<int> children[10000][26];
int child[10000][26];

bool check_zero() {
  for0(i, ALPH_SIZE) {
    if (freq[i] == 0)
      return true;
  }
  return false;
}

void process_segment(int seg, int i, int j) {
  ms(processed, false);
  for (int k = i; k < j; k++) {
    int c = s[k] - 'a';
    freq[c]--;
    if (not processed[c] and check_zero()) {
      is_option[seg][c] = true;
      for0(c2, ALPH_SIZE) {
        if (freq[c2] == 0)
          children[seg][c].pb(c2);
      }
    }
    processed[c] = true;
  }
}

string search(int num_segs) {
  ms(child, -1);
  for (int seg = num_segs - 2; seg >= 0; seg--) {
    for0(c, ALPH_SIZE) {
      if (is_option[seg][c]) {
        bool found = false;
        iter(children[seg][c]) {
          if (is_option[seg + 1][*it]) {
            found = true;
            child[seg][c] = *it;
            break;
          }
        }
        if (!found)
          is_option[seg][c] = false;
      }
    }
  }
  print2D(is_option, num_segs, ALPH_SIZE) print2D(child, num_segs, ALPH_SIZE)

      string ret = "";
  int cur;
  for0(c, ALPH_SIZE) {
    if (is_option[0][c]) {
      cur = c;
      break;
    }
  }
  for (int seg = 0; seg < num_segs; seg++) {
    ret += 'a' + cur;
    cur = child[seg][cur];
  }
  return ret;
}

int32_t main() {
#ifdef DEBUG
  freopen("C.txt", "r", stdin);
// freopen("","w",stdout);
#endif

  ms(is_option, false);
  cin >> s;
  int n = s.length();

  int i = 0;
  bool done = false;
  int seg = 0;

  while (i < n) {
    ms(freq, 0);
    int j = i;

    done = false;
    while (j < n and not done) {
      freq[s[j] - 'a']++;
      j++;
      done = not check_zero();
    }
    pr(i) pr(j) nl pr(s.substr(i, j - i)) nl

        if (done) {
      process_segment(seg, i, j);
    }
    else {
      for0(c, ALPH_SIZE) {
        if (freq[c] == 0)
          is_option[seg][c] = true;
      }
    }

    i = j;
    seg++;
  }

  if (done) {
    for0(c, ALPH_SIZE) is_option[seg][c] = true;
    seg++;
  }

  print2D(is_option, seg, ALPH_SIZE) string ans = search(seg);
  cout << ans << endl;
  return 0;
}