Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define M 1000000007
#define deb(x) cout << #x << " " << x << endl;
#define foi(a, b) for (int i = a; i < b; i++)
#define foj(a, b) for (int j = a; j < b; j++)
#define fod(a, b) for (int i = a; i > b; i--)
#define MAX 1e9
#define MIN -1e9
#define nl cout << "\n";
#define show(arr, a, b)                                                        \
  for (int i = a; i < b; i++)                                                  \
    cout << arr[i] << " ";                                                     \
  cout << endl;
#define mp(a, b) pair<ll, ll> p(a, b)
#define pb push_back
typedef pair<ll, ll> p1;
typedef vector<ll> vecd;
typedef vector<string> vecsd;
typedef map<ll, ll> mpd;
typedef set<ll> setd;
typedef multiset<ll> msetd;
ll dp[3002][3002];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  string s1, s2;
  cin >> s1 >> s2;
  foi(0, s1.length() + 1) { foj(0, s2.length() + 1) dp[i][j] = 0; }
  foi(0, s1.length() + 1) {
    foj(0, s2.length() + 1) {
      if (i == 0 || j == 0)
        dp[i][j] = 0;
      else {
        if (s1[i - 1] == s2[j - 1]) {
          dp[i][j] = 1 + dp[i - 1][j - 1];
        } else {
          dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
        }
      }
    }
  }
  string ans = "";
  ll i = s1.length(), j = s2.length();
  while (i > 0 || j > 0) {
    if (dp[i][j] == (dp[i - 1][j - 1] + 1)) {
      ans = s1[i - 1] + ans;
      i--;
      j--;
    } else {
      if (dp[i][j] == dp[i - 1][j])
        i--;
      if (dp[i][j] == dp[i][j - 1])
        j--;
    }
  }
  cout << ans << "\n";
  // cout<<dp[s1.length()][s2.length()]<<"\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define M 1000000007
#define deb(x) cout << #x << " " << x << endl;
#define foi(a, b) for (int i = a; i < b; i++)
#define foj(a, b) for (int j = a; j < b; j++)
#define fod(a, b) for (int i = a; i > b; i--)
#define MAX 1e9
#define MIN -1e9
#define nl cout << "\n";
#define show(arr, a, b)                                                        \
  for (int i = a; i < b; i++)                                                  \
    cout << arr[i] << " ";                                                     \
  cout << endl;
#define mp(a, b) pair<ll, ll> p(a, b)
#define pb push_back
typedef pair<ll, ll> p1;
typedef vector<ll> vecd;
typedef vector<string> vecsd;
typedef map<ll, ll> mpd;
typedef set<ll> setd;
typedef multiset<ll> msetd;
ll dp[3002][3002];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  string s1, s2;
  cin >> s1 >> s2;
  foi(0, s1.length() + 1) { foj(0, s2.length() + 1) dp[i][j] = 0; }
  foi(0, s1.length() + 1) {
    foj(0, s2.length() + 1) {
      if (i == 0 || j == 0)
        dp[i][j] = 0;
      else {
        if (s1[i - 1] == s2[j - 1]) {
          dp[i][j] = 1 + dp[i - 1][j - 1];
        } else {
          dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
        }
      }
    }
  }
  string ans = "";
  ll i = s1.length(), j = s2.length();
  while (i > 0 && j > 0) {
    if (dp[i][j] == (dp[i - 1][j - 1] + 1) && s1[i - 1] == s2[j - 1]) {
      ans = s1[i - 1] + ans;
      i--;
      j--;
    } else {
      if (dp[i][j] == dp[i - 1][j])
        i--;
      if (dp[i][j] == dp[i][j - 1])
        j--;
    }
  }
  cout << ans << "\n";
  // cout<<dp[s1.length()][s2.length()]<<"\n";
  return 0;
}