Problem p03165 (C++)

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

#define ff first
#define ss second
#define int long long
#define in insert
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n]
#define w(x)                                                                   \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)
#define FIO                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

int32_t main() {
  FIO;
  string a, b;
  cin >> a >> b;
  int dp[a.length() + 1][b.length() + 1];

  for (int i = 0; i <= (int)a.length(); i++) {
    for (int j = 0; j <= (int)b.length(); j++) {
      if (i == 0 || j == 0) {
        dp[i][j] = 0;
        continue;
      }

      if (a[i - 1] == b[j - 1])
        dp[i][j] = dp[i - 1][j - 1] + 1;
      else
        dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);
    }
  }

  string ans;

  int n = a.length(), m = b.length();
  while (n > 0 || m > 0) {
    if (dp[n][m] == dp[n - 1][m])
      n--;
    else if (dp[n][m] == dp[n][m - 1])
      m--;
    else
      ans += a[n - 1], n--, m--;
  }

  reverse(ans.begin(), ans.end());
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define ff first
#define ss second
#define int long long
#define in insert
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define pii pair<int, int>
#define vi vector<int>
#define mii map<int, int>
#define pqb priority_queue<int>
#define pqs priority_queue<int, vi, greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n]
#define w(x)                                                                   \
  int x;                                                                       \
  cin >> x;                                                                    \
  while (x--)
#define FIO                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

int32_t main() {
  FIO;
  string a, b;
  cin >> a >> b;
  int dp[a.length() + 1][b.length() + 1];

  for (int i = 0; i <= (int)a.length(); i++) {
    for (int j = 0; j <= (int)b.length(); j++) {
      if (i == 0 || j == 0) {
        dp[i][j] = 0;
        continue;
      }

      if (a[i - 1] == b[j - 1])
        dp[i][j] = dp[i - 1][j - 1] + 1;
      else
        dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);
    }
  }

  string ans;

  int n = a.length(), m = b.length();
  while (n > 0 && m > 0) {
    if (dp[n][m] == dp[n - 1][m])
      n--;
    else if (dp[n][m] == dp[n][m - 1])
      m--;
    else
      ans += a[n - 1], n--, m--;
  }

  reverse(ans.begin(), ans.end());
  cout << ans << endl;

  return 0;
}