Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define FasterIO                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define bug(a, b) cout << a << "\t" << b << "\n";
#define Case(a, b) cout << "Case " << a << ": " << b << "\n";
#define watch(a) cout << fixed << setprecision(a)
#define lp(i, a, b) for (int i = a; i < b; i++)
#define Endl "\n";
#define tab "\t"
#define reset(a, b) memset(a, b, sizeof(a));
#define sf(a) scanf(" %d", &a);
#define srt(a) sort(a.begin(), a.end());
#define pi 2 * acos(0.0)
#define inf INT_MAX
#define infn INT_MIN
const int sz = 1e5 + 9;
string s, t;
int dp[3009][3009];

void lcs(int i, int j) {
  if (i >= s.size() || j >= t.size())
    return;
  if (dp[i][j] != -1)
    return;
  if (s[i] == t[j]) {
    lcs(i + 1, j + 1);
    dp[i][j] = 1 + max(0, dp[i + 1][j + 1]);

  } else {
    lcs(i + 1, j);
    lcs(i, j + 1);
    dp[i][j] = max(dp[i + 1][j], dp[i][j + 1]);
    dp[i][j] = max(0, dp[i][j]);
  }
  return;
}

void solve() {
  reset(dp, -1);
  getline(cin, s);
  getline(cin, t);
  lcs(0, 0);
  // cout<<dp[0][0];
  /*   for(int i=0;i<=s.size();i++){
        for(int j=0;j<=t.size();j++){
            cout<<dp[i][j]<<tab;
        }
        cout<<endl;
     }*/
  int i = 0, j = 0, temp = dp[i][j];
  if (temp == 0) {
    cout << "\n\n";
    return;
  }
  while (true) {

    if (dp[i][j + 1] == temp) {
      j++;
      continue;
    }
    if (dp[i + 1][j] == temp) {
      i++;
      continue;
    }
    if (temp == 1) {
      cout << s[i];
      break;
    }
    if (dp[i + 1][j + 1] == temp - 1) {
      cout << s[i];
      temp--, i++, j++;
      continue;
    }
  }
  cout << "\n";
}

int main() {
  FasterIO;
#ifndef ONLINE_JUDGE
  freopen("1.txt", "r", stdin);
#endif
  // int t;
  //  cin>>t;
  //   while(t--)
  solve();
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define FasterIO                                                               \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define bug(a, b) cout << a << "\t" << b << "\n";
#define Case(a, b) cout << "Case " << a << ": " << b << "\n";
#define watch(a) cout << fixed << setprecision(a)
#define lp(i, a, b) for (int i = a; i < b; i++)
#define Endl "\n";
#define tab "\t"
#define reset(a, b) memset(a, b, sizeof(a));
#define sf(a) scanf(" %d", &a);
#define srt(a) sort(a.begin(), a.end());
#define pi 2 * acos(0.0)
#define inf INT_MAX
#define infn INT_MIN
const int sz = 1e5 + 9;
string s, t;
int dp[3009][3009];

void lcs(int i, int j) {
  if (i >= s.size() || j >= t.size())
    return;
  if (dp[i][j] != -1)
    return;
  if (s[i] == t[j]) {
    lcs(i + 1, j + 1);
    dp[i][j] = 1 + max(0, dp[i + 1][j + 1]);

  } else {
    lcs(i + 1, j);
    lcs(i, j + 1);
    dp[i][j] = max(dp[i + 1][j], dp[i][j + 1]);
    dp[i][j] = max(0, dp[i][j]);
  }
  return;
}

void solve() {
  reset(dp, -1);
  getline(cin, s);
  getline(cin, t);
  lcs(0, 0);
  // cout<<dp[0][0];
  /*   for(int i=0;i<=s.size();i++){
        for(int j=0;j<=t.size();j++){
            cout<<dp[i][j]<<tab;
        }
        cout<<endl;
     }*/
  int i = 0, j = 0, temp = dp[i][j];
  if (temp == 0) {
    cout << "\n\n";
    return;
  }
  while (true) {

    if (dp[i][j + 1] == temp) {
      j++;
      continue;
    }
    if (dp[i + 1][j] == temp) {
      i++;
      continue;
    }
    if (temp == 1) {
      cout << s[i];
      break;
    }
    if (dp[i + 1][j + 1] == temp - 1) {
      cout << s[i];
      temp--, i++, j++;
      continue;
    }
  }
  cout << "\n";
}

int main() {
  /*    FasterIO;
      #ifndef ONLINE_JUDGE
          freopen("1.txt","r",stdin);
      #endif
      */
  // int t;
  //  cin>>t;
  //   while(t--)
  solve();
}