Problem p03165 (C++)

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

#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);

#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define mp make_pair
#define pu push
#define pp pop_back
#define in insert
#define ld long double
#define endl '\n'
#define debug cout << "Hold right there sparky.....\n";
#define forn(low, high, i) for (i = low; i < high; i++)
#define forrev(high, low, i) for (i = high; i >= low; i--)
#define trace(x) cerr << #x << ": " << x << " " << endl;
#define zeroes(x) memset(x, 0, sizeof(x))
typedef long long int ll;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
const ll mod = 1e9 + 7;
const ll maxn = 100000;

int main() {

  IOS string s, t;
  cin >> s >> t;
  s = "$" + s;
  t = "$" + t;
  ll dp[300][300];
  for (ll i = 0; i < s.size(); i++) {
    for (ll j = 0; j < t.size(); j++) {
      if (i == 0 || j == 0) {
        dp[i][j] = 0;
        continue;
      }
      if (s[i] == t[j]) {
        dp[i][j] = (dp[i - 1][j - 1]) + 1;

        // trace(dp[i][j]);
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }
  ll i = s.size() - 1;
  ll j = t.size() - 1;
  string ans = "";
  while (i > 0 && j > 0) {
    if (s[i] == t[j]) {
      ans += (s[i]);
      i--;
      j--;
    } else if (dp[i - 1][j] > dp[i][j - 1]) {
      i--;
    } else
      j--;
  }
  reverse(ans.begin(), ans.end());

  cout << ans << endl;
}


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

#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);

#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define mp make_pair
#define pu push
#define pp pop_back
#define in insert
#define ld long double
#define endl '\n'
#define debug cout << "Hold right there sparky.....\n";
#define forn(low, high, i) for (i = low; i < high; i++)
#define forrev(high, low, i) for (i = high; i >= low; i--)
#define trace(x) cerr << #x << ": " << x << " " << endl;
#define zeroes(x) memset(x, 0, sizeof(x))
typedef long long int ll;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
const ll mod = 1e9 + 7;
const ll maxn = 100000;

int main() {

  IOS string s, t;
  cin >> s >> t;
  s = "$" + s;
  t = "$" + t;
  ll dp[3005][3005];
  for (ll i = 0; i < s.size(); i++) {
    for (ll j = 0; j < t.size(); j++) {
      if (i == 0 || j == 0) {
        dp[i][j] = 0;
        continue;
      }
      if (s[i] == t[j]) {
        dp[i][j] = (dp[i - 1][j - 1]) + 1;

        // trace(dp[i][j]);
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }
  ll i = s.size() - 1;
  ll j = t.size() - 1;
  string ans = "";
  while (i > 0 && j > 0) {
    if (s[i] == t[j]) {
      ans += (s[i]);
      i--;
      j--;
    } else if (dp[i - 1][j] > dp[i][j - 1]) {
      i--;
    } else
      j--;
  }
  reverse(ans.begin(), ans.end());

  cout << ans << endl;
}