Problem p03165 (C++)

Buggy submission (status: Runtime Error):
// Code by : Abhishek Tiwari
// codechef.com/users/devil_within || hackerearth.com/@devil_within
#include <bits/stdc++.h>
#define fio                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define int long long int
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define repr(i, a, b) for (int i = (a); i > (b); --i)
#define mp make_pair
#define pb push_back
#define pf push_front
#define pii pair<int, int>
#define ff first
#define ss second
#define mod 1000000007
#define inf 1000000000
#define gcd __gcd
#define lcm(a, b) ((a) * (b)) / gcd(a, b)
using namespace std;

/*******************************************************************************************/
/*******************************************************************************************/
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "[";
  for (int i = 0; i < v.size() - 1; ++i)
    os << v[i] << ", ";
  os << v[v.size() - 1] << "]\n";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &v) {
  os << "{";
  for (auto it : v) {
    os << it;
    if (it != *v.rbegin())
      os << ", ";
  }
  os << "}\n";
  return os;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const map<T, S> &v) {
  os << "{";
  for (auto it : v) {
    os << "(" << it.first << " : " << it.second << ")";
    if (it != *v.rbegin())
      os << ", ";
  }
  os << "}\n";
  return os;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const pair<T, S> &v) {
  os << "(";
  os << v.first << ", " << v.second << ")";
  return os;
}
template <typename T> T maxi(T a, T b) { return a > b ? a : b; }
template <typename T> T mini(T a, T b) { return a < b ? a : b; }
/*******************************************************************************************/
/*******************************************************************************************/
int fpow(int x, int y, int p);
vector<vector<int>> dp;
int Lcs(int i, int j, string &s, string &t) {
  if (i == s.length() || j == t.length())
    return 0;
  if (dp[i][j] != -1)
    return dp[i][j];
  if (s[i] == t[j])
    dp[i][j] = 1 + Lcs(i + 1, j + 1, s, t);
  else
    dp[i][j] = maxi(Lcs(i + 1, j, s, t), Lcs(i, j + 1, s, t));
  return dp[i][j];
}
string findLcs(int i, int j, string &s, string &t) {
  if (i >= s.length() || j >= t.length())
    return "";
  if (s[i] == t[j])
    return s[i] + findLcs(i + 1, j + 1, s, t);
  else if (dp[i + 1][j] > dp[i][j + 1])
    return findLcs(i + 1, j, s, t);
  else
    return findLcs(i, j + 1, s, t);
}
int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif
  fio;
  int tcases = 1;
  // cin>>tcases;
  for (int tcase = 1; tcase <= tcases; ++tcase) {
    string s, t;
    cin >> s >> t;
    dp.resize(s.length() + 1);
    rep(i, 0, s.length() + 1) dp[i].resize(t.length() + 1, -1);
    Lcs(0, 0, s, t);
    // for(auto  i : dp) cout<<i;
    cout << findLcs(0, 0, s, t);
    // cout<<"Case #"<<tcase<<": ";
  }
  return 0;
}

// Functions

int fpow(int x, int y, int p) {
  int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}


Fixed submission:
// Code by : Abhishek Tiwari
// codechef.com/users/devil_within || hackerearth.com/@devil_within
#include <bits/stdc++.h>
#define fio                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define int long long int
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define repr(i, a, b) for (int i = (a); i > (b); --i)
#define mp make_pair
#define pb push_back
#define pf push_front
#define pii pair<int, int>
#define ff first
#define ss second
#define mod 1000000007
#define inf 1000000000
#define gcd __gcd
#define lcm(a, b) ((a) * (b)) / gcd(a, b)
using namespace std;

/*******************************************************************************************/
/*******************************************************************************************/
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "[";
  for (int i = 0; i < v.size() - 1; ++i)
    os << v[i] << ", ";
  os << v[v.size() - 1] << "]\n";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &v) {
  os << "{";
  for (auto it : v) {
    os << it;
    if (it != *v.rbegin())
      os << ", ";
  }
  os << "}\n";
  return os;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const map<T, S> &v) {
  os << "{";
  for (auto it : v) {
    os << "(" << it.first << " : " << it.second << ")";
    if (it != *v.rbegin())
      os << ", ";
  }
  os << "}\n";
  return os;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const pair<T, S> &v) {
  os << "(";
  os << v.first << ", " << v.second << ")";
  return os;
}
template <typename T> T maxi(T a, T b) { return a > b ? a : b; }
template <typename T> T mini(T a, T b) { return a < b ? a : b; }
/*******************************************************************************************/
/*******************************************************************************************/
int fpow(int x, int y, int p);
vector<vector<int>> dp;
int Lcs(int i, int j, string &s, string &t) {
  if (i == s.length() || j == t.length())
    return 0;
  if (dp[i][j] != -1)
    return dp[i][j];
  if (s[i] == t[j])
    dp[i][j] = 1 + Lcs(i + 1, j + 1, s, t);
  else
    dp[i][j] = maxi(Lcs(i + 1, j, s, t), Lcs(i, j + 1, s, t));
  return dp[i][j];
}
string findLcs(int i, int j, string &s, string &t) {
  if (i >= s.length() || j >= t.length())
    return "";
  if (s[i] == t[j])
    return s[i] + findLcs(i + 1, j + 1, s, t);
  else if (dp[i + 1][j] > dp[i][j + 1])
    return findLcs(i + 1, j, s, t);
  else
    return findLcs(i, j + 1, s, t);
}
int32_t main() {
#ifndef ONLINE_JUDGE
// freopen("input.txt" , "r" , stdin);freopen("output.txt" , "w" ,
// stdout);freopen("error.txt" , "w" , stderr);
#endif
  fio;
  int tcases = 1;
  // cin>>tcases;
  for (int tcase = 1; tcase <= tcases; ++tcase) {
    string s, t;
    cin >> s >> t;
    dp.resize(s.length() + 1);
    rep(i, 0, s.length() + 1) dp[i].resize(t.length() + 1, -1);
    Lcs(0, 0, s, t);
    // for(auto  i : dp) cout<<i;
    cout << findLcs(0, 0, s, t);
    // cout<<"Case #"<<tcase<<": ";
  }
  return 0;
}

// Functions

int fpow(int x, int y, int p) {
  int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}