Problem p03165 (C++)

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

#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0, a1, a2, a3, a4, x, ...) x
#define dump_1(x1) cerr << #x1 << ": " << x1 << endl
#define dump_2(x1, x2)                                                         \
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << endl
#define dump_3(x1, x2, x3)                                                     \
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \
       << x3 << endl
#define dump_4(x1, x2, x3, x4)                                                 \
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \
       << x3 << ", " #x4 << ": " << x4 << endl
#define dump_5(x1, x2, x3, x4, x5)                                             \
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \
       << x3 << ", " #x4 << ": " << x4 << ", " #x5 << ": " << x5 << endl
#define dump(...)                                                              \
  CHOOSE((__VA_ARGS__, dump_5, dump_4, dump_3, dump_2, dump_1, ~))(__VA_ARGS__)
#define check(s) cerr << s << endl

#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end());
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)

using namespace std;

using ll = long long;

vector<int> dx = {0, 1, 0, -1};
vector<int> dy = {1, 0, -1, 0};

const ll LINF = 2e18;
const int INF = 1e9;

void solve(std::string s, std::string t) {
  int N = sz(s);
  int M = sz(t);
  vector<vector<int>> dp(N + 1, vector<int>(M + 1, 0));

  rep(i, N) {
    rep(j, M) {
      int p = dp.at(i + 1).at(j);
      int q = dp.at(i).at(j + 1);
      if (p > q) {
        dp.at(i + 1).at(j + 1) = p;
      } else {
        dp.at(i + 1).at(j + 1) = q;
        if (s.at(i) == t.at(j)) {
          dp.at(i + 1).at(j + 1)++;
        }
      }
    }
  }

  string ans = "";
  int l = dp.at(N).at(M);
  int i = N - 1;
  int j = M - 1;
  while (l > 0) {
    if (s.at(i) == t.at(j)) {
      ans += s.at(i);
      i--;
      j--;
      l--;
    } else if (dp.at(i + 1).at(j + 1) == dp.at(i).at(j + 1) && i > 0) {
      i--;
    } else if (j > 0) {
      j--;
    }
  }
  reverse(all(ans));
  cout << ans << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  std::string s;
  std::cin >> s;
  std::string t;
  std::cin >> t;
  solve(s, t);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0, a1, a2, a3, a4, x, ...) x
#define dump_1(x1) cerr << #x1 << ": " << x1 << endl
#define dump_2(x1, x2)                                                         \
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << endl
#define dump_3(x1, x2, x3)                                                     \
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \
       << x3 << endl
#define dump_4(x1, x2, x3, x4)                                                 \
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \
       << x3 << ", " #x4 << ": " << x4 << endl
#define dump_5(x1, x2, x3, x4, x5)                                             \
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \
       << x3 << ", " #x4 << ": " << x4 << ", " #x5 << ": " << x5 << endl
#define dump(...)                                                              \
  CHOOSE((__VA_ARGS__, dump_5, dump_4, dump_3, dump_2, dump_1, ~))(__VA_ARGS__)
#define check(s) cerr << s << endl

#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end());
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)

using namespace std;

using ll = long long;

vector<int> dx = {0, 1, 0, -1};
vector<int> dy = {1, 0, -1, 0};

const ll LINF = 2e18;
const int INF = 1e9;

void solve(std::string s, std::string t) {
  int N = sz(s);
  int M = sz(t);
  vector<vector<int>> dp(N + 1, vector<int>(M + 1, 0));

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

  string ans = "";
  int l = dp.at(N).at(M);
  int i = N - 1;
  int j = M - 1;
  while (l > 0) {
    if (s.at(i) == t.at(j)) {
      ans += s.at(i);
      i--;
      j--;
      l--;
    } else if (dp.at(i + 1).at(j + 1) == dp.at(i).at(j + 1) && i > 0) {
      i--;
    } else if (j > 0) {
      j--;
    }
  }
  reverse(all(ans));
  cout << ans << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  std::string s;
  std::cin >> s;
  std::string t;
  std::cin >> t;
  solve(s, t);
  return 0;
}