Problem p03165 (C++)

Buggy submission (status: Runtime Error):
// #include "bits/stdc++.h"

#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, a, b) for (int i = (a), i##_len = (b); i < i##_len; i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)

#define int ll
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair

// typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<double, double> pdd;
typedef vector<vector<int>> mat;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string S, T;
  cin >> S >> T;
  vector<vector<int>> DP(3010, vector<int>(3010, 0));
  vector<vector<pii>> PAR(3010, vector<pii>(3010, mp(0, 0)));

  rep(i, 0, SZ(S)) rep(j, 0, SZ(T)) {
    if (S[i] == T[j]) {
      DP[i + 1][j + 1] = DP[i][j] + 1;
      PAR[i][j] = mp(i - 1, j - 1);
    } else {
      if (chmax(DP[i + 1][j + 1], DP[i][j + 1])) {
        PAR[i][j] = mp(i - 1, j);
      }
      if (chmax(DP[i + 1][j + 1], DP[i + 1][j])) {
        PAR[i][j] = mp(i, j - 1);
      }
    }
  }
  string ANS;
  int l = SZ(S) - 1, r = SZ(T) - 1;
  while (true) {
    if (S[l] == T[r])
      ANS += S[l];
    if (l <= 0 && r <= 0)
      break;
    auto par = PAR[l][r];
    l = par.first;
    r = par.second;
  }
  reverse(all(ANS));
  cout << ANS << endl;

  return 0;
}

Fixed submission:
// #include "bits/stdc++.h"

#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, a, b) for (int i = (a), i##_len = (b); i < i##_len; i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)

#define int ll
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair

// typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<double, double> pdd;
typedef vector<vector<int>> mat;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  string S, T;
  cin >> S >> T;
  vector<vector<int>> DP(3010, vector<int>(3010, 0));
  vector<vector<pii>> PAR(3010, vector<pii>(3010, mp(0, 0)));

  rep(i, 0, SZ(S)) rep(j, 0, SZ(T)) {
    if (S[i] == T[j]) {
      DP[i + 1][j + 1] = DP[i][j] + 1;
      PAR[i][j] = mp(i - 1, j - 1);
    } else {
      if (chmax(DP[i + 1][j + 1], DP[i][j + 1])) {
        PAR[i][j] = mp(i - 1, j);
      }
      if (chmax(DP[i + 1][j + 1], DP[i + 1][j])) {
        PAR[i][j] = mp(i, j - 1);
      }
    }
  }
  string ANS;
  int l = SZ(S) - 1, r = SZ(T) - 1;
  while (true) {
    if (S[l] == T[r])
      ANS += S[l];
    if (l < 0 || r < 0 || l == 0 && r == 0)
      break;
    auto par = PAR[l][r];
    l = par.first;
    r = par.second;
  }
  reverse(all(ANS));
  cout << ANS << endl;

  return 0;
}