Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>

#include <stack>

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;

// #define rep(i,n) for(int i=0;i<(n);++i)
#define all(a) (a).begin(), (a).end()
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) for (int i = 0; i < (n); ++i)
#define repi(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define _fromto2(i, n) for (int i = 0; i <= (n); ++i)
#define _fromto3(i, a, b) for (int i = (a); i <= (b); ++i)
#define fromto(...) _overload3(__VA_ARGS__, _fromto3, _fromto2, )(__VA_ARGS__)

#define rrep(i, n) for (int i = (n); i >= 0; --i)
#define iter(it, v) for (auto it = v.begin(); it != v.end(); ++it)
const int MOD = 1e9 + 7;
const int INF = 1e9;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

// debug
template <class T> ostream &operator<<(ostream &os, vector<T> &vec) {
  os << "{";
  for (int i = 0; i < vec.size(); ++i)
    os << (i ? ", " : "") << vec[i];
  os << "}";
  return os;
}
template <class T1, class T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &rhs) {
  out << "(" << rhs.first << ", " << rhs.second << ")";
  return out;
}
#include <type_traits>
template <
    typename T, size_t SIZE,
    enable_if_t<!is_same<remove_cv_t<T>, char>::value, nullptr_t> = nullptr>
ostream &operator<<(ostream &os, T (&arr)[SIZE]) {
  os << "{";
  for (size_t i = 0; i < SIZE; ++i)
    os << (i ? ", " : "") << arr[i];
  os << "}";
  return os;
}
#ifdef LOCAL
#define debug(v) cerr << v << "\n"
#else
#define debug(...) 42
#endif

int main() {
  string s, t;
  cin >> s >> t;
  int N = s.size(), M = t.size();

  int dp[N + 1][M + 1];
  rep(i, N + 1) rep(j, M + 1) dp[i][j] = 0;

  rep(i, N) rep(j, M) {
    if (s[i] == t[j])
      dp[i + 1][j + 1] = 1 + dp[i][j];
    else
      dp[i + 1][j + 1] = max(dp[i][j + 1], max(dp[i + 1][j], dp[i][j]));
  }

  stack<char> st;
  int lcs = dp[N][M];
  int m = M;
  rrep(i, N) {
    if (dp[i][m] == lcs - 1) {
      st.push(s[i]);
      while (dp[i][m] == lcs - 1)
        --m;
      ++m;
      --lcs;
    }
  }
  while (!st.empty()) {
    cout << st.top();
    st.pop();
  }
  cout << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>

#include <stack>

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;

// #define rep(i,n) for(int i=0;i<(n);++i)
#define all(a) (a).begin(), (a).end()
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) for (int i = 0; i < (n); ++i)
#define repi(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define _fromto2(i, n) for (int i = 0; i <= (n); ++i)
#define _fromto3(i, a, b) for (int i = (a); i <= (b); ++i)
#define fromto(...) _overload3(__VA_ARGS__, _fromto3, _fromto2, )(__VA_ARGS__)

#define rrep(i, n) for (int i = (n); i >= 0; --i)
#define iter(it, v) for (auto it = v.begin(); it != v.end(); ++it)
const int MOD = 1e9 + 7;
const int INF = 1e9;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

// debug
template <class T> ostream &operator<<(ostream &os, vector<T> &vec) {
  os << "{";
  for (int i = 0; i < vec.size(); ++i)
    os << (i ? ", " : "") << vec[i];
  os << "}";
  return os;
}
template <class T1, class T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &rhs) {
  out << "(" << rhs.first << ", " << rhs.second << ")";
  return out;
}
#include <type_traits>
template <
    typename T, size_t SIZE,
    enable_if_t<!is_same<remove_cv_t<T>, char>::value, nullptr_t> = nullptr>
ostream &operator<<(ostream &os, T (&arr)[SIZE]) {
  os << "{";
  for (size_t i = 0; i < SIZE; ++i)
    os << (i ? ", " : "") << arr[i];
  os << "}";
  return os;
}
#ifdef LOCAL
#define debug(v) cerr << v << "\n"
#else
#define debug(...) 42
#endif

int main() {
  string s, t;
  cin >> s >> t;
  int N = s.size(), M = t.size();

  int dp[N + 1][M + 1];
  rep(i, N + 1) rep(j, M + 1) dp[i][j] = 0;

  rep(i, N) rep(j, M) {
    if (s[i] == t[j])
      dp[i + 1][j + 1] = 1 + dp[i][j];
    else
      dp[i + 1][j + 1] = max(dp[i][j + 1], max(dp[i + 1][j], dp[i][j]));
  }

  stack<char> st;
  int lcs = dp[N][M];
  int m = M;
  rrep(i, N) {
    if (dp[i][m] == lcs - 1) {
      st.push(s[i]);
      while (m >= 0 && dp[i][m] == lcs - 1)
        --m;
      ++m;
      --lcs;
    }
  }
  while (!st.empty()) {
    cout << st.top();
    st.pop();
  }
  cout << endl;

  return 0;
}