Problem p03165 (C++)

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

using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;

#define ALL(a) begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define MT make_tuple
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

#define FF first
#define SS second

template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) {
  return is >> p.FF >> p.SS;
}
template <class T> istream &operator>>(istream &is, vector<T> &xs) {
  for (auto &x : xs)
    is >> x;
  return is;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << p.FF << " " << p.SS;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &xs) {
  for (unsigned int i = 0; i < xs.size(); ++i)
    os << (i ? " " : "") << xs[i];
  return os;
}
template <class T> void maxi(T &x, T y) {
  if (x < y)
    x = y;
}
template <class T> void mini(T &x, T y) {
  if (x > y)
    x = y;
}
void debug(istringstream &) {}
template <char sep = ',', class Head, class... Tail>
void debug(istringstream &iss, Head &&head, Tail &&...tail) {
  string name;
  getline(iss, name, ',');
  cerr << sep << name << "=" << head;
  debug(iss, forward<Tail>(tail)...);
}
#define DEBUG(...)                                                             \
  do {                                                                         \
    istringstream ss(#__VA_ARGS__);                                            \
    debug<' '>(ss, __VA_ARGS__);                                               \
    cerr << endl;                                                              \
  } while (0)

const double EPS = 1e-10;
const double PI = acos(-1.0);
const LL MOD = 1e9 + 7;

int dp[3010][3010];
int prv[3010][3010];

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  string S, T;
  cin >> S >> T;

  int N = SZ(S);
  int M = SZ(T);

  REP(i, N) REP(j, M) {
    if (S[i] == T[j]) {
      if (dp[i + 1][j + 1] < dp[i][j] + 1) {
        dp[i + 1][j + 1] = dp[i][j] + 1;
        prv[i + 1][j + 1] = 2;
      }
    }
    if (dp[i + 1][j] < dp[i][j]) {
      dp[i + 1][j] = dp[i][j];
      prv[i + 1][j] = 0;
    }
    if (dp[i][j + 1] < dp[i][j]) {
      dp[i][j + 1] = dp[i][j];
      prv[i][j + 1] = 1;
    }
  }

  int ii = 0, jj = 0;
  REP(i, N + 1) REP(j, M + 1) if (dp[ii][jj] < dp[i][j]) ii = i, jj = j;
  // cout << dp[ii][jj] << endl;

  string ans;
  while (ii + jj) {
    int p = prv[ii][jj];
    if (p == 2) {
      --ii;
      --jj;
      ans += S[ii];
    } else if (p == 1) {
      --jj;
    } else {
      --ii;
    }
  }
  reverse(ALL(ans));
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;

#define ALL(a) begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define MT make_tuple
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

#define FF first
#define SS second

template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) {
  return is >> p.FF >> p.SS;
}
template <class T> istream &operator>>(istream &is, vector<T> &xs) {
  for (auto &x : xs)
    is >> x;
  return is;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << p.FF << " " << p.SS;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &xs) {
  for (unsigned int i = 0; i < xs.size(); ++i)
    os << (i ? " " : "") << xs[i];
  return os;
}
template <class T> void maxi(T &x, T y) {
  if (x < y)
    x = y;
}
template <class T> void mini(T &x, T y) {
  if (x > y)
    x = y;
}
void debug(istringstream &) {}
template <char sep = ',', class Head, class... Tail>
void debug(istringstream &iss, Head &&head, Tail &&...tail) {
  string name;
  getline(iss, name, ',');
  cerr << sep << name << "=" << head;
  debug(iss, forward<Tail>(tail)...);
}
#define DEBUG(...)                                                             \
  do {                                                                         \
    istringstream ss(#__VA_ARGS__);                                            \
    debug<' '>(ss, __VA_ARGS__);                                               \
    cerr << endl;                                                              \
  } while (0)

const double EPS = 1e-10;
const double PI = acos(-1.0);
const LL MOD = 1e9 + 7;

int dp[3010][3010];
int prv[3010][3010];

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  string S, T;
  cin >> S >> T;

  int N = SZ(S);
  int M = SZ(T);

  REP(i, N) REP(j, M) {
    if (S[i] == T[j]) {
      if (dp[i + 1][j + 1] < dp[i][j] + 1) {
        dp[i + 1][j + 1] = dp[i][j] + 1;
        prv[i + 1][j + 1] = 2;
      }
    }
    if (dp[i + 1][j] < dp[i][j]) {
      dp[i + 1][j] = dp[i][j];
      prv[i + 1][j] = 0;
    }
    if (dp[i][j + 1] < dp[i][j]) {
      dp[i][j + 1] = dp[i][j];
      prv[i][j + 1] = 1;
    }
  }

  int ii = 0, jj = 0;
  REP(i, N + 1) REP(j, M + 1) if (dp[ii][jj] < dp[i][j]) ii = i, jj = j;
  // cout << dp[ii][jj] << endl;

  string ans;
  while (ii && jj) {
    int p = prv[ii][jj];
    if (p == 2) {
      --ii;
      --jj;
      ans += S[ii];
    } else if (p == 1) {
      --jj;
    } else {
      --ii;
    }
  }
  reverse(ALL(ans));
  cout << ans << endl;

  return 0;
}