Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define ld long double
#define pb push_back
#define MOD 2019
#define inf 3e18
#define vi vector<int>
#define vld vector<ld>
#define pii pair<int, int>
#define mii map<int, int>
#define fi first
#define se second
#define fastIO                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define db(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << " " << name << " : " << arg1 << '\n';
}
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...);
}

void inp_out() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("debug.txt", "w", stderr);
#endif
}

int dp[3000][3000];
string s, t;

int fun(int idx_s, int idx_t) {

  // db(idx_s,idx_t);
  if ((idx_s == s.size()) || (idx_t == t.size()))
    return dp[idx_s][idx_t] = 0;
  if (dp[idx_s][idx_t] != -1)
    return dp[idx_s][idx_t];
  int &ans = dp[idx_s][idx_t];
  if (s[idx_s] == t[idx_t])
    ans = 1 + fun(idx_s + 1, idx_t + 1);
  ans = max(ans, fun(idx_s + 1, idx_t));
  ans = max(ans, fun(idx_s, idx_t + 1));

  return ans;
}

int32_t main() {
  fastIO
      // inp_out();
      // string s,t;
      memset(dp, -1, sizeof(dp));
  cin >> s >> t;
  fun(0, 0);
  string ans = "";
  int idx_s = 0, idx_t = 0;
  while (idx_s < s.size() and idx_t < t.size()) {
    if ((dp[idx_s + 1][idx_t + 1] == dp[idx_s][idx_t] - 1) and
        (s[idx_s] == t[idx_t])) {
      ans += s[idx_s];
      idx_s += 1;
      idx_t += 1;
    } else if (dp[idx_s][idx_t] == dp[idx_s + 1][idx_t])
      idx_s += 1;
    else
      idx_t += 1;
  }
  cout << ans;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define ld long double
#define pb push_back
#define MOD 2019
#define inf 3e18
#define vi vector<int>
#define vld vector<ld>
#define pii pair<int, int>
#define mii map<int, int>
#define fi first
#define se second
#define fastIO                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define db(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << " " << name << " : " << arg1 << '\n';
}
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...);
}

void inp_out() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("debug.txt", "w", stderr);
#endif
}

int dp[3005][3005];
string s, t;

int fun(int idx_s, int idx_t) {

  // db(idx_s,idx_t);
  if ((idx_s == s.size()) || (idx_t == t.size()))
    return dp[idx_s][idx_t] = 0;
  if (dp[idx_s][idx_t] != -1)
    return dp[idx_s][idx_t];
  int &ans = dp[idx_s][idx_t];
  if (s[idx_s] == t[idx_t])
    ans = 1 + fun(idx_s + 1, idx_t + 1);
  ans = max(ans, fun(idx_s + 1, idx_t));
  ans = max(ans, fun(idx_s, idx_t + 1));

  return ans;
}

int32_t main() {
  fastIO
      // inp_out();
      // string s,t;
      memset(dp, -1, sizeof(dp));
  cin >> s >> t;
  fun(0, 0);
  string ans = "";
  int idx_s = 0, idx_t = 0;
  while (idx_s < s.size() and idx_t < t.size()) {
    if ((dp[idx_s + 1][idx_t + 1] == dp[idx_s][idx_t] - 1) and
        (s[idx_s] == t[idx_t])) {
      ans += s[idx_s];
      idx_s += 1;
      idx_t += 1;
    } else if (dp[idx_s][idx_t] == dp[idx_s + 1][idx_t])
      idx_s += 1;
    else
      idx_t += 1;
  }
  cout << ans;

  return 0;
}