Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
// T+N
// #pragma GCC optimize ("O3")
// #pragma GCC target ("sse4")
#define endl "\n"
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
template <class T, class T2> inline bool chkmax(T &x, const T2 &y) {
  return x < y ? x = y, 1 : 0;
}
template <class T, class T2> inline bool chkmin(T &x, const T2 &y) {
  return x > y ? x = y, 1 : 0;
}
const ll mod = 1e9 + 7;
template <class T> inline void fix(T &x) {
  if (x >= mod | x <= -mod) {
    x %= mod;
  }
  if (x < 0) {
    x += mod;
  }
}
#define out(x) cout << __LINE__ << ": " << (#x) << " = " << (x) << endl

const int MAX_N = 1e3 + 10;
ll ans[MAX_N][MAX_N], hist[MAX_N][MAX_N];
string a, b;

void output(int i, int j) {
  if (i == 0 && j == 0) {
    return;
  }
  if (hist[i][j] == 2) {
    output(i - 1, j - 1);
    cout << a[i - 1];
  } else if (hist[i][j] == 1) {
    output(i - 1, j);
  } else if (hist[i][j] == -1) {
    output(i, j - 1);
  }
}

signed main() {
  // ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
  cin >> a >> b;
  ll ret = 0;
  for (int i = 0; i < MAX_N; i++) {
    fill_n(ans[i], MAX_N, -mod);
  }
  ans[0][0] = 0;
  for (int i = 0; i <= a.size(); i++) {
    for (int j = 0; j <= b.size(); j++) {
      if (a[i] == b[j]) {
        if (chkmax(ans[i + 1][j + 1], ans[i][j] + 1)) {
          hist[i + 1][j + 1] = 2;
        }
      }
      if (chkmax(ans[i + 1][j], ans[i][j])) {
        hist[i + 1][j] = 1;
      }
      if (chkmax(ans[i][j + 1], ans[i][j])) {
        hist[i][j + 1] = -1;
      }
    }
  }
  output(a.size(), b.size());
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
// T+N
// #pragma GCC optimize ("O3")
// #pragma GCC target ("sse4")
#define endl "\n"
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
template <class T, class T2> inline bool chkmax(T &x, const T2 &y) {
  return x < y ? x = y, 1 : 0;
}
template <class T, class T2> inline bool chkmin(T &x, const T2 &y) {
  return x > y ? x = y, 1 : 0;
}
const ll mod = 1e9 + 7;
template <class T> inline void fix(T &x) {
  if (x >= mod | x <= -mod) {
    x %= mod;
  }
  if (x < 0) {
    x += mod;
  }
}
#define out(x) cout << __LINE__ << ": " << (#x) << " = " << (x) << endl

const int MAX_N = 4e3 + 10;
ll ans[MAX_N][MAX_N], hist[MAX_N][MAX_N];
string a, b;

void output(int i, int j) {
  if (i == 0 && j == 0) {
    return;
  }
  if (hist[i][j] == 2) {
    output(i - 1, j - 1);
    cout << a[i - 1];
  } else if (hist[i][j] == 1) {
    output(i - 1, j);
  } else if (hist[i][j] == -1) {
    output(i, j - 1);
  }
}

signed main() {
  // ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
  cin >> a >> b;
  ll ret = 0;
  for (int i = 0; i < MAX_N; i++) {
    fill_n(ans[i], MAX_N, -mod);
  }
  ans[0][0] = 0;
  for (int i = 0; i <= a.size(); i++) {
    for (int j = 0; j <= b.size(); j++) {
      if (a[i] == b[j]) {
        if (chkmax(ans[i + 1][j + 1], ans[i][j] + 1)) {
          hist[i + 1][j + 1] = 2;
        }
      }
      if (chkmax(ans[i + 1][j], ans[i][j])) {
        hist[i + 1][j] = 1;
      }
      if (chkmax(ans[i][j + 1], ans[i][j])) {
        hist[i][j + 1] = -1;
      }
    }
  }
  output(a.size(), b.size());
  return 0;
}