Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define mk make_pair
#define fi first
#define se second
#define vll vector<ll>
#define pii pair<ll, ll>
#define vvll vector<vector<ll>>
#define pb push_back
#define sz(v) (v).size()
#define inf 1e18
#define md 1000000007
#define all(v) (v).begin(), (v).end()
#define rep(i, a, b) for (ll i = a; i < b; ++i)
#define repi(i, a, b) for (ll i = a; i >= b; --i)
#define tel(a)                                                                 \
  { cout << a << "\n"; }
#define tell(a, b)                                                             \
  { cout << a << " | " << b << "\n"; }
#define telll(a, b, c)                                                         \
  { cout << a << " | " << b << " | " << c << "\n"; }
#define teln(v, n)                                                             \
  {                                                                            \
    cout << "v- ";                                                             \
    rep(i, 0, n) cout << v[i] << " ";                                          \
    cout << "\n";                                                              \
  }
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
using namespace std;
#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

#define M 110
#define N 1010

string s, t, ans;
ll dp[N][N];

ll f(ll i, ll j) {
  if (!i or !j)
    return 0;
  if (dp[i][j] != -1)
    return dp[i][j];
  ll ans = 0;
  if (s[i - 1] == t[j - 1])
    ans = f(i - 1, j - 1) + 1;
  else
    ans = max(f(i - 1, j), f(i, j - 1));
  return dp[i][j] = ans;
}
void find(ll i, ll j) {
  if (!i or !j)
    return;
  if (s[i - 1] == t[j - 1])
    ans += s[i - 1], find(i - 1, j - 1);
  else {
    if (f(i - 1, j) > f(i, j - 1))
      find(i - 1, j);
    else
      find(i, j - 1);
  }
}
int main() {
  IOS;
  // freopen("inp.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);

  cin >> s >> t;
  memset(dp, -1, sizeof(dp));
  ans = "";
  ll n = sz(s), m = sz(t);
  find(n, m);
  ll x = sz(ans);
  if (x)
    repi(i, x - 1, 0) cout << ans[i];
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define mk make_pair
#define fi first
#define se second
#define vll vector<ll>
#define pii pair<ll, ll>
#define vvll vector<vector<ll>>
#define pb push_back
#define sz(v) (v).size()
#define inf 1e18
#define md 1000000007
#define all(v) (v).begin(), (v).end()
#define rep(i, a, b) for (ll i = a; i < b; ++i)
#define repi(i, a, b) for (ll i = a; i >= b; --i)
#define tel(a)                                                                 \
  { cout << a << "\n"; }
#define tell(a, b)                                                             \
  { cout << a << " | " << b << "\n"; }
#define telll(a, b, c)                                                         \
  { cout << a << " | " << b << " | " << c << "\n"; }
#define teln(v, n)                                                             \
  {                                                                            \
    cout << "v- ";                                                             \
    rep(i, 0, n) cout << v[i] << " ";                                          \
    cout << "\n";                                                              \
  }
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
using namespace std;
#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

#define M 110
#define N 3010

string s, t, ans;
ll dp[N][N];

ll f(ll i, ll j) {
  if (!i or !j)
    return 0;
  if (dp[i][j] != -1)
    return dp[i][j];
  ll ans = 0;
  if (s[i - 1] == t[j - 1])
    ans = f(i - 1, j - 1) + 1;
  else
    ans = max(f(i - 1, j), f(i, j - 1));
  return dp[i][j] = ans;
}
void find(ll i, ll j) {
  if (!i or !j)
    return;
  if (s[i - 1] == t[j - 1])
    ans += s[i - 1], find(i - 1, j - 1);
  else {
    if (f(i - 1, j) > f(i, j - 1))
      find(i - 1, j);
    else
      find(i, j - 1);
  }
}
int main() {
  IOS;
  // freopen("inp.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);

  cin >> s >> t;
  memset(dp, -1, sizeof(dp));
  ans = "";
  ll n = sz(s), m = sz(t);
  find(n, m);
  ll x = sz(ans);
  if (x)
    repi(i, x - 1, 0) cout << ans[i];
  return 0;
}