Problem p03165 (C++)

Buggy submission (status: Runtime Error):
/*
kaafi fucked up
*/
#define SET

#pragma GCC optimize("O3")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("unroll-loops")

#define DEBUG

#include <bits/stdc++.h>
#include <string>
#define pb push_back
#define mp make_pair
#define f(i, a, b) for (long long i = a; i < b; i++)
#define revf(i, b, a) for (int i = b; i >= a; i--)
#define srt(v) sort(v.begin(), v.end())
#define rev_srt(v) sort(v.rbegin(), v.rend())
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;

#ifdef SET
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_multi_set;

#endif

#define nl '\n'
#define sp " "
#define precision(x) cout << fixed << setprecision(x);
#define fio                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;

template <typename T> void debugv(vector<T> a) {
#ifdef DEBUG
  for (auto i : a) {
    cout << i << sp;
  }
  cout << nl;
  return;
#endif
  return;
}

template <typename T> void debuga(T *a, int n, int beg = 0) {
#ifdef DEBUG
  f(i, beg, beg + n) { cout << a[i] << sp; }
  cout << nl;
  return;
#endif
  return;
}

void logg(string s) {
#ifdef DEBUG
  cout << s << nl;
#endif
}

template <class T> inline bool remax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <class T> inline bool remin(T &a, T b) { return a > b ? a = b, 1 : 0; }

ll md = 1000000007;
// const ll MXSZ= (ll)5e5;
// ll fact[MXSZ];
// ll mi[MXSZ];

// ll my_gcd(ll n, ll m)
// {
//     if (n % m == 0)
//         return m;
//     if (n < m)
//         swap(n, m);
//     while (m > 0)
//     {
//         n = n % m;
//         swap(n, m);
//     }
//     return n;
// }

ll pw(ll a, ll b) {
  ll c = 1, m = a % md;
  while (b) {
    if (b & 1)
      c = (c * m) % md;
    m = (m * m) % md;
    b /= 2;
  }
  return c;
}

ll modinv(ll n) { return pw(n, md - 2); }

inline ll add(ll a, ll b) { return (md + a % md + b % md) % md; }
inline ll subt(ll a, ll b) { return (a % md - b % md + md) % md; }
inline ll mult(ll a, ll b) { return (1ll * (a % md) * (b % md)) % md; }

// ll ncr(int n, int r)
// {
//     if (!n)
//         return 1;
//     return mult(fact[n], mult(mi[r], mi[n - r]));
// }

// void pre()
// {
//     fact[0] = 1;
//     ll lim = MXSZ;
//     f(i, 1, lim) fact[i] = mult(i, fact[i - 1]);

//     f(i, 0, lim) mi[i] = modinv(fact[i]);
//     // f(i, 0, 10) cout << mi[i] << sp;
//     // cout << nl;
// }
template <class T> T myceil(T a, T b) {
  if (a % b)
    return (a / b) + 1;
  else
    return a / b;
};

const ll MX = 1e3 + 5;

ll dp[MX][MX];
ll btr[MX][MX][2];
ll fl[MX][MX];
ll last[MX];

void solvetestcase(ll tcn) {
  string s, t;
  cin >> s;
  cin >> t;
  ll n = s.size();
  ll m = t.size();

  f(i, 1, n + 1) f(j, 1, m + 1) {
    dp[i][j] = max({dp[i][j - 1], dp[i - 1][j],
                    dp[i - 1][j - 1] + (s[i - 1] == t[j - 1])});
    if (dp[i][j] == dp[i - 1][j - 1] + (s[i - 1] == t[j - 1])) {
      btr[i][j][0] = i - 1;
      btr[i][j][1] = j - 1;
      fl[i][j] = (s[i - 1] == t[j - 1]);
    } else if (dp[i][j] == dp[i][j - 1]) {
      btr[i][j][0] = i;
      btr[i][j][1] = j - 1;
    } else {
      btr[i][j][0] = i - 1;
      btr[i][j][1] = j;
    }
  }
  if (!dp[n][m]) {
    cout << "\n";
    return;
  }
  string ans;
  int x = n;
  int y = m;
  // cout<<dp[n][m]<<nl;
  while (x > 0 && y > 0) {
    if (fl[x][y])
      ans.pb(s[x - 1]);
    ll xx = x;
    x = btr[x][y][0];
    y = btr[xx][y][1];
  }
  reverse(all(ans));
  cout << ans << nl;
}

int main() {
  fio;
  int t = 1;
  // cin >> t;
  // pre();
  f(i, 1, t + 1) {
    // cout << "Case #" << i << ": ";
    solvetestcase(i);

    // cout << nl;
  }
  return 0;
};

Fixed submission:
/*
kaafi fucked up
*/
#define SET

#pragma GCC optimize("O3")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("unroll-loops")

#define DEBUG

#include <bits/stdc++.h>
#include <string>
#define pb push_back
#define mp make_pair
#define f(i, a, b) for (long long i = a; i < b; i++)
#define revf(i, b, a) for (int i = b; i >= a; i--)
#define srt(v) sort(v.begin(), v.end())
#define rev_srt(v) sort(v.rbegin(), v.rend())
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;

#ifdef SET
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_multi_set;

#endif

#define nl '\n'
#define sp " "
#define precision(x) cout << fixed << setprecision(x);
#define fio                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;

template <typename T> void debugv(vector<T> a) {
#ifdef DEBUG
  for (auto i : a) {
    cout << i << sp;
  }
  cout << nl;
  return;
#endif
  return;
}

template <typename T> void debuga(T *a, int n, int beg = 0) {
#ifdef DEBUG
  f(i, beg, beg + n) { cout << a[i] << sp; }
  cout << nl;
  return;
#endif
  return;
}

void logg(string s) {
#ifdef DEBUG
  cout << s << nl;
#endif
}

template <class T> inline bool remax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <class T> inline bool remin(T &a, T b) { return a > b ? a = b, 1 : 0; }

ll md = 1000000007;
// const ll MXSZ= (ll)5e5;
// ll fact[MXSZ];
// ll mi[MXSZ];

// ll my_gcd(ll n, ll m)
// {
//     if (n % m == 0)
//         return m;
//     if (n < m)
//         swap(n, m);
//     while (m > 0)
//     {
//         n = n % m;
//         swap(n, m);
//     }
//     return n;
// }

ll pw(ll a, ll b) {
  ll c = 1, m = a % md;
  while (b) {
    if (b & 1)
      c = (c * m) % md;
    m = (m * m) % md;
    b /= 2;
  }
  return c;
}

ll modinv(ll n) { return pw(n, md - 2); }

inline ll add(ll a, ll b) { return (md + a % md + b % md) % md; }
inline ll subt(ll a, ll b) { return (a % md - b % md + md) % md; }
inline ll mult(ll a, ll b) { return (1ll * (a % md) * (b % md)) % md; }

// ll ncr(int n, int r)
// {
//     if (!n)
//         return 1;
//     return mult(fact[n], mult(mi[r], mi[n - r]));
// }

// void pre()
// {
//     fact[0] = 1;
//     ll lim = MXSZ;
//     f(i, 1, lim) fact[i] = mult(i, fact[i - 1]);

//     f(i, 0, lim) mi[i] = modinv(fact[i]);
//     // f(i, 0, 10) cout << mi[i] << sp;
//     // cout << nl;
// }
template <class T> T myceil(T a, T b) {
  if (a % b)
    return (a / b) + 1;
  else
    return a / b;
};

const ll MX = 3e3 + 5;

ll dp[MX][MX];
ll btr[MX][MX][2];
ll fl[MX][MX];
ll last[MX];

void solvetestcase(ll tcn) {
  string s, t;
  cin >> s;
  cin >> t;
  ll n = s.size();
  ll m = t.size();

  f(i, 1, n + 1) f(j, 1, m + 1) {
    dp[i][j] = max({dp[i][j - 1], dp[i - 1][j],
                    dp[i - 1][j - 1] + (s[i - 1] == t[j - 1])});
    if (dp[i][j] == dp[i - 1][j - 1] + (s[i - 1] == t[j - 1])) {
      btr[i][j][0] = i - 1;
      btr[i][j][1] = j - 1;
      fl[i][j] = (s[i - 1] == t[j - 1]);
    } else if (dp[i][j] == dp[i][j - 1]) {
      btr[i][j][0] = i;
      btr[i][j][1] = j - 1;
    } else {
      btr[i][j][0] = i - 1;
      btr[i][j][1] = j;
    }
  }
  if (!dp[n][m]) {
    cout << "\n";
    return;
  }
  string ans;
  int x = n;
  int y = m;
  // cout<<dp[n][m]<<nl;
  while (x > 0 && y > 0) {
    if (fl[x][y])
      ans.pb(s[x - 1]);
    ll xx = x;
    x = btr[x][y][0];
    y = btr[xx][y][1];
  }
  reverse(all(ans));
  cout << ans << nl;
}

int main() {
  fio;
  int t = 1;
  // cin >> t;
  // pre();
  f(i, 1, t + 1) {
    // cout << "Case #" << i << ": ";
    solvetestcase(i);

    // cout << nl;
  }
  return 0;
};