Problem p03165 (C++)

Buggy submission (status: Runtime Error):
/*
@author: @sancodemonster
*/

#include <bits/stdc++.h>

#define flash ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define endl '\n'
#define ll long long int
#define ld long double
#define pb push_back
#define mp make_pair
#define MOD 1000000007
#define f(i, a, b) for (long long i = a; i < b; i++)
#define all(c) (c).begin(), (c).end()
using namespace std;
string s, t;
ll dp[3010][3010];
ll parentDP[3010][3010] = {0};
string ansStr = "";
std::vector<ll> v;
ll rec(ll i, ll j, string &s, string &t) {

  if (i == s.size() || j == t.size()) {
    return 0;
  }
  ll one = INT_MIN, two = INT_MIN, three = INT_MIN;

  if (dp[i][j] != -1)
    return dp[i][j];

  if (s[i] == t[j]) {
    one = 1 + rec(i + 1, j + 1, s, t);
  }

  else {
    // if(i<s.size())
    two = rec(i + 1, j, s, t);

    // if(j<t.size())
    three = rec(i, j + 1, s, t);
  }

  ll maxi = INT_MIN;

  maxi = max(one, two);
  if (maxi == one) {
    parentDP[i][j] = 1;
    dp[i][j] = one;
  } else {
    parentDP[i][j] = 2;
    dp[i][j] = two;
  }

  maxi = max(maxi, three);
  if (maxi == three) {
    parentDP[i][j] = 3;
    dp[i][j] = three;
  }

  return dp[i][j];
}

void backtrack(ll i, ll j, string &s, string &t) {
  if (i == s.size() || j == t.size()) {
    return;
  }

  if (parentDP[i][j] == 1) {
    v.push_back(i);
    backtrack(i + 1, j + 1, s, t);
  }

  if (parentDP[i][j] == 2) {
    backtrack(i + 1, j, s, t);
  }

  if (parentDP[i][j] == 3)
    backtrack(i, j + 1, s, t);
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("C:\\desktopp\\new coding problems\\in.txt", "r", stdin);
  freopen("C:\\desktopp\\new coding problems\\out.txt", "w", stdout);
#endif

  flash;
  /////////////////////////////////////////////////////////////////////////////////////////////////////
  cin >> s;
  cin >> t;

  ll i, j;
  f(i, 0, 3010) {
    f(j, 0, 3010) { dp[i][j] = -1; }
  }
  ll str = rec(0, 0, s, t);
  // cout<<str;
  backtrack(0, 0, s, t);
  f(i, 0, v.size()) { cout << s[v[i]]; }
  //   cout<<ansStr;
  /////////////////////////////////////////////////////////////////////////////////////////////////////

  return 0;
}


Fixed submission:
/*
@author: @sancodemonster
*/

#include <bits/stdc++.h>

#define flash ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define endl '\n'
#define ll long long int
#define ld long double
#define pb push_back
#define mp make_pair
#define MOD 1000000007
#define f(i, a, b) for (long long i = a; i < b; i++)
#define all(c) (c).begin(), (c).end()
using namespace std;
string s, t;
ll dp[3010][3010];
ll parentDP[3010][3010] = {0};
string ansStr = "";
std::vector<ll> v;
ll rec(ll i, ll j, string &s, string &t) {

  if (i == s.size() || j == t.size()) {
    return 0;
  }
  ll one = INT_MIN, two = INT_MIN, three = INT_MIN;

  if (dp[i][j] != -1)
    return dp[i][j];

  if (s[i] == t[j]) {
    one = 1 + rec(i + 1, j + 1, s, t);
  }

  else {
    // if(i<s.size())
    two = rec(i + 1, j, s, t);

    // if(j<t.size())
    three = rec(i, j + 1, s, t);
  }

  ll maxi = INT_MIN;

  maxi = max(one, two);
  if (maxi == one) {
    parentDP[i][j] = 1;
    dp[i][j] = one;
  } else {
    parentDP[i][j] = 2;
    dp[i][j] = two;
  }

  maxi = max(maxi, three);
  if (maxi == three) {
    parentDP[i][j] = 3;
    dp[i][j] = three;
  }

  return dp[i][j];
}

void backtrack(ll i, ll j, string &s, string &t) {
  if (i == s.size() || j == t.size()) {
    return;
  }

  if (parentDP[i][j] == 1) {
    v.push_back(i);
    backtrack(i + 1, j + 1, s, t);
  }

  if (parentDP[i][j] == 2) {
    backtrack(i + 1, j, s, t);
  }

  if (parentDP[i][j] == 3)
    backtrack(i, j + 1, s, t);
}

int main() {

  flash;
  /////////////////////////////////////////////////////////////////////////////////////////////////////
  cin >> s;
  cin >> t;

  ll i, j;
  f(i, 0, 3010) {
    f(j, 0, 3010) { dp[i][j] = -1; }
  }
  ll str = rec(0, 0, s, t);
  // cout<<str;
  backtrack(0, 0, s, t);
  f(i, 0, v.size()) { cout << s[v[i]]; }
  //   cout<<ansStr;
  /////////////////////////////////////////////////////////////////////////////////////////////////////

  return 0;
}