Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define Int int32_t
#define all(c) c.begin(), c.end()
#define FAST ios_base::sync_with_stdio(false), cin.tie(NULL);
#define pii pair<int, int>
#define pb push_back
#define F first
#define S second
#define endl "\n"
#define bitcnt(n) __builtin_popcountll(n)
#define setpre(n) cout << fixed << setprecision(n)
#define tr(c)                                                                  \
  for (const auto &x : c)                                                      \
    cout << x << " ";                                                          \
  cout << "\n";
#define ol(c, s, e)                                                            \
  for (int pos = s; pos < e; pos++)                                            \
    cout << c[pos] << " ";                                                     \
  cout << "\n";
#define PI acos(-1LL)

const int M = 1000000007;
const int N = 2e5 + 5;
const long long INF = 1e9 + 12;

string a, b;
int dp[3005][3005];
int fun(int i, int j) {
  if (i < 0 || j < 0)
    return 0;
  if (dp[i][j] != -1)
    return dp[i][j];
  if (a[i] == b[j]) {
    return dp[i][j] = 1 + fun(i - 1, j - 1);
  } else {
    return dp[i][j] = max(fun(i - 1, j), fun(i, j - 1));
  }
}
string ans;
void path(int i, int j) {
  if (i < 0 || j < 0)
    return;
  if (a[i] == b[j]) {
    ans += a[i];
    path(i - 1, j - 1);
  } else {
    if (dp[i][j] == dp[i - 1][j])
      path(i - 1, j);
    else
      path(i, j - 1);
  }
}
void solve() {
  cin >> a >> b;
  memset(dp, -1, sizeof dp);
  fun(a.size() - 1, b.size() - 1);
  path(a.size() - 1, b.size() - 1);
  reverse(all(ans));
  cout << ans;
}

signed main() {
  FAST int tc = 1;
  //    TODO: check for tc
  //    cin >> tc;
  for (int t = 1; t <= tc; t++) {
    solve();
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define Int int32_t
#define all(c) c.begin(), c.end()
#define FAST ios_base::sync_with_stdio(false), cin.tie(NULL);
#define pii pair<int, int>
#define pb push_back
#define F first
#define S second
#define endl "\n"
#define bitcnt(n) __builtin_popcountll(n)
#define setpre(n) cout << fixed << setprecision(n)
#define tr(c)                                                                  \
  for (const auto &x : c)                                                      \
    cout << x << " ";                                                          \
  cout << "\n";
#define ol(c, s, e)                                                            \
  for (int pos = s; pos < e; pos++)                                            \
    cout << c[pos] << " ";                                                     \
  cout << "\n";
#define PI acos(-1LL)

const int M = 1000000007;
const int N = 2e5 + 5;
const long long INF = 1e9 + 12;

string a, b;
int dp[3005][3005];
int fun(int i, int j) {
  if (i < 0 || j < 0)
    return 0;
  if (dp[i][j] != -1)
    return dp[i][j];
  if (a[i] == b[j]) {
    return dp[i][j] = 1 + fun(i - 1, j - 1);
  } else {
    return dp[i][j] = max(fun(i - 1, j), fun(i, j - 1));
  }
}
string ans;
void path(int i, int j) {
  if (i < 0 || j < 0)
    return;
  if (a[i] == b[j]) {
    ans += a[i];
    path(i - 1, j - 1);
  } else {
    if (fun(i, j) == fun(i - 1, j))
      path(i - 1, j);
    else
      path(i, j - 1);
  }
}
void solve() {
  cin >> a >> b;
  memset(dp, -1, sizeof dp);
  fun(a.size() - 1, b.size() - 1);
  path(a.size() - 1, b.size() - 1);
  reverse(all(ans));
  cout << ans;
}

signed main() {
  FAST int tc = 1;
  //    TODO: check for tc
  //    cin >> tc;
  for (int t = 1; t <= tc; t++) {
    solve();
  }
}