Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define endl "\n"
#define ull unsigned long long
#define ALL(v) ((v).begin()), ((v).end())
#define INCREASING(v) sort(ALL(v))
#define DECREASING(v) INCREASING(v), reverse(ALL(v))
#define SZ(v) ((int)((v).size()))
#define LP(c, x, n) for (int c = x; c < n; c++)
#define LO(c, n, x) for (int c = n; c >= x; c--)
#define watch(x) cout << (#x) << " is " << (x) << endl
#define PB push_back
#define F first
#define S second
#define MP make_pair
#define PI (2 * acos(0.0))
#define EPS 1e-8
#define INF 1.0 / 0.0
#define Yala_Besor3a                                                           \
  ios_base::sync_with_stdio(0), ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define ReadFile freopen("delivery.in", "r", stdin)
#define WriteFile freopen("output.txt", "w", stdout)
using namespace std;
const int OO = 0x3f3f3f3f, NegOO = -1 * OO, N = 1e3 + 5, mod = 998244353;
int dp[N][N];
string x, z;
int szx, szz;
int solve(int i, int j) {
  if (i == szx || j == szz)
    return 0;
  int &rf = dp[i][j];
  if (~rf)
    return rf;
  if (x[i] == z[j])
    rf = max(1 + solve(i + 1, j + 1), rf);
  else
    rf = max(rf, max(solve(i + 1, j), solve(i, j + 1)));
  return rf;
}
void Build(int i, int j) {
  if (i == szx || j == szz)
    return;
  if (x[i] == z[j]) {
    cout << x[i];
    Build(i + 1, j + 1);
  } else {
    int ch1 = solve(i + 1, j);
    int ch2 = solve(i, j + 1);
    if (ch1 >= ch2)
      Build(i + 1, j);
    else
      Build(i, j + 1);
  }
  return;
}
int main() {
  cin >> x >> z;
  szx = SZ(x), szz = SZ(z);
  memset(dp, -1, sizeof(dp));
  solve(0, 0);
  Build(0, 0);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define endl "\n"
#define ull unsigned long long
#define ALL(v) ((v).begin()), ((v).end())
#define INCREASING(v) sort(ALL(v))
#define DECREASING(v) INCREASING(v), reverse(ALL(v))
#define SZ(v) ((int)((v).size()))
#define LP(c, x, n) for (int c = x; c < n; c++)
#define LO(c, n, x) for (int c = n; c >= x; c--)
#define watch(x) cout << (#x) << " is " << (x) << endl
#define PB push_back
#define F first
#define S second
#define MP make_pair
#define PI (2 * acos(0.0))
#define EPS 1e-8
#define INF 1.0 / 0.0
#define Yala_Besor3a                                                           \
  ios_base::sync_with_stdio(0), ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define ReadFile freopen("delivery.in", "r", stdin)
#define WriteFile freopen("output.txt", "w", stdout)
using namespace std;
const int OO = 0x3f3f3f3f, NegOO = -1 * OO, N = 3e3 + 5, mod = 998244353;
int dp[N][N];
string x, z;
int szx, szz;
int solve(int i, int j) {
  if (i == szx || j == szz)
    return 0;
  int &rf = dp[i][j];
  if (~rf)
    return rf;
  if (x[i] == z[j])
    rf = max(1 + solve(i + 1, j + 1), rf);
  else
    rf = max(rf, max(solve(i + 1, j), solve(i, j + 1)));
  return rf;
}
void Build(int i, int j) {
  if (i == szx || j == szz)
    return;
  if (x[i] == z[j]) {
    cout << x[i];
    Build(i + 1, j + 1);
  } else {
    int ch1 = solve(i + 1, j);
    int ch2 = solve(i, j + 1);
    if (ch1 >= ch2)
      Build(i + 1, j);
    else
      Build(i, j + 1);
  }
  return;
}
int main() {
  cin >> x >> z;
  szx = SZ(x), szz = SZ(z);
  memset(dp, -1, sizeof(dp));
  solve(0, 0);
  Build(0, 0);
  return 0;
}