Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#ifndef TEMPLATE
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
#ifdef LOCAL
#include <debug.h>
#else
#define db(...) 21
#endif
#define mp make_pair
#define sz(a) ((int)(a).size())
#define mem(x, v) memset((x), (v), sizeof((x)));
#define A(x) (x).begin(), (x).end()
#define re(i, n) for (int i = 0; i < (int)n; i++)
#define rep(i, k, n) for (int i = k; i <= n; i++)
#define erp(i, k, n) for (int i = n; i >= k; i--)
#define input(v, n) re(i, n) cin >> v[i]
using vi = vector<int>;
using vpi = vector<pair<int, int>>;
using vvi = vector<vi>;
using tp = tuple<int, int, int>;
using pi = pair<int, int>;

const int nax = 1e5 + 7;
const int mod = 1e9 + 7; // 1e9+2667 :: 0xcf : inf, 0xc0 -inf
const int inf = numeric_limits<int>::max() - (int)9e6;
#endif
// ===================== MANASH =================== //

int n;
char ch[3001][3001];
pair<int, int> par[3001][3001];
void test() {

  string a, b;
  cin >> a >> b;
  int m;
  n = sz(a), m = sz(b);
  vvi dp(n + 1, vi(m + 1));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (a[i - 1] == b[j - 1]) {
        dp[i][j] = 1 + dp[i - 1][j - 1];
        ch[i][j] = a[i - 1];
        par[i][j] = {i - 1, j - 1};
      } else if (dp[i - 1][j] > dp[i][j - 1]) {
        dp[i][j] = dp[i - 1][j];
        ch[i][j] = ch[i - 1][j];
        par[i][j] = par[i - 1][j];
      } else {
        dp[i][j] = dp[i][j - 1];
        ch[i][j] = ch[i][j - 1];
        par[i][j] = par[i][j - 1];
      }
    }
  }
  string res;
  int i = n, j = m;
  while (i and j) {

    assert(ch[i][j]);
    res.push_back(ch[i][j]);
    auto [x, y] = par[i][j];
    i = x, j = y;
  }
  reverse(A(res));
  cout << res;
}

signed main() {

  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif
  int T = 1;
  // cin >> T;
  for (int tt = 1; tt <= T; tt++) {
    // cout << "Case #" << tt << " ";
    test();
    cout << "\n";
  }
  return 0;
}

Fixed submission:
#ifndef TEMPLATE
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
#ifdef LOCAL
#include <debug.h>
#else
#define db(...) 21
#endif
#define mp make_pair
#define sz(a) ((int)(a).size())
#define mem(x, v) memset((x), (v), sizeof((x)));
#define A(x) (x).begin(), (x).end()
#define re(i, n) for (int i = 0; i < (int)n; i++)
#define rep(i, k, n) for (int i = k; i <= n; i++)
#define erp(i, k, n) for (int i = n; i >= k; i--)
#define input(v, n) re(i, n) cin >> v[i]
using vi = vector<int>;
using vpi = vector<pair<int, int>>;
using vvi = vector<vi>;
using tp = tuple<int, int, int>;
using pi = pair<int, int>;

const int nax = 1e5 + 7;
const int mod = 1e9 + 7; // 1e9+2667 :: 0xcf : inf, 0xc0 -inf
const int inf = numeric_limits<int>::max() - (int)9e6;
#endif
// ===================== MANASH =================== //

int n;
char ch[3001][3001];
pair<int, int> par[3001][3001];
void test() {

  string a, b;
  cin >> a >> b;
  int m;
  n = sz(a), m = sz(b);
  vvi dp(n + 1, vi(m + 1));
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (a[i - 1] == b[j - 1]) {
        dp[i][j] = 1 + dp[i - 1][j - 1];
        ch[i][j] = a[i - 1];
        par[i][j] = {i - 1, j - 1};
      } else if (dp[i - 1][j] > dp[i][j - 1]) {
        dp[i][j] = dp[i - 1][j];
        ch[i][j] = ch[i - 1][j];
        par[i][j] = par[i - 1][j];
      } else {
        dp[i][j] = dp[i][j - 1];
        ch[i][j] = ch[i][j - 1];
        par[i][j] = par[i][j - 1];
      }
    }
  }
  string res;
  int i = n, j = m;
  while (ch[i][j]) {
    assert(ch[i][j]);
    res.push_back(ch[i][j]);
    auto [x, y] = par[i][j];
    i = x, j = y;
  }
  reverse(A(res));
  cout << res;
}

signed main() {

  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif
  int T = 1;
  // cin >> T;
  for (int tt = 1; tt <= T; tt++) {
    // cout << "Case #" << tt << " ";
    test();
    cout << "\n";
  }
  return 0;
}