Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define loop(n, i, a) for (ll i = a; i < n; i++)
#define loopR(n, i, a) for (ll i = n - 1; i >= a; i--)
#define all(arr, n) arr, arr + n
#define allv(v) (v).begin(), (v).end()
#define rallv(v) (v).rbegin(), (v).rend()
#define m_p make_pair
#define ll long long
#define pii pair<ll, ll>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<pair<ll, ll>>
#define sz(x) (int)x.size()
#define pb push_back
#define endl "\n"
#define Endl "\n"
#define f first
#define s second
#define mem(dp, n) memset(dp, n, sizeof dp)
#define test(i) cout << i << endl
int dx[] = {-1, 1, 0, 0, -1, -1, 1, 1};
int dy[] = {0, 0, 1, -1, -1, 1, -1, 1};
void fast() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
}
const int mxN = 2e3 + 5, oo = 0x3f3f3f3f, mod = 1e9 + 7;
string s, t;
int dp[mxN][mxN];
int go(int i, int j) {
  if (i == sz(s) || j == sz(t))
    return 0;
  int &ret = dp[i][j];
  if (~ret)
    return ret;
  if (s[i] == t[j])
    return ret = go(i + 1, j + 1) + 1;
  return ret = max(go(i + 1, j), go(i, j + 1));
}
void build(int i, int j) {
  if (i == sz(s) || j == sz(t))
    return;
  if (s[i] == t[j])
    cout << s[i], build(i + 1, j + 1);
  else if (go(i, j) == go(i, j + 1))
    build(i, j + 1);
  else
    build(i + 1, j);
}
void solve() {
  cin >> s >> t;
  mem(dp, -1);
  build(0, 0);
}
int main() {
  fast();
  solve();
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define loop(n, i, a) for (ll i = a; i < n; i++)
#define loopR(n, i, a) for (ll i = n - 1; i >= a; i--)
#define all(arr, n) arr, arr + n
#define allv(v) (v).begin(), (v).end()
#define rallv(v) (v).rbegin(), (v).rend()
#define m_p make_pair
#define ll long long
#define pii pair<ll, ll>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<pair<ll, ll>>
#define sz(x) (int)x.size()
#define pb push_back
#define endl "\n"
#define Endl "\n"
#define f first
#define s second
#define mem(dp, n) memset(dp, n, sizeof dp)
#define test(i) cout << i << endl
int dx[] = {-1, 1, 0, 0, -1, -1, 1, 1};
int dy[] = {0, 0, 1, -1, -1, 1, -1, 1};
void fast() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
}
const int mxN = 3e3 + 5, oo = 0x3f3f3f3f, mod = 1e9 + 7;
string s, t;
int dp[mxN][mxN];
int go(int i, int j) {
  if (i == sz(s) || j == sz(t))
    return 0;
  int &ret = dp[i][j];
  if (~ret)
    return ret;
  if (s[i] == t[j])
    return ret = go(i + 1, j + 1) + 1;
  return ret = max(go(i + 1, j), go(i, j + 1));
}
void build(int i, int j) {
  if (i == sz(s) || j == sz(t))
    return;
  if (s[i] == t[j])
    cout << s[i], build(i + 1, j + 1);
  else if (go(i, j) == go(i, j + 1))
    build(i, j + 1);
  else
    build(i + 1, j);
}
void solve() {
  cin >> s >> t;
  mem(dp, -1);
  build(0, 0);
}
int main() {
  fast();
  solve();
}