Problem p03165 (C++)

Buggy submission (status: Runtime Error):
/*------U Have To DO It------*/
/* BY-> RicoProg */
/*        ___
           __|__|
          |  |___       */
#include <bits/stdc++.h>
using namespace std;

//---------------------------------------------------MACROS----------------------------------------------------------
#define ll long long
#define beg(i, n) for (int i = 0; i < n; i++)
#define beg1(i, n) for (int i = 1; i < n; i++)
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define pb push_back
#define mp make_pair
#define nl "\n"
#define vec(v, n) vector<ll> v(n)
#define all(x) x.begin(), x.end()
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mii map<int, int>
#define mll map<ll, ll>
#define msi map<string, int>
#define mci map<char, int>
#define f1 first
#define f2 second

//---------------------------------------------------GLOBAL----------------------------------------------------------
const ll MOD = 998244353;
const ll MAX = 1e5 + 7;
//---------------------------------------------------FUNCTIONS-------------------------------------------------------

struct node {
  ll n, x, y;
  bool st;
};

node dp[3001][3001];
string s, t;

void rec(ll x, ll y) {
  if (x < 0 || y < 0) {
    return;
  } else if (dp[x][y].n != -1) {
    return;
  } else if (s[x] == t[y]) {
    // cout << x << " A " << y << nl ;
    rec(x - 1, y - 1);
    if (x - 1 >= 0 && y - 1 >= 0)
      dp[x][y].n = 1 + dp[x - 1][y - 1].n;
    else
      dp[x][y].n = 1 + 0;
    dp[x][y].st = true;
    dp[x][y].x = x - 1, dp[x][y].y = y - 1;
    return;
  } else {
    // cout << x << " B " << y << nl ;
    rec(x - 1, y);
    rec(x, y - 1);
    ll a = (x - 1 > -1 && y > -1) * (dp[x - 1][y].n);
    ll b = (x > -1 && y - 1 > -1) * (dp[x][y - 1].n);
    dp[x][y].st = false;
    if (a > b)
      dp[x][y].x = x - 1, dp[x][y].y = y, dp[x][y].n = a;
    else
      dp[x][y].x = x, dp[x][y].y = y - 1, dp[x][y].n = b;
    return;
  }
  return;
}

void solve() {
  beg(i, 3001) { beg(j, 3001) dp[i][j].n = -1, dp[i][j].st = false; }
  cin >> s >> t;
  rec(s.length() - 1, t.length() - 1);
  string ans = "";
  ll x_n = s.length() - 1, y_n = t.length() - 1, x_t, y_t;
  while (true) {
    // cout << x_n << " " << y_n << nl ;
    if (x_n < 0 || y_n < 0) {
      break;
    }
    x_t = x_n, y_t = y_n;
    x_n = dp[x_t][y_t].x;
    y_n = dp[x_t][y_t].y;
    if (dp[x_t][y_t].st)
      ans += s[x_t];
  }
  // cout << dp[s.length()-1][t.length()-1].n << nl ;
  reverse(all(ans));
  cout << ans;
}

//---------------------------------------------------DRIVER----------------------------------------------------------
int main() {
  fast;
  int t = 1;
  // cin >> t ;
  while (t--) {
    solve();
  }
  return 0;
}

Fixed submission:
/*------U Have To DO It------*/
/* BY-> RicoProg */
/*        ___
           __|__|
          |  |___       */
#include <bits/stdc++.h>
using namespace std;

//---------------------------------------------------MACROS----------------------------------------------------------
#define ll long long
#define beg(i, n) for (int i = 0; i < n; i++)
#define beg1(i, n) for (int i = 1; i < n; i++)
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define pb push_back
#define mp make_pair
#define nl "\n"
#define vec(v, n) vector<ll> v(n)
#define all(x) x.begin(), x.end()
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mii map<int, int>
#define mll map<ll, ll>
#define msi map<string, int>
#define mci map<char, int>
#define f1 first
#define f2 second

//---------------------------------------------------GLOBAL----------------------------------------------------------
const ll MOD = 998244353;
const ll MAX = 1e5 + 7;
//---------------------------------------------------FUNCTIONS-------------------------------------------------------

struct node {
  ll n, x, y;
  bool st;
};

node dp[3001][3001];
string s, t;

void rec(ll x, ll y) {
  if (x < 0 || y < 0) {
    return;
  } else if (dp[x][y].n != -1) {
    return;
  } else if (s[x] == t[y]) {
    // cout << x << " A " << y << nl ;
    rec(x - 1, y - 1);
    if (x - 1 >= 0 && y - 1 >= 0)
      dp[x][y].n = 1 + dp[x - 1][y - 1].n;
    else
      dp[x][y].n = 1 + 0;
    dp[x][y].st = true;
    dp[x][y].x = x - 1, dp[x][y].y = y - 1;
    return;
  } else {
    // cout << x << " B " << y << nl ;
    rec(x - 1, y);
    rec(x, y - 1);
    ll a, b;
    a = b = 0;
    if (x - 1 > -1)
      a = dp[x - 1][y].n;
    if (y - 1 > -1)
      b = dp[x][y - 1].n;
    dp[x][y].st = false;
    if (a > b)
      dp[x][y].x = x - 1, dp[x][y].y = y, dp[x][y].n = a;
    else
      dp[x][y].x = x, dp[x][y].y = y - 1, dp[x][y].n = b;
    return;
  }
  return;
}

void solve() {
  beg(i, 3001) { beg(j, 3001) dp[i][j].n = -1, dp[i][j].st = false; }
  cin >> s >> t;
  rec(s.length() - 1, t.length() - 1);
  string ans = "";
  ll x_n = s.length() - 1, y_n = t.length() - 1, x_t, y_t;
  while (true) {
    // cout << x_n << " " << y_n << nl ;
    if (x_n < 0 || y_n < 0) {
      break;
    }
    x_t = x_n, y_t = y_n;
    x_n = dp[x_t][y_t].x;
    y_n = dp[x_t][y_t].y;
    if (dp[x_t][y_t].st)
      ans += s[x_t];
  }
  // cout << dp[s.length()-1][t.length()-1].n << nl ;
  reverse(all(ans));
  cout << ans;
}

//---------------------------------------------------DRIVER----------------------------------------------------------
int main() {
  fast;
  int t = 1;
  // cin >> t ;
  while (t--) {
    solve();
  }
  return 0;
}