Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ln "\n"
#define pb push_back
#define pll pair<ll, ll>
#define ppll pair<ll, pll>
#define vll vector<ll>
#define vpll vector<pll>
#define vvll vector<vector<ll>>
#define sll stack<ll>
#define qll queue<ll>
#define mp make_pair
#define f first
#define s second
#define bs binary_search
#define lb lower_bound
#define ub upper_bound
#define Test                                                                   \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)
#define fast_io                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define init(x, n, v)                                                          \
  for (ll i = 0; i <= n; i++)                                                  \
    x[i] = v;
#define all(x) x.begin(), x.end()
ll MOD = 1e9 + 7, MAX = 1e18;
int main() {
  fast_io;
  string a, b, c = "";
  ll n, m, i, j;
  cin >> a >> b;
  n = a.size();
  m = b.size();
  ll dp[n + 1][m + 1];
  for (i = 0; i <= n; i++) {
    for (j = 0; j <= m; j++) {
      if (!i || !j)
        dp[i][j] = 0;
      else 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]);
    }
  }
  for (i = n; i > 0;) {
    for (j = m; j > 0;) {
      if (a[i - 1] == b[j - 1])
        c.pb(a[i - 1]), i--, j--;
      else {
        if (dp[i][j] == dp[i - 1][j])
          i--;
        else
          j--;
      }
    }
  }
  reverse(all(c));
  cout << c;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ln "\n"
#define pb push_back
#define pll pair<ll, ll>
#define ppll pair<ll, pll>
#define vll vector<ll>
#define vpll vector<pll>
#define vvll vector<vector<ll>>
#define sll stack<ll>
#define qll queue<ll>
#define mp make_pair
#define f first
#define s second
#define bs binary_search
#define lb lower_bound
#define ub upper_bound
#define Test                                                                   \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)
#define fast_io                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define init(x, n, v)                                                          \
  for (ll i = 0; i <= n; i++)                                                  \
    x[i] = v;
#define all(x) x.begin(), x.end()
ll MOD = 1e9 + 7, MAX = 1e18;
int main() {
  fast_io;
  string a, b, c = "";
  ll n, m, i, j;
  cin >> a >> b;
  n = a.size();
  m = b.size();
  ll dp[n + 1][m + 1];
  for (i = 0; i <= n; i++) {
    for (j = 0; j <= m; j++) {
      if (!i || !j)
        dp[i][j] = 0;
      else 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]);
    }
  }
  i = n, j = m;
  while (i && j) {
    if (a[i - 1] == b[j - 1])
      c.pb(a[i - 1]), i--, j--;
    else {
      if (dp[i][j] == dp[i - 1][j])
        i--;
      else
        j--;
    }
  }
  reverse(all(c));
  cout << c;
  return 0;
}