Problem p03165 (C++)

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

// C++14 (GCC 5.4.1)

#define LLINF (1LL << 60)
typedef long long ll;
#define mod 1000000007

#define repd(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
#define rrepd(i, a, b) for (int i = (a); i >= (b); i--)

template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

template <typename T> inline T divrup(T a, T b) {
  if (a % b)
    return a / b + 1;
  else
    return a / b;
}
template <typename T> inline string toString(const T &a) {
  ostringstream oss;
  oss << a;
  return oss.str();
}

#define dbg_v1(i, V)                                                           \
  do {                                                                         \
    cout << "-v1----\n";                                                       \
    rep(i, V.size()) cout << " " << i << "\t:" << V[i] << endl;                \
    cout << endl;                                                              \
  } while (0)
#define dbg_v2(i, k, V)                                                        \
  do {                                                                         \
    cout << "-v2----\n";                                                       \
    rep(i, V.size()) {                                                         \
      rep(k, V[0].size()) cout << " " << V[i][k];                              \
      cout << endl;                                                            \
    }                                                                          \
  } while (0)

int main() {
  ios::sync_with_stdio(false); // stdoutとcoutの同期解除
  cin.tie(nullptr);            // cinとcoutの同期解除

  string s, t;
  cin >> s >> t;
  vector<vector<int>> dp(3000, vector<int>(3000, 0));
  int ss = s.size(), ts = t.size();
  rep(i, ss) rep(k, ts) {
    if (s[i] == t[k])
      chmax(dp[i + 1][k + 1], dp[i][k] + 1);
    chmax(dp[i + 1][k + 1], dp[i + 1][k]);
    chmax(dp[i + 1][k + 1], dp[i][k + 1]);
  }
  string ans = "";
  int d = dp[ss][ts];
  while (true) {
    if (d == 0)
      break;
    if (dp[ss - 1][ts] == d)
      ss--;
    else if (dp[ss][ts - 1] == d)
      ts--;
    else {
      ans = s[ss - 1] + ans;
      ss--, ts--, d--;
    }
  }

  cout << ans << endl;
  return (0);
}

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

// C++14 (GCC 5.4.1)

#define LLINF (1LL << 60)
typedef long long ll;
#define mod 1000000007

#define repd(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
#define rrepd(i, a, b) for (int i = (a); i >= (b); i--)

template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

template <typename T> inline T divrup(T a, T b) {
  if (a % b)
    return a / b + 1;
  else
    return a / b;
}
template <typename T> inline string toString(const T &a) {
  ostringstream oss;
  oss << a;
  return oss.str();
}

#define dbg_v1(i, V)                                                           \
  do {                                                                         \
    cout << "-v1----\n";                                                       \
    rep(i, V.size()) cout << " " << i << "\t:" << V[i] << endl;                \
    cout << endl;                                                              \
  } while (0)
#define dbg_v2(i, k, V)                                                        \
  do {                                                                         \
    cout << "-v2----\n";                                                       \
    rep(i, V.size()) {                                                         \
      rep(k, V[0].size()) cout << " " << V[i][k];                              \
      cout << endl;                                                            \
    }                                                                          \
  } while (0)

int main() {
  ios::sync_with_stdio(false); // stdoutとcoutの同期解除
  cin.tie(nullptr);            // cinとcoutの同期解除

  string s, t;
  cin >> s >> t;
  vector<vector<int>> dp(3001, vector<int>(3001, 0));
  int ss = s.size(), ts = t.size();
  rep(i, ss) rep(k, ts) {
    if (s[i] == t[k])
      chmax(dp[i + 1][k + 1], dp[i][k] + 1);
    chmax(dp[i + 1][k + 1], dp[i + 1][k]);
    chmax(dp[i + 1][k + 1], dp[i][k + 1]);
  }
  string ans = "";
  int d = dp[ss][ts];
  while (true) {
    if (d == 0)
      break;
    if (dp[ss - 1][ts] == d)
      ss--;
    else if (dp[ss][ts - 1] == d)
      ts--;
    else {
      ans = s[ss - 1] + ans;
      ss--, ts--, d--;
    }
  }

  cout << ans << endl;
  return (0);
}