Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
using in = int64_t;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
const double PI = 3.14159265358979323846;
const in MOD = 1000000007;
const in INF = 1 << 30;
#define vec2(a, y, x) vector<vector<int>> a(y, vector<int>(x, 0))
#define vec1(a, n)                                                             \
  vector<int> a(n);                                                            \
  rep(i, n) cin >> a[i]
using P = pair<int, int>;
vector<int> dx = {0, 1, -1, 0};
vector<int> dy = {1, 0, 0, -1};

bool chmax(int &a, int b) {
  if (a < b) {
    a = b;
    return 0;
  }
  return 1;
}
bool chmin(int &a, int b) {
  if (a > b) {
    a = b;
    return 0;
  }
  return 1;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  string s, t;
  cin >> s >> t;
  int xs = s.size(), xt = t.size();
  vec2(dp, xs + 1, xt + 1); // dp[i][j]:=sのi,tのjまでの共通部分列の長さ
  rep(i, xs + 1) rep(j, xt + 1) {
    if (i > 0)
      chmax(dp[i][j], dp[i - 1][j]);
    if (j > 0)
      chmax(dp[i][j], dp[i][j - 1]);
    if (i > 0 && j > 0 && s[i - 1] == t[j - 1])
      chmax(dp[i][j], dp[i - 1][j - 1] + 1);
  }
  cout << dp[4][5] << endl;
  string ans = "";
  int cnt = dp[xs][xt];
  int i = xs, j = xt;
  while (cnt > 0) {
    if (dp[i][j] == dp[i][j - 1])
      j--;
    else if (dp[i][j] == dp[i - 1][j])
      i--;
    else {
      i--;
      ans = s[i] + ans;
      j--;
      cnt--;
    }
  }
  cout << ans << '\n';
}


Fixed submission:
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
using in = int64_t;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
const double PI = 3.14159265358979323846;
const in MOD = 1000000007;
const in INF = 1 << 30;
#define vec2(a, y, x) vector<vector<int>> a(y, vector<int>(x, 0))
#define vec1(a, n)                                                             \
  vector<int> a(n);                                                            \
  rep(i, n) cin >> a[i]
using P = pair<int, int>;
vector<int> dx = {0, 1, -1, 0};
vector<int> dy = {1, 0, 0, -1};

bool chmax(int &a, int b) {
  if (a < b) {
    a = b;
    return 0;
  }
  return 1;
}
bool chmin(int &a, int b) {
  if (a > b) {
    a = b;
    return 0;
  }
  return 1;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  string s, t;
  cin >> s >> t;
  int xs = s.size(), xt = t.size();
  vec2(dp, xs + 1, xt + 1); // dp[i][j]:=sのi,tのjまでの共通部分列の長さ
  rep(i, xs + 1) rep(j, xt + 1) {
    if (i > 0)
      chmax(dp[i][j], dp[i - 1][j]);
    if (j > 0)
      chmax(dp[i][j], dp[i][j - 1]);
    if (i > 0 && j > 0 && s[i - 1] == t[j - 1])
      chmax(dp[i][j], dp[i - 1][j - 1] + 1);
  }
  // cout<<dp[4][5]<<endl;
  string ans = "";
  int cnt = dp[xs][xt];
  int i = xs, j = xt;
  while (cnt > 0) {
    if (dp[i][j] == dp[i][j - 1])
      j--;
    else if (dp[i][j] == dp[i - 1][j])
      i--;
    else {
      i--;
      ans = s[i] + ans;
      j--;
      cnt--;
    }
  }
  cout << ans << '\n';
}